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