mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / vd_mtga.c
blob861f898f80564fc6acc52d1352f6c3c75f832ecf
1 /*
2 * based on: XreaL's x_r_img_tga.* (http://www.sourceforge.net/projects/xreal/)
3 * libtarga.*
4 * xli's tga.*
6 * Copyright (c) 2002 Tilman Sauerbeck <tsauerbeck@users.sourceforge.net>
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <libavutil/intreadwrite.h>
31 #include "config.h"
32 #include "mp_msg.h"
34 #include "libvo/fastmemcpy.h"
36 #include "vd_internal.h"
38 static const vd_info_t info =
40 "TGA Images decoder",
41 "mtga",
42 "Tilman Sauerbeck, A'rpi",
43 "Tilman Sauerbeck",
44 "only 24bpp and 32bpp RGB targa files support so far"
47 LIBVD_EXTERN(mtga)
49 typedef enum
51 TGA_NO_DATA,
52 TGA_UNCOMP_PALETTED,
53 TGA_UNCOMP_TRUECOLOR,
54 TGA_UNCOMP_GRAYSCALE,
55 TGA_RLE_PALETTED = 9,
56 TGA_RLE_TRUECOLOR,
57 TGA_RLE_GRAYSCALE
58 } TGAImageType;
60 typedef struct
62 unsigned char id_len;
63 unsigned short img_type;
65 unsigned short width;
66 unsigned short height;
68 unsigned char bpp;
69 unsigned char origin; /* 0 = lower left, 1 = upper left */
70 unsigned short start_row;
71 short increment;
72 } TGAInfo;
74 static unsigned int out_fmt = 0;
76 static int last_w = -1;
77 static int last_h = -1;
78 static int last_c = -1;
81 /* to set/get/query special features/parameters */
82 static int control(sh_video_t *sh, int cmd, void *arg, ...)
84 switch (cmd)
86 case VDCTRL_QUERY_FORMAT:
87 if (*((int *) arg) == out_fmt) return CONTROL_TRUE;
88 return CONTROL_FALSE;
90 return CONTROL_UNKNOWN;
93 /* init driver */
94 static int init(sh_video_t *sh)
96 sh->context = calloc(1, sizeof(TGAInfo));
97 last_w = -1;
99 return 1;
103 /* uninit driver */
104 static void uninit(sh_video_t *sh)
106 TGAInfo *info = sh->context;
107 free(info);
108 return;
112 /* decode a runlength-encoded tga */
113 static void decode_rle_tga(TGAInfo *info, unsigned char *data, mp_image_t *mpi)
115 int row, col, replen, i, num_bytes = info->bpp / 8;
116 unsigned char repetitions, packet_header, *final;
118 /* see line 207 to see why this loop is set up like this */
119 for (row = info->start_row; (!info->origin && row) || (info->origin && row < info->height); row += info->increment)
121 final = mpi->planes[0] + mpi->stride[0] * row;
123 for (col = 0; col < info->width; col += repetitions)
125 packet_header = *data++;
126 repetitions = (1 + (packet_header & 0x7f));
127 replen = repetitions * num_bytes;
129 if (packet_header & 0x80) /* runlength encoded packet */
131 memcpy(final, data, num_bytes);
133 // Note: this will be slow when DR to vram!
134 i=num_bytes;
135 while(2*i<=replen){
136 memcpy(final+i,final,i);
137 i*=2;
139 memcpy(final+i,final,replen-i);
140 data += num_bytes;
142 else /* raw packet */
144 fast_memcpy(final, data, replen);
145 data += replen;
148 final += replen;
152 return;
156 static void decode_uncompressed_tga(TGAInfo *info, unsigned char *data, mp_image_t *mpi)
158 unsigned char *final;
159 int row, num_bytes = info->bpp / 8;
161 /* see line 207 to see why this loop is set up like this */
162 for (row = info->start_row; (!info->origin && row) || (info->origin && row < info->height); row += info->increment)
164 final = mpi->planes[0] + mpi->stride[0] * row;
165 fast_memcpy(final, data, info->width * num_bytes);
166 data += info->width * num_bytes;
169 return;
173 static short read_tga_header(unsigned char *buf, TGAInfo *info)
175 info->id_len = buf[0];
177 info->img_type = buf[2];
179 /* targa data is always stored in little endian byte order */
180 info->width = AV_RL16(&buf[12]);
181 info->height = AV_RL16(&buf[14]);
183 info->bpp = buf[16];
185 info->origin = (buf[17] & 0x20) >> 5;
187 /* FIXME check for valid targa data */
189 return 0;
193 /* decode a frame */
194 static mp_image_t *decode(sh_video_t *sh, void *raw, int len, int flags)
196 TGAInfo *info = sh->context;
197 unsigned char *data = raw;
198 mp_image_t *mpi;
201 if (len <= 0)
202 return NULL; /* skip frame */
204 read_tga_header(data, info); /* read information about the file */
206 if (info->bpp == 24)
207 out_fmt = IMGFMT_BGR24;
208 else if (info->bpp == 32)
209 out_fmt = IMGFMT_BGR32;
210 else
212 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "Unsupported TGA type! depth=%d\n",info->bpp);
213 return NULL;
216 if (info->img_type != TGA_UNCOMP_TRUECOLOR && info->img_type != TGA_RLE_TRUECOLOR) /* not a true color image */
218 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "Unsupported TGA type: %i!\n", info->img_type);
219 return NULL;
222 /* if img.origin is 0, we decode from bottom to top. if it's 1, we decode from top to bottom */
223 info->start_row = (info->origin) ? 0 : info->height - 1;
224 info->increment = (info->origin) ? 1 : -1;
226 /* set data to the beginning of the image data */
227 data += 18 + info->id_len;
229 /* (re)init libvo if image parameters changed (width/height/colorspace) */
230 if (last_w != info->width || last_h != info->height || last_c != out_fmt)
232 last_w = info->width;
233 last_h = info->height;
234 last_c = out_fmt;
236 if (!out_fmt || !mpcodecs_config_vo(sh, info->width, info->height, out_fmt))
237 return NULL;
240 if (!(mpi = mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, info->width, info->height)))
241 return NULL;
243 /* finally decode the image */
244 if (info->img_type == TGA_UNCOMP_TRUECOLOR)
245 decode_uncompressed_tga(info, data, mpi);
246 else if (info->img_type == TGA_RLE_TRUECOLOR)
247 decode_rle_tga(info, data, mpi);
248 // else
249 // mpi = NULL;
251 return mpi;