]> git.sesse.net Git - vlc/blob - modules/codec/png.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / codec / png.c
1 /*****************************************************************************
2  * png.c: png decoder module making use of libpng.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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 #include <png.h>
31
32 /*****************************************************************************
33  * decoder_sys_t : png decoder descriptor
34  *****************************************************************************/
35 struct decoder_sys_t
36 {
37     vlc_bool_t b_error;
38 };
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  OpenDecoder   ( vlc_object_t * );
44 static void CloseDecoder  ( vlc_object_t * );
45
46 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 vlc_module_begin();
52     set_category( CAT_INPUT );
53     set_subcategory( SUBCAT_INPUT_VCODEC );
54     set_description( _("PNG video decoder") );
55     set_capability( "decoder", 1000 );
56     set_callbacks( OpenDecoder, CloseDecoder );
57     add_shortcut( "png" );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * OpenDecoder: probe the decoder and return score
62  *****************************************************************************/
63 static int OpenDecoder( vlc_object_t *p_this )
64 {
65     decoder_t *p_dec = (decoder_t*)p_this;
66     decoder_sys_t *p_sys;
67
68     if( p_dec->fmt_in.i_codec != VLC_FOURCC('p','n','g',' ') &&
69         p_dec->fmt_in.i_codec != VLC_FOURCC('M','P','N','G') )
70     {
71         return VLC_EGENERIC;
72     }
73
74     /* Allocate the memory needed to store the decoder's structure */
75     if( ( p_dec->p_sys = p_sys =
76           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
77     {
78         msg_Err( p_dec, "out of memory" );
79         return VLC_EGENERIC;
80     }
81
82     /* Set output properties */
83     p_dec->fmt_out.i_cat = VIDEO_ES;
84     p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
85
86     /* Set callbacks */
87     p_dec->pf_decode_video = DecodeBlock;
88
89     return VLC_SUCCESS;
90 }
91
92 static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
93 {
94     block_t *p_block = (block_t *)png_get_io_ptr( p_png );
95     png_size_t i_read = __MIN( p_block->i_buffer, (int)i_length );
96     memcpy( data, p_block->p_buffer, i_length );
97     p_block->p_buffer += i_length;
98     p_block->i_buffer -= i_length;
99
100     if( i_length != i_read ) png_error( p_png, "not enough data" );
101 }
102
103 static void user_error( png_structp p_png, png_const_charp error_msg )
104 {
105     decoder_t *p_dec = (decoder_t *)png_get_error_ptr( p_png );
106     p_dec->p_sys->b_error = VLC_TRUE;
107     msg_Err( p_dec, error_msg );
108 }
109
110 static void user_warning( png_structp p_png, png_const_charp warning_msg )
111 {
112     decoder_t *p_dec = (decoder_t *)png_get_error_ptr( p_png );
113     msg_Warn( p_dec, warning_msg );
114 }
115
116 /****************************************************************************
117  * DecodeBlock: the whole thing
118  ****************************************************************************
119  * This function must be fed with a complete compressed frame.
120  ****************************************************************************/
121 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
122 {
123     decoder_sys_t *p_sys = p_dec->p_sys;
124     block_t *p_block;
125     picture_t *p_pic = 0;
126
127     png_uint_32 i_width, i_height;
128     int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
129     int i_bit_depth, i;
130
131     png_structp p_png;
132     png_infop p_info, p_end_info;
133     png_bytep *p_row_pointers = NULL;
134
135     if( !pp_block || !*pp_block ) return NULL;
136
137     p_block = *pp_block;
138     p_sys->b_error = VLC_FALSE;
139
140     p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
141     if( p_png == NULL )
142     {
143         block_Release( p_block ); *pp_block = NULL;
144         return NULL;
145     }
146  
147     p_info = png_create_info_struct( p_png );
148     if( p_info == NULL )
149     {
150         png_destroy_read_struct( &p_png, png_infopp_NULL, png_infopp_NULL );
151         block_Release( p_block ); *pp_block = NULL;
152         return NULL;
153     }
154
155     p_end_info = png_create_info_struct( p_png );
156     if( p_end_info == NULL )
157     {
158         png_destroy_read_struct( &p_png, &p_info, png_infopp_NULL );
159         block_Release( p_block ); *pp_block = NULL;
160         return NULL;
161     }
162  
163     /* libpng longjmp's there in case of error */
164     if( setjmp( png_jmpbuf( p_png ) ) )
165         goto error;
166
167     png_set_read_fn( p_png, (void *)p_block, user_read );
168     png_set_error_fn( p_png, (void *)p_dec, user_error, user_warning );
169
170     png_read_info( p_png, p_info );
171     if( p_sys->b_error ) goto error;
172
173     png_get_IHDR( p_png, p_info, &i_width, &i_height,
174                   &i_bit_depth, &i_color_type, &i_interlace_type,
175                   &i_compression_type, &i_filter_type);
176     if( p_sys->b_error ) goto error;
177
178     /* Set output properties */
179     p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
180     p_dec->fmt_out.video.i_width = i_width;
181     p_dec->fmt_out.video.i_height = i_height;
182     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
183
184     if( i_color_type == PNG_COLOR_TYPE_PALETTE )
185         png_set_palette_to_rgb( p_png );
186
187     if( i_color_type == PNG_COLOR_TYPE_GRAY ||
188         i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
189           png_set_gray_to_rgb( p_png );
190
191     /* Strip to 8 bits per channel */
192     if( i_bit_depth == 16 ) png_set_strip_16( p_png );
193
194     if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
195     {
196         png_set_tRNS_to_alpha( p_png );
197     }
198     else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
199     {
200         p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','2','4');
201     }
202     if( i_color_type & PNG_COLOR_MASK_COLOR &&
203         p_dec->fmt_out.i_codec != VLC_FOURCC('R','V','2','4') )
204     {
205         /* Invert colors */
206         png_set_bgr( p_png );
207     }
208
209     /* Get a new picture */
210     p_pic = p_dec->pf_vout_buffer_new( p_dec );
211     if( !p_pic ) goto error;
212
213     /* Decode picture */
214     p_row_pointers = malloc( sizeof(png_bytep) * i_height );
215     for( i = 0; i < (int)i_height; i++ )
216         p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
217
218     png_read_image( p_png, p_row_pointers );
219     if( p_sys->b_error ) goto error;
220     png_read_end( p_png, p_end_info );
221     if( p_sys->b_error ) goto error;
222
223     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
224     free( p_row_pointers );
225
226     p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
227
228     block_Release( p_block ); *pp_block = NULL;
229     return p_pic;
230
231  error:
232
233     if( p_row_pointers ) free( p_row_pointers );
234     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
235     block_Release( p_block ); *pp_block = NULL;
236     return NULL;
237 }
238
239 /*****************************************************************************
240  * CloseDecoder: png decoder destruction
241  *****************************************************************************/
242 static void CloseDecoder( vlc_object_t *p_this )
243 {
244     decoder_t *p_dec = (decoder_t *)p_this;
245     decoder_sys_t *p_sys = p_dec->p_sys;
246
247     free( p_sys );
248 }