]> git.sesse.net Git - ffmpeg/blob - libavcodec/targa.c
Create a targa.h file to contain common definitions for targa encoder
[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 "libavcore/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 static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
37 {
38     int i, x, y;
39     int depth = (bpp + 1) >> 3;
40     int type, count;
41     int diff;
42
43     diff = stride - w * depth;
44     x = y = 0;
45     while(y < h){
46         type = *src++;
47         count = (type & 0x7F) + 1;
48         type &= 0x80;
49         if((x + count > w) && (x + count + 1 > (h - y) * w)){
50             av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
51             return;
52         }
53         for(i = 0; i < count; i++){
54             switch(depth){
55             case 1:
56                 *dst = *src;
57                 break;
58             case 2:
59                 *((uint16_t*)dst) = AV_RL16(src);
60                 break;
61             case 3:
62                 dst[0] = src[0];
63                 dst[1] = src[1];
64                 dst[2] = src[2];
65                 break;
66             case 4:
67                 *((uint32_t*)dst) = AV_RL32(src);
68                 break;
69             }
70             dst += depth;
71             if(!type)
72                 src += depth;
73
74             x++;
75             if(x == w){
76                 x = 0;
77                 y++;
78                 dst += diff;
79             }
80         }
81         if(type)
82             src += depth;
83     }
84 }
85
86 static int decode_frame(AVCodecContext *avctx,
87                         void *data, int *data_size,
88                         AVPacket *avpkt)
89 {
90     const uint8_t *buf = avpkt->data;
91     int buf_size = avpkt->size;
92     TargaContext * const s = avctx->priv_data;
93     AVFrame *picture = data;
94     AVFrame * const p= (AVFrame*)&s->picture;
95     uint8_t *dst;
96     int stride;
97     int idlen, pal, compr, x, y, w, h, bpp, flags;
98     int first_clr, colors, csize;
99
100     /* parse image header */
101     idlen = *buf++;
102     pal = *buf++;
103     compr = *buf++;
104     first_clr = AV_RL16(buf); buf += 2;
105     colors = AV_RL16(buf); buf += 2;
106     csize = *buf++;
107     x = AV_RL16(buf); buf += 2;
108     y = AV_RL16(buf); buf += 2;
109     w = AV_RL16(buf); buf += 2;
110     h = AV_RL16(buf); buf += 2;
111     bpp = *buf++;
112     flags = *buf++;
113     //skip identifier if any
114     buf += idlen;
115     s->bpp = bpp;
116     s->width = w;
117     s->height = h;
118     switch(s->bpp){
119     case 8:
120         avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
121         break;
122     case 15:
123         avctx->pix_fmt = PIX_FMT_RGB555;
124         break;
125     case 16:
126         avctx->pix_fmt = PIX_FMT_RGB555;
127         break;
128     case 24:
129         avctx->pix_fmt = PIX_FMT_BGR24;
130         break;
131     case 32:
132         avctx->pix_fmt = PIX_FMT_RGB32;
133         break;
134     default:
135         av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
136         return -1;
137     }
138
139     if(s->picture.data[0])
140         avctx->release_buffer(avctx, &s->picture);
141
142     if(av_image_check_size(w, h, 0, avctx))
143         return -1;
144     if(w != avctx->width || h != avctx->height)
145         avcodec_set_dimensions(avctx, w, h);
146     if(avctx->get_buffer(avctx, p) < 0){
147         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
148         return -1;
149     }
150     if(flags & 0x20){
151         dst = p->data[0];
152         stride = p->linesize[0];
153     }else{ //image is upside-down
154         dst = p->data[0] + p->linesize[0] * (h - 1);
155         stride = -p->linesize[0];
156     }
157
158     if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
159         memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
160         if(avctx->palctrl->palette_changed){
161             p->palette_has_changed = 1;
162             avctx->palctrl->palette_changed = 0;
163         }
164     }
165     if(colors){
166         if((colors + first_clr) > 256){
167             av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
168             return -1;
169         }
170         if(csize != 24){
171             av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
172             return -1;
173         }
174         if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
175             buf += colors * ((csize + 1) >> 3);
176         else{
177             int r, g, b, t;
178             int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
179             for(t = 0; t < colors; t++){
180                 r = *buf++;
181                 g = *buf++;
182                 b = *buf++;
183                 *pal++ = (b << 16) | (g << 8) | r;
184             }
185             p->palette_has_changed = 1;
186         }
187     }
188     if((compr & (~TGA_RLE)) == TGA_NODATA)
189         memset(p->data[0], 0, p->linesize[0] * s->height);
190     else{
191         if(compr & TGA_RLE)
192             targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
193         else{
194             for(y = 0; y < s->height; y++){
195 #if HAVE_BIGENDIAN
196                 if((s->bpp + 1) >> 3 == 2){
197                     uint16_t *dst16 = (uint16_t*)dst;
198                     for(x = 0; x < s->width; x++)
199                         dst16[x] = AV_RL16(buf + x * 2);
200                 }else if((s->bpp + 1) >> 3 == 4){
201                     uint32_t *dst32 = (uint32_t*)dst;
202                     for(x = 0; x < s->width; x++)
203                         dst32[x] = AV_RL32(buf + x * 4);
204                 }else
205 #endif
206                     memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
207
208                 dst += stride;
209                 buf += s->width * ((s->bpp + 1) >> 3);
210             }
211         }
212     }
213
214     *picture= *(AVFrame*)&s->picture;
215     *data_size = sizeof(AVPicture);
216
217     return buf_size;
218 }
219
220 static av_cold int targa_init(AVCodecContext *avctx){
221     TargaContext *s = avctx->priv_data;
222
223     avcodec_get_frame_defaults((AVFrame*)&s->picture);
224     avctx->coded_frame= (AVFrame*)&s->picture;
225
226     return 0;
227 }
228
229 static av_cold int targa_end(AVCodecContext *avctx){
230     TargaContext *s = avctx->priv_data;
231
232     if(s->picture.data[0])
233         avctx->release_buffer(avctx, &s->picture);
234
235     return 0;
236 }
237
238 AVCodec targa_decoder = {
239     "targa",
240     AVMEDIA_TYPE_VIDEO,
241     CODEC_ID_TARGA,
242     sizeof(TargaContext),
243     targa_init,
244     NULL,
245     targa_end,
246     decode_frame,
247     CODEC_CAP_DR1,
248     NULL,
249     .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
250 };