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