2 * based on: XreaL's x_r_img_tga.* (http://www.sourceforge.net/projects/xreal/)
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.
29 #include <libavutil/intreadwrite.h>
34 #include "libvo/fastmemcpy.h"
36 #include "vd_internal.h"
38 static const vd_info_t info
=
42 "Tilman Sauerbeck, A'rpi",
44 "only 24bpp and 32bpp RGB targa files support so far"
63 unsigned short img_type
;
66 unsigned short height
;
69 unsigned char origin
; /* 0 = lower left, 1 = upper left */
70 unsigned short start_row
;
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
, ...)
86 case VDCTRL_QUERY_FORMAT
:
87 if (*((int *) arg
) == out_fmt
) return CONTROL_TRUE
;
90 return CONTROL_UNKNOWN
;
94 static int init(sh_video_t
*sh
)
96 sh
->context
= calloc(1, sizeof(TGAInfo
));
104 static void uninit(sh_video_t
*sh
)
106 TGAInfo
*info
= sh
->context
;
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!
136 memcpy(final
+i
,final
,i
);
139 memcpy(final
+i
,final
,replen
-i
);
142 else /* raw packet */
144 fast_memcpy(final
, data
, replen
);
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
;
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]);
185 info
->origin
= (buf
[17] & 0x20) >> 5;
187 /* FIXME check for valid targa data */
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
;
202 return NULL
; /* skip frame */
204 read_tga_header(data
, info
); /* read information about the file */
207 out_fmt
= IMGFMT_BGR24
;
208 else if (info
->bpp
== 32)
209 out_fmt
= IMGFMT_BGR32
;
212 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "Unsupported TGA type! depth=%d\n",info
->bpp
);
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
);
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
;
236 if (!out_fmt
|| !mpcodecs_config_vo(sh
, info
->width
, info
->height
, out_fmt
))
240 if (!(mpi
= mpcodecs_get_image(sh
, MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
, info
->width
, info
->height
)))
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
);