Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

png.c

00001 /*****************************************************************************
00002  * png.c: png decoder module making use of libpng.
00003  *****************************************************************************
00004  * Copyright (C) 1999-2001 the VideoLAN team
00005  * $Id: png.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <vlc/vlc.h>
00028 #include <vlc/decoder.h>
00029 
00030 #include <png.h>
00031 
00032 /*****************************************************************************
00033  * decoder_sys_t : png decoder descriptor
00034  *****************************************************************************/
00035 struct decoder_sys_t
00036 {
00037     vlc_bool_t b_error;
00038 };
00039 
00040 /*****************************************************************************
00041  * Local prototypes
00042  *****************************************************************************/
00043 static int  OpenDecoder   ( vlc_object_t * );
00044 static void CloseDecoder  ( vlc_object_t * );
00045 
00046 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
00047 
00048 /*****************************************************************************
00049  * Module descriptor
00050  *****************************************************************************/
00051 vlc_module_begin();
00052     set_category( CAT_INPUT );
00053     set_subcategory( SUBCAT_INPUT_VCODEC );
00054     set_description( _("PNG video decoder") );
00055     set_capability( "decoder", 1000 );
00056     set_callbacks( OpenDecoder, CloseDecoder );
00057     add_shortcut( "png" );
00058 vlc_module_end();
00059 
00060 /*****************************************************************************
00061  * OpenDecoder: probe the decoder and return score
00062  *****************************************************************************/
00063 static int OpenDecoder( vlc_object_t *p_this )
00064 {
00065     decoder_t *p_dec = (decoder_t*)p_this;
00066     decoder_sys_t *p_sys;
00067 
00068     if( p_dec->fmt_in.i_codec != VLC_FOURCC('p','n','g',' ') )
00069     {
00070         return VLC_EGENERIC;
00071     }
00072 
00073     /* Allocate the memory needed to store the decoder's structure */
00074     if( ( p_dec->p_sys = p_sys =
00075           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
00076     {
00077         msg_Err( p_dec, "out of memory" );
00078         return VLC_EGENERIC;
00079     }
00080 
00081     /* Set output properties */
00082     p_dec->fmt_out.i_cat = VIDEO_ES;
00083     p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
00084 
00085     /* Set callbacks */
00086     p_dec->pf_decode_video = DecodeBlock;
00087 
00088     return VLC_SUCCESS;
00089 }
00090 
00091 static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
00092 {
00093     block_t *p_block = (block_t *)png_get_io_ptr( p_png );
00094     png_size_t i_read = __MIN( p_block->i_buffer, (int)i_length );
00095     memcpy( data, p_block->p_buffer, i_length );
00096     p_block->p_buffer += i_length;
00097     p_block->i_buffer -= i_length;
00098 
00099     if( i_length != i_read ) png_error( p_png, "not enough data" );
00100 }
00101 
00102 static void user_error( png_structp p_png, png_const_charp error_msg )
00103 {
00104     decoder_t *p_dec = (decoder_t *)png_get_error_ptr( p_png );
00105     p_dec->p_sys->b_error = VLC_TRUE;
00106     msg_Err( p_dec, error_msg );
00107 }
00108 
00109 static void user_warning( png_structp p_png, png_const_charp warning_msg )
00110 {
00111     decoder_t *p_dec = (decoder_t *)png_get_error_ptr( p_png );
00112     msg_Warn( p_dec, warning_msg );
00113 }
00114 
00115 /****************************************************************************
00116  * DecodeBlock: the whole thing
00117  ****************************************************************************
00118  * This function must be fed with a complete compressed frame.
00119  ****************************************************************************/
00120 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
00121 {
00122     decoder_sys_t *p_sys = p_dec->p_sys;
00123     block_t *p_block;
00124     picture_t *p_pic = 0;
00125 
00126     png_uint_32 i_width, i_height;
00127     int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
00128     int i_bit_depth, i;
00129 
00130     png_structp p_png;
00131     png_infop p_info, p_end_info;
00132     png_bytep *p_row_pointers = NULL;
00133 
00134     if( !pp_block || !*pp_block ) return NULL;
00135 
00136     p_block = *pp_block;
00137     p_sys->b_error = VLC_FALSE;
00138 
00139     p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
00140     p_info = png_create_info_struct( p_png );
00141     p_end_info = png_create_info_struct( p_png );
00142  
00143     png_set_read_fn( p_png, (void *)p_block, user_read );
00144     png_set_error_fn( p_png, (void *)p_dec, user_error, user_warning );
00145 
00146     png_read_info( p_png, p_info );
00147     if( p_sys->b_error ) goto error;
00148 
00149     png_get_IHDR( p_png, p_info, &i_width, &i_height,
00150                   &i_bit_depth, &i_color_type, &i_interlace_type,
00151                   &i_compression_type, &i_filter_type);
00152     if( p_sys->b_error ) goto error;
00153 
00154     /* Set output properties */
00155     p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
00156     p_dec->fmt_out.video.i_width = i_width;
00157     p_dec->fmt_out.video.i_height = i_height;
00158     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
00159 
00160     if( i_color_type == PNG_COLOR_TYPE_PALETTE )
00161         png_set_palette_to_rgb( p_png );
00162 
00163     if( i_color_type == PNG_COLOR_TYPE_GRAY ||
00164         i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
00165           png_set_gray_to_rgb( p_png );
00166 
00167     /* Strip to 8 bits per channel */
00168     if( i_bit_depth == 16 ) png_set_strip_16( p_png );
00169 
00170     if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
00171     {
00172         png_set_tRNS_to_alpha( p_png );
00173     }
00174     else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
00175     {
00176         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','2','4');
00177     }
00178     if( i_color_type & PNG_COLOR_MASK_COLOR &&
00179         p_dec->fmt_out.i_codec != VLC_FOURCC('R','V','2','4') )
00180     {
00181         /* Invert colors */
00182         png_set_bgr( p_png );
00183     }
00184 
00185     /* Get a new picture */
00186     p_pic = p_dec->pf_vout_buffer_new( p_dec );
00187     if( !p_pic ) goto error;
00188 
00189     /* Decode picture */
00190     p_row_pointers = malloc( sizeof(png_bytep) * i_height );
00191     for( i = 0; i < (int)i_height; i++ )
00192         p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
00193 
00194     png_read_image( p_png, p_row_pointers );
00195     if( p_sys->b_error ) goto error;
00196     png_read_end( p_png, p_end_info );
00197     if( p_sys->b_error ) goto error;
00198 
00199     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
00200     free( p_row_pointers );
00201 
00202     block_Release( p_block ); *pp_block = NULL;
00203     return p_pic;
00204 
00205  error:
00206 
00207     if( p_row_pointers ) free( p_row_pointers );
00208     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
00209     block_Release( p_block ); *pp_block = NULL;
00210     return NULL;
00211 }
00212 
00213 /*****************************************************************************
00214  * CloseDecoder: png decoder destruction
00215  *****************************************************************************/
00216 static void CloseDecoder( vlc_object_t *p_this )
00217 {
00218     decoder_t *p_dec = (decoder_t *)p_this;
00219     decoder_sys_t *p_sys = p_dec->p_sys;
00220 
00221     free( p_sys );
00222 }

Generated on Tue Dec 20 10:14:29 2005 for vlc-0.8.4a by  doxygen 1.4.2