]> git.sesse.net Git - vlc/blob - modules/codec/sdl_image.c
* sdl_image.c: fixed decoding of 24/32 bpp images (the color masks were not
[vlc] / modules / codec / sdl_image.c
1 /*****************************************************************************
2  * sdl_image.c: sdl decoder module making use of libsdl_image.
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/decoder.h>
29
30 #include SDL_IMAGE_INCLUDE_FILE
31
32 /*****************************************************************************
33  * decoder_sys_t : sdl decoder descriptor
34  *****************************************************************************/
35 struct decoder_sys_t
36 {
37     const char *psz_sdl_type;
38 };
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  OpenDecoder   ( vlc_object_t * );
44 static void CloseDecoder  ( vlc_object_t * );
45
46 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 vlc_module_begin();
52     set_category( CAT_INPUT );
53     set_subcategory( SUBCAT_INPUT_VCODEC );
54     set_description( _("SDL_image video decoder") );
55     set_capability( "decoder", 900 );
56     set_callbacks( OpenDecoder, CloseDecoder );
57     add_shortcut( "sdl_image" );
58 vlc_module_end();
59
60 static const struct supported_fmt_t
61 {
62     vlc_fourcc_t i_fourcc;
63     char *psz_sdl_type;
64 } p_supported_fmt[] =
65 {
66     { VLC_FOURCC('t','g','a',' '), "TGA" },
67     { VLC_FOURCC('b','m','p',' '), "BMP" },
68     { VLC_FOURCC('p','n','m',' '), "PNM" },
69     { VLC_FOURCC('x','p','m',' '), "XPM" },
70     { VLC_FOURCC('x','c','f',' '), "XCF" },
71     { VLC_FOURCC('p','c','x',' '), "PCX" },
72     { VLC_FOURCC('g','i','f',' '), "GIF" },
73     { VLC_FOURCC('j','p','e','g'), "JPG" },
74     { VLC_FOURCC('t','i','f','f'), "TIF" },
75     { VLC_FOURCC('l','b','m',' '), "LBM" },
76     { VLC_FOURCC('p','n','g',' '), "PNG" }
77 };
78
79 /*****************************************************************************
80  * OpenDecoder: probe the decoder and return score
81  *****************************************************************************/
82 static int OpenDecoder( vlc_object_t *p_this )
83 {
84     decoder_t *p_dec = (decoder_t *)p_this;
85     decoder_sys_t *p_sys;
86     int i;
87
88     /* Find codec. */
89     for ( i = 0;
90           i < (int)(sizeof(p_supported_fmt)/sizeof(struct supported_fmt_t));
91           i++ )
92     {
93         if ( p_supported_fmt[i].i_fourcc == p_dec->fmt_in.i_codec )
94             break;
95     }
96     if ( i == (int)(sizeof(p_supported_fmt)/sizeof(struct supported_fmt_t)) )
97     {
98         return VLC_EGENERIC;
99     }
100
101     /* Allocate the memory needed to store the decoder's structure */
102     if( ( p_dec->p_sys = p_sys =
103           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
104     {
105         msg_Err( p_dec, "out of memory" );
106         return VLC_EGENERIC;
107     }
108     p_sys->psz_sdl_type = p_supported_fmt[i].psz_sdl_type;
109
110     /* Set output properties - this is a decoy and isn't used anywhere */
111     p_dec->fmt_out.i_cat = VIDEO_ES;
112     p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
113
114     /* Set callbacks */
115     p_dec->pf_decode_video = DecodeBlock;
116
117     return VLC_SUCCESS;
118 }
119
120 /****************************************************************************
121  * DecodeBlock: the whole thing
122  ****************************************************************************
123  * This function must be fed with a complete compressed frame.
124  ****************************************************************************/
125 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
126 {
127     decoder_sys_t *p_sys = p_dec->p_sys;
128     block_t *p_block;
129     picture_t *p_pic = NULL;
130     SDL_Surface *p_surface;
131     SDL_RWops *p_rw;
132
133     if( pp_block == NULL || *pp_block == NULL ) return NULL;
134     p_block = *pp_block;
135
136     p_rw = SDL_RWFromConstMem( p_block->p_buffer, p_block->i_buffer );
137
138     /* Decode picture. */
139     p_surface = IMG_LoadTyped_RW( p_rw, 1, (char*)p_sys->psz_sdl_type );
140     if ( p_surface == NULL )
141     {
142         msg_Warn( p_dec, "SDL_image couldn't load the image (%s)",
143                   IMG_GetError() );
144         goto error;
145     }
146
147     switch ( p_surface->format->BitsPerPixel )
148     {
149     case 16:
150         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','1','6');
151         break;
152     case 8:
153     case 24:
154         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','2','4');
155         break;
156     case 32:
157         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
158         break;
159     default:
160         msg_Warn( p_dec, "unknown bits/pixel format (%d)",
161                   p_surface->format->BitsPerPixel );
162         goto error;
163     }
164     p_dec->fmt_out.video.i_width = p_surface->w;
165     p_dec->fmt_out.video.i_height = p_surface->h;
166     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_surface->w
167                                      / p_surface->h;
168
169     /* Get a new picture. */
170     p_pic = p_dec->pf_vout_buffer_new( p_dec );
171     if ( p_pic == NULL ) goto error;
172
173     switch ( p_surface->format->BitsPerPixel )
174     {
175         case 8:
176         {
177             int i, j;
178             uint8_t *r = p_pic->p[0].p_pixels;
179             uint8_t *g = p_pic->p[0].p_pixels + 1;
180             uint8_t *b = p_pic->p[0].p_pixels + 2;
181             SDL_Palette *p_palette = p_surface->format->palette;
182
183             for ( i = 0; i < p_surface->h; i++ )
184             {
185                 for ( j = 0; j < p_surface->w; j++ )
186                 {
187                     uint8_t i_index = ((uint8_t *)p_surface->pixels)[j];
188                     SDL_Color *p_color = &p_palette->colors[i_index];
189                     r[j] = p_color->r;
190                     g[j] = p_color->g;
191                     b[j] = p_color->b;
192                 }
193             }
194             r += p_pic->p[0].i_pitch;
195             g += p_pic->p[0].i_pitch;
196             b += p_pic->p[0].i_pitch;
197             break;
198         }
199         case 16:
200         {
201             int i;
202             uint8_t *p_src = p_surface->pixels;
203             uint8_t *p_dst = p_pic->p[0].p_pixels;
204             int i_pitch = p_pic->p[0].i_pitch < p_surface->pitch ?
205                 p_pic->p[0].i_pitch : p_surface->pitch;
206
207             for ( i = 0; i < p_surface->h; i++ )
208             {
209                 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_pitch );
210                 p_src += p_surface->pitch;
211                 p_dst += p_pic->p[0].i_pitch;
212             }
213             break;
214         }
215         case 24:
216         {
217             int i, j;
218             uint8_t *p_src, *p_dst;
219             uint8_t r, g, b;
220             for ( i = 0; i < p_surface->h; i++ )
221             {
222                 p_src = p_surface->pixels + i * p_surface->pitch;
223                 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
224                 for ( j = 0; j < p_surface->w; j++ )
225                 {
226                     SDL_GetRGB( *(uint32_t*)p_src, p_surface->format,
227                                 &r, &g, &b );
228                     *(p_dst++) = r;
229                     *(p_dst++) = g;
230                     *(p_dst++) = b;
231                     p_src += 3;
232                 }
233             }
234             break;
235         }
236         case 32:
237         {
238             int i, j;
239             uint8_t *p_src, *p_dst;
240             uint8_t r, g, b, a;
241             for ( i = 0; i < p_surface->h; i++ )
242             {
243                 p_src = p_surface->pixels + i * p_surface->pitch;
244                 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
245                 for ( j = 0; j < p_surface->w; j++ )
246                 {
247                     SDL_GetRGBA( *(uint32_t*)p_src, p_surface->format,
248                                 &r, &g, &b, &a );
249                     *(p_dst++) = b;
250                     *(p_dst++) = g;
251                     *(p_dst++) = r;
252                     *(p_dst++) = a;
253                     p_src += 4;
254                 }
255             }
256             break;
257         }
258     }
259
260     SDL_FreeSurface( p_surface );
261     block_Release( p_block ); *pp_block = NULL;
262     return p_pic;
263
264 error:
265     if ( p_surface != NULL ) SDL_FreeSurface( p_surface );
266     block_Release( p_block ); *pp_block = NULL;
267     return NULL;
268 }
269
270 /*****************************************************************************
271  * CloseDecoder: sdl decoder destruction
272  *****************************************************************************/
273 static void CloseDecoder( vlc_object_t *p_this )
274 {
275     decoder_t *p_dec = (decoder_t *)p_this;
276     decoder_sys_t *p_sys = p_dec->p_sys;
277
278     free( p_sys );
279 }