mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / vd_sgi.c
blob1ec9b2e868bfd2906df89973ff1e2a57bfec0b99
1 /*
2 * Copyright (c) 2003 Todd Kirby <slapcat@pacbell.net>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <libavutil/intreadwrite.h>
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "mpbswap.h"
29 #include "vd_internal.h"
31 #define SGI_HEADER_LEN 512
32 #define SGI_MAGIC 474
34 #define SGI_GRAYSCALE_IMAGE 1
35 #define SGI_RGB_IMAGE 3
36 #define SGI_RGBA_IMAGE 4
38 #define OUT_PIXEL_STRIDE 3 /* RGB */
41 static const vd_info_t info =
43 "SGI Image decoder",
44 "sgi",
45 "Todd Kirby",
46 "Todd Kirby",
50 LIBVD_EXTERN(sgi)
52 typedef struct {
53 short magic;
54 char rle;
55 char bytes_per_channel;
56 unsigned short dimension;
57 unsigned short xsize;
58 unsigned short ysize;
59 unsigned short zsize;
60 } SGIInfo;
62 static unsigned int outfmt = IMGFMT_BGR24;
64 static unsigned short last_x = -1;
65 static unsigned short last_y = -1;
68 /* to set/get/query special features/parameters */
69 static int
70 control(sh_video_t* sh, int cmd, void *arg, ...)
72 switch (cmd)
74 case VDCTRL_QUERY_FORMAT:
75 if (*((unsigned int *) arg) == outfmt) {
76 return CONTROL_TRUE;
78 return CONTROL_FALSE;
80 return CONTROL_UNKNOWN;
84 /* init driver */
85 static int
86 init(sh_video_t *sh)
88 sh->context = calloc(1, sizeof(SGIInfo));
89 last_x = -1;
91 return 1;
95 /* uninit driver */
96 static void
97 uninit(sh_video_t *sh)
99 SGIInfo *info = sh->context;
100 free(info);
104 /* expand an rle row into a channel */
105 static void
106 expandrow(unsigned char *optr, unsigned char *iptr, int chan_offset)
108 unsigned char pixel, count;
109 optr += chan_offset;
111 while (1) {
112 pixel = *iptr++;
114 if (!(count = (pixel & 0x7f))) {
115 return;
117 if(pixel & 0x80) {
118 while (count--) {
119 *optr = *iptr;
120 optr += OUT_PIXEL_STRIDE;
121 iptr++;
123 } else {
124 pixel = *iptr++;
126 while (count--) {
127 *optr = pixel;
128 optr += OUT_PIXEL_STRIDE;
135 /* expand an rle row into all 3 channels.
136 a separate function for grayscale so we don't slow down the
137 more common case rgb function with a bunch of ifs. */
138 static void
139 expandrow_gs(unsigned char *optr, unsigned char *iptr)
141 unsigned char pixel, count;
143 while (1) {
144 pixel = *iptr++;
146 if (!(count = (pixel & 0x7f))) {
147 return;
149 if(pixel & 0x80) {
150 while (count--) {
151 optr[0] = *iptr;
152 optr[1] = *iptr;
153 optr[2] = *iptr;
154 optr += OUT_PIXEL_STRIDE;
155 iptr++;
157 } else {
158 pixel = *iptr++;
160 while (count--) {
161 optr[0] = pixel;
162 optr[1] = pixel;
163 optr[2] = pixel;
164 optr += OUT_PIXEL_STRIDE;
171 /* decode a run length encoded sgi image */
172 static void
173 decode_rle_sgi(SGIInfo *info, unsigned char *data, mp_image_t *mpi)
175 unsigned char *rle_data, *dest_row;
176 uint32_t *starttab;
177 int y, z, ysize, zsize, chan_offset;
178 long start_offset;
180 ysize = info->ysize;
181 zsize = info->zsize;
183 /* rle offset table is right after the header */
184 starttab = (uint32_t*)(data + SGI_HEADER_LEN);
186 for (z = 0; z < zsize; z++) {
188 /* set chan_offset so RGB ends up BGR */
189 chan_offset = (zsize - 1) - z;
191 /* The origin for SGI images is the lower-left corner
192 so read scan lines from bottom to top */
193 for (y = ysize - 1; y >= 0; y--) {
194 dest_row = mpi->planes[0] + mpi->stride[0] * (ysize - 1 - y);
196 /* set start of next run (offsets are from start of header) */
197 start_offset = AV_RB32(&starttab[y + z * ysize]);
199 rle_data = &data[start_offset];
201 if(info->zsize == SGI_GRAYSCALE_IMAGE) {
202 expandrow_gs(dest_row, rle_data);
203 } else {
204 expandrow(dest_row, rle_data, chan_offset);
211 /* decode an sgi image */
212 static void
213 decode_uncompressed_sgi(SGIInfo *info, unsigned char *data, mp_image_t *mpi)
215 unsigned char *src_row, *dest_row;
216 int x, y, z, xsize, ysize, zsize, chan_offset;
218 xsize = info->xsize;
219 ysize = info->ysize;
220 zsize = info->zsize;
222 /* skip header */
223 data += SGI_HEADER_LEN;
225 for (z = 0; z < zsize; z++) {
227 /* set row ptr to start of current plane */
228 src_row = data + (xsize * ysize * z);
230 /* set chan_offset for RGB -> BGR */
231 chan_offset = (zsize - 1) - z;
233 /* the origin for SGI images is the lower-left corner
234 so read scan lines from bottom to top. */
235 for (y = ysize - 1; y >= 0; y--) {
236 dest_row = mpi->planes[0] + mpi->stride[0] * y;
237 for (x = 0; x < xsize; x++) {
239 /* we only do 24 bit output so promote 8 bit pixels to 24 */
240 if (zsize == SGI_GRAYSCALE_IMAGE) {
241 /* write greyscale value into all channels */
242 dest_row[0] = src_row[x];
243 dest_row[1] = src_row[x];
244 dest_row[2] = src_row[x];
245 } else {
246 dest_row[chan_offset] = src_row[x];
249 dest_row += OUT_PIXEL_STRIDE;
252 /* move to next row of the current source plane */
253 src_row += xsize;
259 /* read sgi header fields */
260 static void
261 read_sgi_header(unsigned char *buf, SGIInfo *info)
263 /* sgi data is always stored in big endian byte order */
264 info->magic = AV_RB16(&buf[0]);
265 info->rle = buf[2];
266 info->bytes_per_channel = buf[3];
267 info->dimension = AV_RB16(&buf[4]);
268 info->xsize = AV_RB16(&buf[6]);
269 info->ysize = AV_RB16(&buf[8]);
270 info->zsize = AV_RB16(&buf[10]);
274 /* decode a frame */
275 static
276 mp_image_t *decode(sh_video_t *sh, void *raw, int len, int flags)
278 SGIInfo *info = sh->context;
279 unsigned char *data = raw;
280 mp_image_t *mpi;
282 if (len <= 0) {
283 return NULL; /* skip frame */
286 read_sgi_header(data, info);
288 /* make sure this is an SGI image file */
289 if (info->magic != SGI_MAGIC) {
290 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "Bad magic number in image.\n");
291 return NULL;
294 /* check image depth */
295 if (info->bytes_per_channel != 1) {
296 mp_msg(MSGT_DECVIDEO, MSGL_INFO,
297 "Unsupported bytes per channel value %i.\n", info->bytes_per_channel);
298 return NULL;
301 /* check image dimension */
302 if (info->dimension != 2 && info->dimension != 3) {
303 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "Unsupported image dimension %i.\n",
304 info->dimension);
305 return NULL;
308 /* change rgba images to rgb so alpha channel will be ignored */
309 if (info->zsize == SGI_RGBA_IMAGE) {
310 info->zsize = SGI_RGB_IMAGE;
313 /* check image depth */
314 if (info->zsize != SGI_RGB_IMAGE && info->zsize != SGI_GRAYSCALE_IMAGE) {
315 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "Unsupported image depth.\n");
316 return NULL;
319 /* (re)init libvo if image size is changed */
320 if (last_x != info->xsize || last_y != info->ysize)
322 last_x = info->xsize;
323 last_y = info->ysize;
325 if (!mpcodecs_config_vo(sh, info->xsize, info->ysize, outfmt)) {
326 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "Config vo failed:\n");
327 return NULL;
331 if (!(mpi = mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
332 info->xsize, info->ysize))) {
333 return NULL;
336 if (info->rle) {
337 decode_rle_sgi(info, data, mpi);
338 } else {
339 decode_uncompressed_sgi(info, data, mpi);
342 return mpi;