1 /*****************************************************************************
2 * sdl_image.c: sdl decoder module making use of libsdl_image.
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
35 #include SDL_IMAGE_INCLUDE_FILE
37 /*****************************************************************************
38 * decoder_sys_t : sdl decoder descriptor
39 *****************************************************************************/
42 const char *psz_sdl_type;
45 /*****************************************************************************
47 *****************************************************************************/
48 static int OpenDecoder ( vlc_object_t * );
49 static void CloseDecoder ( vlc_object_t * );
51 static picture_t *DecodeBlock ( decoder_t *, block_t ** );
53 /*****************************************************************************
55 *****************************************************************************/
57 set_category( CAT_INPUT )
58 set_subcategory( SUBCAT_INPUT_VCODEC )
59 set_shortname( N_("SDL Image decoder"))
60 set_description( N_("SDL_image video decoder") )
61 set_capability( "decoder", 60 )
62 set_callbacks( OpenDecoder, CloseDecoder )
63 add_shortcut( "sdl_image" )
66 static const struct supported_fmt_t
68 vlc_fourcc_t i_fourcc;
69 const char *psz_sdl_type;
72 { VLC_CODEC_TARGA, "TGA" },
73 { VLC_CODEC_BMP, "BMP" },
74 { VLC_CODEC_PNM, "PNM" },
75 { VLC_FOURCC('x','p','m',' '), "XPM" },
76 { VLC_FOURCC('x','c','f',' '), "XCF" },
77 { VLC_CODEC_PCX, "PCX" },
78 { VLC_CODEC_GIF, "GIF" },
79 { VLC_CODEC_JPEG, "JPG" },
80 { VLC_CODEC_TIFF, "TIF" },
81 { VLC_FOURCC('l','b','m',' '), "LBM" },
82 { VLC_CODEC_PNG, "PNG" }
85 /*****************************************************************************
86 * OpenDecoder: probe the decoder and return score
87 *****************************************************************************/
88 static int OpenDecoder( vlc_object_t *p_this )
90 decoder_t *p_dec = (decoder_t *)p_this;
96 i < (int)(sizeof(p_supported_fmt)/sizeof(struct supported_fmt_t));
99 if ( p_supported_fmt[i].i_fourcc == p_dec->fmt_in.i_codec )
102 if ( i == (int)(sizeof(p_supported_fmt)/sizeof(struct supported_fmt_t)) )
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 )
111 p_sys->psz_sdl_type = p_supported_fmt[i].psz_sdl_type;
113 /* Set output properties - this is a decoy and isn't used anywhere */
114 p_dec->fmt_out.i_cat = VIDEO_ES;
115 p_dec->fmt_out.i_codec = VLC_CODEC_RGB32;
118 p_dec->pf_decode_video = DecodeBlock;
123 /****************************************************************************
124 * DecodeBlock: the whole thing
125 ****************************************************************************
126 * This function must be fed with a complete compressed frame.
127 ****************************************************************************/
128 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
130 decoder_sys_t *p_sys = p_dec->p_sys;
132 picture_t *p_pic = NULL;
133 SDL_Surface *p_surface;
136 if( pp_block == NULL || *pp_block == NULL ) return NULL;
139 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
141 block_Release( p_block ); *pp_block = NULL;
145 p_rw = SDL_RWFromConstMem( p_block->p_buffer, p_block->i_buffer );
147 /* Decode picture. */
148 p_surface = IMG_LoadTyped_RW( p_rw, 1, (char*)p_sys->psz_sdl_type );
149 if ( p_surface == NULL )
151 msg_Warn( p_dec, "SDL_image couldn't load the image (%s)",
156 switch ( p_surface->format->BitsPerPixel )
159 p_dec->fmt_out.i_codec = VLC_CODEC_RGB16;
163 p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
166 p_dec->fmt_out.i_codec = VLC_CODEC_RGB32;
169 msg_Warn( p_dec, "unknown bits/pixel format (%d)",
170 p_surface->format->BitsPerPixel );
173 p_dec->fmt_out.video.i_width = p_surface->w;
174 p_dec->fmt_out.video.i_height = p_surface->h;
175 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_surface->w
178 /* Get a new picture. */
179 p_pic = decoder_NewPicture( p_dec );
180 if ( p_pic == NULL ) goto error;
182 switch ( p_surface->format->BitsPerPixel )
187 uint8_t *p_src, *p_dst;
189 for ( i = 0; i < p_surface->h; i++ )
191 p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch;
192 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
193 for ( j = 0; j < p_surface->w; j++ )
195 SDL_GetRGB( *(p_src++), p_surface->format,
207 uint8_t *p_src = p_surface->pixels;
208 uint8_t *p_dst = p_pic->p[0].p_pixels;
209 int i_pitch = p_pic->p[0].i_pitch < p_surface->pitch ?
210 p_pic->p[0].i_pitch : p_surface->pitch;
212 for ( i = 0; i < p_surface->h; i++ )
214 vlc_memcpy( p_dst, p_src, i_pitch );
215 p_src += p_surface->pitch;
216 p_dst += p_pic->p[0].i_pitch;
223 uint8_t *p_src, *p_dst;
225 for ( i = 0; i < p_surface->h; i++ )
227 p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch;
228 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
229 for ( j = 0; j < p_surface->w; j++ )
231 SDL_GetRGB( *(uint32_t*)p_src, p_surface->format,
244 uint8_t *p_src, *p_dst;
246 for ( i = 0; i < p_surface->h; i++ )
248 p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch;
249 p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch;
250 for ( j = 0; j < p_surface->w; j++ )
252 SDL_GetRGBA( *(uint32_t*)p_src, p_surface->format,
265 p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
267 SDL_FreeSurface( p_surface );
268 block_Release( p_block ); *pp_block = NULL;
272 if ( p_surface != NULL ) SDL_FreeSurface( p_surface );
273 block_Release( p_block ); *pp_block = NULL;
277 /*****************************************************************************
278 * CloseDecoder: sdl decoder destruction
279 *****************************************************************************/
280 static void CloseDecoder( vlc_object_t *p_this )
282 decoder_t *p_dec = (decoder_t *)p_this;
283 decoder_sys_t *p_sys = p_dec->p_sys;