]> git.sesse.net Git - vlc/blob - modules/codec/sdl_image.c
* More compiler warning fixes (const mostly)
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_codec.h>
29 #include <vlc_vout.h>
30
31 #include SDL_IMAGE_INCLUDE_FILE
32
33 /*****************************************************************************
34  * decoder_sys_t : sdl decoder descriptor
35  *****************************************************************************/
36 struct decoder_sys_t
37 {
38     const char *psz_sdl_type;
39 };
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  OpenDecoder   ( vlc_object_t * );
45 static void CloseDecoder  ( vlc_object_t * );
46
47 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_category( CAT_INPUT );
54     set_subcategory( SUBCAT_INPUT_VCODEC );
55     set_description( _("SDL_image video decoder") );
56     set_capability( "decoder", 60 );
57     set_callbacks( OpenDecoder, CloseDecoder );
58     add_shortcut( "sdl_image" );
59 vlc_module_end();
60
61 static const struct supported_fmt_t
62 {
63     vlc_fourcc_t i_fourcc;
64     const char *psz_sdl_type;
65 } p_supported_fmt[] =
66 {
67     { VLC_FOURCC('t','g','a',' '), "TGA" },
68     { VLC_FOURCC('b','m','p',' '), "BMP" },
69     { VLC_FOURCC('p','n','m',' '), "PNM" },
70     { VLC_FOURCC('x','p','m',' '), "XPM" },
71     { VLC_FOURCC('x','c','f',' '), "XCF" },
72     { VLC_FOURCC('p','c','x',' '), "PCX" },
73     { VLC_FOURCC('g','i','f',' '), "GIF" },
74     { VLC_FOURCC('j','p','e','g'), "JPG" },
75     { VLC_FOURCC('t','i','f','f'), "TIF" },
76     { VLC_FOURCC('l','b','m',' '), "LBM" },
77     { VLC_FOURCC('p','n','g',' '), "PNG" }
78 };
79
80 /*****************************************************************************
81  * OpenDecoder: probe the decoder and return score
82  *****************************************************************************/
83 static int OpenDecoder( vlc_object_t *p_this )
84 {
85     decoder_t *p_dec = (decoder_t *)p_this;
86     decoder_sys_t *p_sys;
87     int i;
88
89     /* Find codec. */
90     for ( i = 0;
91           i < (int)(sizeof(p_supported_fmt)/sizeof(struct supported_fmt_t));
92           i++ )
93     {
94         if ( p_supported_fmt[i].i_fourcc == p_dec->fmt_in.i_codec )
95             break;
96     }
97     if ( i == (int)(sizeof(p_supported_fmt)/sizeof(struct supported_fmt_t)) )
98     {
99         return VLC_EGENERIC;
100     }
101
102     /* Allocate the memory needed to store the decoder's structure */
103     if( ( p_dec->p_sys = p_sys =
104           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
105     {
106         msg_Err( p_dec, "out of memory" );
107         return VLC_EGENERIC;
108     }
109     p_sys->psz_sdl_type = p_supported_fmt[i].psz_sdl_type;
110
111     /* Set output properties - this is a decoy and isn't used anywhere */
112     p_dec->fmt_out.i_cat = VIDEO_ES;
113     p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
114
115     /* Set callbacks */
116     p_dec->pf_decode_video = DecodeBlock;
117
118     return VLC_SUCCESS;
119 }
120
121 /****************************************************************************
122  * DecodeBlock: the whole thing
123  ****************************************************************************
124  * This function must be fed with a complete compressed frame.
125  ****************************************************************************/
126 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
127 {
128     decoder_sys_t *p_sys = p_dec->p_sys;
129     block_t *p_block;
130     picture_t *p_pic = NULL;
131     SDL_Surface *p_surface;
132     SDL_RWops *p_rw;
133
134     if( pp_block == NULL || *pp_block == NULL ) return NULL;
135     p_block = *pp_block;
136
137     p_rw = SDL_RWFromConstMem( p_block->p_buffer, p_block->i_buffer );
138
139     /* Decode picture. */
140     p_surface = IMG_LoadTyped_RW( p_rw, 1, (char*)p_sys->psz_sdl_type );
141     if ( p_surface == NULL )
142     {
143         msg_Warn( p_dec, "SDL_image couldn't load the image (%s)",
144                   IMG_GetError() );
145         goto error;
146     }
147
148     switch ( p_surface->format->BitsPerPixel )
149     {
150     case 16:
151         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','1','6');
152         break;
153     case 8:
154     case 24:
155         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','2','4');
156         break;
157     case 32:
158         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
159         break;
160     default:
161         msg_Warn( p_dec, "unknown bits/pixel format (%d)",
162                   p_surface->format->BitsPerPixel );
163         goto error;
164     }
165     p_dec->fmt_out.video.i_width = p_surface->w;
166     p_dec->fmt_out.video.i_height = p_surface->h;
167     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_surface->w
168                                      / p_surface->h;
169
170     /* Get a new picture. */
171     p_pic = p_dec->pf_vout_buffer_new( p_dec );
172     if ( p_pic == NULL ) goto error;
173
174     switch ( p_surface->format->BitsPerPixel )
175     {
176         case 8:
177         {
178             int i, j;
179             uint8_t *p_src, *p_dst;
180             uint8_t r, g, b;
181             for ( i = 0; i < p_surface->h; i++ )
182             {
183                 p_src = p_surface->pixels + i * p_surface->pitch;
184                 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
185                 for ( j = 0; j < p_surface->w; j++ )
186                 {
187                     SDL_GetRGB( *(p_src++), p_surface->format,
188                                 &r, &g, &b );
189                     *(p_dst++) = r;
190                     *(p_dst++) = g;
191                     *(p_dst++) = b;
192                 }
193             }
194             break;
195         }
196         case 16:
197         {
198             int i;
199             uint8_t *p_src = p_surface->pixels;
200             uint8_t *p_dst = p_pic->p[0].p_pixels;
201             int i_pitch = p_pic->p[0].i_pitch < p_surface->pitch ?
202                 p_pic->p[0].i_pitch : p_surface->pitch;
203
204             for ( i = 0; i < p_surface->h; i++ )
205             {
206                 p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_pitch );
207                 p_src += p_surface->pitch;
208                 p_dst += p_pic->p[0].i_pitch;
209             }
210             break;
211         }
212         case 24:
213         {
214             int i, j;
215             uint8_t *p_src, *p_dst;
216             uint8_t r, g, b;
217             for ( i = 0; i < p_surface->h; i++ )
218             {
219                 p_src = p_surface->pixels + i * p_surface->pitch;
220                 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
221                 for ( j = 0; j < p_surface->w; j++ )
222                 {
223                     SDL_GetRGB( *(uint32_t*)p_src, p_surface->format,
224                                 &r, &g, &b );
225                     *(p_dst++) = r;
226                     *(p_dst++) = g;
227                     *(p_dst++) = b;
228                     p_src += 3;
229                 }
230             }
231             break;
232         }
233         case 32:
234         {
235             int i, j;
236             uint8_t *p_src, *p_dst;
237             uint8_t r, g, b, a;
238             for ( i = 0; i < p_surface->h; i++ )
239             {
240                 p_src = p_surface->pixels + i * p_surface->pitch;
241                 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
242                 for ( j = 0; j < p_surface->w; j++ )
243                 {
244                     SDL_GetRGBA( *(uint32_t*)p_src, p_surface->format,
245                                 &r, &g, &b, &a );
246                     *(p_dst++) = b;
247                     *(p_dst++) = g;
248                     *(p_dst++) = r;
249                     *(p_dst++) = a;
250                     p_src += 4;
251                 }
252             }
253             break;
254         }
255     }
256
257     SDL_FreeSurface( p_surface );
258     block_Release( p_block ); *pp_block = NULL;
259     return p_pic;
260
261 error:
262     if ( p_surface != NULL ) SDL_FreeSurface( p_surface );
263     block_Release( p_block ); *pp_block = NULL;
264     return NULL;
265 }
266
267 /*****************************************************************************
268  * CloseDecoder: sdl decoder destruction
269  *****************************************************************************/
270 static void CloseDecoder( vlc_object_t *p_this )
271 {
272     decoder_t *p_dec = (decoder_t *)p_this;
273     decoder_sys_t *p_sys = p_dec->p_sys;
274
275     free( p_sys );
276 }