1 /*****************************************************************************
2 * png.c: png decoder module making use of libpng.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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 *****************************************************************************/
32 #include <vlc_codec.h>
36 /*****************************************************************************
37 * decoder_sys_t : png decoder descriptor
38 *****************************************************************************/
44 /*****************************************************************************
46 *****************************************************************************/
47 static int OpenDecoder ( vlc_object_t * );
48 static void CloseDecoder ( vlc_object_t * );
50 static picture_t *DecodeBlock ( decoder_t *, block_t ** );
52 /*****************************************************************************
54 *****************************************************************************/
56 set_category( CAT_INPUT );
57 set_subcategory( SUBCAT_INPUT_VCODEC );
58 set_description( _("PNG video decoder") );
59 set_capability( "decoder", 1000 );
60 set_callbacks( OpenDecoder, CloseDecoder );
61 add_shortcut( "png" );
64 /*****************************************************************************
65 * OpenDecoder: probe the decoder and return score
66 *****************************************************************************/
67 static int OpenDecoder( vlc_object_t *p_this )
69 decoder_t *p_dec = (decoder_t*)p_this;
72 if( p_dec->fmt_in.i_codec != VLC_FOURCC('p','n','g',' ') &&
73 p_dec->fmt_in.i_codec != VLC_FOURCC('M','P','N','G') )
78 /* Allocate the memory needed to store the decoder's structure */
79 if( ( p_dec->p_sys = p_sys =
80 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
82 msg_Err( p_dec, "out of memory" );
86 /* Set output properties */
87 p_dec->fmt_out.i_cat = VIDEO_ES;
88 p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
91 p_dec->pf_decode_video = DecodeBlock;
96 static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
98 block_t *p_block = (block_t *)png_get_io_ptr( p_png );
99 png_size_t i_read = __MIN( p_block->i_buffer, i_length );
100 memcpy( data, p_block->p_buffer, i_length );
101 p_block->p_buffer += i_length;
102 p_block->i_buffer -= i_length;
104 if( i_length != i_read ) png_error( p_png, "not enough data" );
107 static void user_error( png_structp p_png, png_const_charp error_msg )
109 decoder_t *p_dec = (decoder_t *)png_get_error_ptr( p_png );
110 p_dec->p_sys->b_error = true;
111 msg_Err( p_dec, error_msg );
114 static void user_warning( png_structp p_png, png_const_charp warning_msg )
116 decoder_t *p_dec = (decoder_t *)png_get_error_ptr( p_png );
117 msg_Warn( p_dec, warning_msg );
120 /****************************************************************************
121 * DecodeBlock: the whole thing
122 ****************************************************************************
123 * This function must be fed with a complete compressed frame.
124 ****************************************************************************/
125 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
127 decoder_sys_t *p_sys = p_dec->p_sys;
129 picture_t *p_pic = 0;
131 png_uint_32 i_width, i_height;
132 int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
136 png_infop p_info, p_end_info;
137 png_bytep *p_row_pointers = NULL;
139 if( !pp_block || !*pp_block ) return NULL;
142 p_sys->b_error = false;
144 p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
147 block_Release( p_block ); *pp_block = NULL;
151 p_info = png_create_info_struct( p_png );
154 png_destroy_read_struct( &p_png, png_infopp_NULL, png_infopp_NULL );
155 block_Release( p_block ); *pp_block = NULL;
159 p_end_info = png_create_info_struct( p_png );
160 if( p_end_info == NULL )
162 png_destroy_read_struct( &p_png, &p_info, png_infopp_NULL );
163 block_Release( p_block ); *pp_block = NULL;
167 /* libpng longjmp's there in case of error */
168 if( setjmp( png_jmpbuf( p_png ) ) )
171 png_set_read_fn( p_png, (void *)p_block, user_read );
172 png_set_error_fn( p_png, (void *)p_dec, user_error, user_warning );
174 png_read_info( p_png, p_info );
175 if( p_sys->b_error ) goto error;
177 png_get_IHDR( p_png, p_info, &i_width, &i_height,
178 &i_bit_depth, &i_color_type, &i_interlace_type,
179 &i_compression_type, &i_filter_type);
180 if( p_sys->b_error ) goto error;
182 /* Set output properties */
183 p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
184 p_dec->fmt_out.video.i_width = i_width;
185 p_dec->fmt_out.video.i_height = i_height;
186 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
188 if( i_color_type == PNG_COLOR_TYPE_PALETTE )
189 png_set_palette_to_rgb( p_png );
191 if( i_color_type == PNG_COLOR_TYPE_GRAY ||
192 i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
193 png_set_gray_to_rgb( p_png );
195 /* Strip to 8 bits per channel */
196 if( i_bit_depth == 16 ) png_set_strip_16( p_png );
198 if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
200 png_set_tRNS_to_alpha( p_png );
202 else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
204 p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','2','4');
206 if( i_color_type & PNG_COLOR_MASK_COLOR &&
207 p_dec->fmt_out.i_codec != VLC_FOURCC('R','V','2','4') )
210 png_set_bgr( p_png );
213 /* Get a new picture */
214 p_pic = p_dec->pf_vout_buffer_new( p_dec );
215 if( !p_pic ) goto error;
218 p_row_pointers = malloc( sizeof(png_bytep) * i_height );
219 for( i = 0; i < (int)i_height; i++ )
220 p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
222 png_read_image( p_png, p_row_pointers );
223 if( p_sys->b_error ) goto error;
224 png_read_end( p_png, p_end_info );
225 if( p_sys->b_error ) goto error;
227 png_destroy_read_struct( &p_png, &p_info, &p_end_info );
228 free( p_row_pointers );
230 p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
232 block_Release( p_block ); *pp_block = NULL;
237 free( p_row_pointers );
238 png_destroy_read_struct( &p_png, &p_info, &p_end_info );
239 block_Release( p_block ); *pp_block = NULL;
243 /*****************************************************************************
244 * CloseDecoder: png decoder destruction
245 *****************************************************************************/
246 static void CloseDecoder( vlc_object_t *p_this )
248 decoder_t *p_dec = (decoder_t *)p_this;
249 decoder_sys_t *p_sys = p_dec->p_sys;