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