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