1 /*****************************************************************************
2 * png.c: png decoder module making use of libpng.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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>
36 /* PNG_SYS_COMMON_MEMBERS:
37 * members common to encoder and decoder descriptors
39 #define PNG_SYS_COMMON_MEMBERS \
42 vlc_object_t *p_obj; \
46 * png common descriptor
50 PNG_SYS_COMMON_MEMBERS
53 typedef struct png_sys_t png_sys_t;
55 /*****************************************************************************
56 * decoder_sys_t : png decoder descriptor
57 *****************************************************************************/
60 PNG_SYS_COMMON_MEMBERS
63 /*****************************************************************************
65 *****************************************************************************/
66 static int OpenDecoder ( vlc_object_t * );
67 static void CloseDecoder ( vlc_object_t * );
69 static picture_t *DecodeBlock ( decoder_t *, block_t ** );
72 * png encoder descriptor
76 PNG_SYS_COMMON_MEMBERS
80 static int OpenEncoder(vlc_object_t *);
81 static void CloseEncoder(vlc_object_t *);
83 static block_t *EncodeBlock(encoder_t *, picture_t *);
85 /*****************************************************************************
87 *****************************************************************************/
89 set_category( CAT_INPUT )
90 set_subcategory( SUBCAT_INPUT_VCODEC )
91 set_description( N_("PNG video decoder") )
92 set_capability( "decoder", 1000 )
93 set_callbacks( OpenDecoder, CloseDecoder )
96 /* encoder submodule */
99 set_section(N_("Encoding"), NULL)
100 set_description(N_("PNG video encoder"))
101 set_capability("encoder", 1000)
102 set_callbacks(OpenEncoder, CloseEncoder)
105 /*****************************************************************************
106 * OpenDecoder: probe the decoder and return score
107 *****************************************************************************/
108 static int OpenDecoder( vlc_object_t *p_this )
110 decoder_t *p_dec = (decoder_t*)p_this;
112 if( p_dec->fmt_in.i_codec != VLC_CODEC_PNG &&
113 p_dec->fmt_in.i_codec != VLC_FOURCC('M','P','N','G') )
118 /* Allocate the memory needed to store the decoder's structure */
119 p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
120 if( p_dec->p_sys == NULL )
123 p_dec->p_sys->p_obj = p_this;
125 /* Set output properties */
126 p_dec->fmt_out.i_cat = VIDEO_ES;
127 p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
130 p_dec->pf_decode_video = DecodeBlock;
135 static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
137 block_t *p_block = (block_t *)png_get_io_ptr( p_png );
138 if( i_length > p_block->i_buffer ) {
139 png_error( p_png, "not enough data" );
143 memcpy( data, p_block->p_buffer, i_length );
144 p_block->p_buffer += i_length;
145 p_block->i_buffer -= i_length;
148 static void user_flush( png_structp p_png )
154 static void user_write( png_structp p_png, png_bytep data, png_size_t i_length )
156 block_t *p_block = (block_t *)png_get_io_ptr( p_png );
157 if( i_length > p_block->i_buffer ) {
159 snprintf( err_str, sizeof(err_str),
160 "block size %zu too small for %zu encoded bytes",
161 p_block->i_buffer, i_length );
162 png_error( p_png, err_str );
166 memcpy( p_block->p_buffer, data, i_length );
167 p_block->p_buffer += i_length;
168 p_block->i_buffer -= i_length;
171 static void user_error( png_structp p_png, png_const_charp error_msg )
173 png_sys_t *p_sys = (png_sys_t *)png_get_error_ptr( p_png );
174 p_sys->b_error = true;
175 msg_Err( p_sys->p_obj, "%s", error_msg );
178 static void user_warning( png_structp p_png, png_const_charp warning_msg )
180 png_sys_t *p_sys = (png_sys_t *)png_get_error_ptr( p_png );
181 msg_Warn( p_sys->p_obj, "%s", warning_msg );
184 /****************************************************************************
185 * DecodeBlock: the whole thing
186 ****************************************************************************
187 * This function must be fed with a complete compressed frame.
188 ****************************************************************************/
189 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
191 decoder_sys_t *p_sys = p_dec->p_sys;
193 picture_t *p_pic = 0;
195 png_uint_32 i_width, i_height;
196 int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
200 png_infop p_info, p_end_info;
201 png_bytep *p_row_pointers = NULL;
203 if( !pp_block || !*pp_block ) return NULL;
206 p_sys->b_error = false;
208 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
210 block_Release( p_block ); *pp_block = NULL;
214 p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
217 block_Release( p_block ); *pp_block = NULL;
221 p_info = png_create_info_struct( p_png );
224 png_destroy_read_struct( &p_png, NULL, NULL );
225 block_Release( p_block ); *pp_block = NULL;
229 p_end_info = png_create_info_struct( p_png );
230 if( p_end_info == NULL )
232 png_destroy_read_struct( &p_png, &p_info, NULL );
233 block_Release( p_block ); *pp_block = NULL;
237 /* libpng longjmp's there in case of error */
238 if( setjmp( png_jmpbuf( p_png ) ) )
241 png_set_read_fn( p_png, (void *)p_block, user_read );
242 png_set_error_fn( p_png, (void *)p_dec, user_error, user_warning );
244 png_read_info( p_png, p_info );
245 if( p_sys->b_error ) goto error;
247 png_get_IHDR( p_png, p_info, &i_width, &i_height,
248 &i_bit_depth, &i_color_type, &i_interlace_type,
249 &i_compression_type, &i_filter_type);
250 if( p_sys->b_error ) goto error;
252 /* Set output properties */
253 p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
254 p_dec->fmt_out.video.i_width = i_width;
255 p_dec->fmt_out.video.i_height = i_height;
256 p_dec->fmt_out.video.i_sar_num = 1;
257 p_dec->fmt_out.video.i_sar_den = 1;
258 p_dec->fmt_out.video.i_rmask = 0x000000ff;
259 p_dec->fmt_out.video.i_gmask = 0x0000ff00;
260 p_dec->fmt_out.video.i_bmask = 0x00ff0000;
262 if( i_color_type == PNG_COLOR_TYPE_PALETTE )
263 png_set_palette_to_rgb( p_png );
265 if( i_color_type == PNG_COLOR_TYPE_GRAY ||
266 i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
267 png_set_gray_to_rgb( p_png );
269 /* Strip to 8 bits per channel */
270 if( i_bit_depth == 16 ) png_set_strip_16( p_png );
272 if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
274 png_set_tRNS_to_alpha( p_png );
276 else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
278 p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
281 /* Get a new picture */
282 p_pic = decoder_NewPicture( p_dec );
283 if( !p_pic ) goto error;
286 p_row_pointers = malloc( sizeof(png_bytep) * i_height );
287 if( !p_row_pointers )
289 for( i = 0; i < (int)i_height; i++ )
290 p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
292 png_read_image( p_png, p_row_pointers );
293 if( p_sys->b_error ) goto error;
294 png_read_end( p_png, p_end_info );
295 if( p_sys->b_error ) goto error;
297 png_destroy_read_struct( &p_png, &p_info, &p_end_info );
298 free( p_row_pointers );
300 p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
302 block_Release( p_block ); *pp_block = NULL;
307 free( p_row_pointers );
308 png_destroy_read_struct( &p_png, &p_info, &p_end_info );
309 block_Release( p_block ); *pp_block = NULL;
313 /*****************************************************************************
314 * CloseDecoder: png decoder destruction
315 *****************************************************************************/
316 static void CloseDecoder( vlc_object_t *p_this )
318 decoder_t *p_dec = (decoder_t *)p_this;
319 decoder_sys_t *p_sys = p_dec->p_sys;
324 static int OpenEncoder(vlc_object_t *p_this)
326 encoder_t *p_enc = (encoder_t *) p_this;
328 if( p_enc->fmt_out.i_codec != VLC_CODEC_PNG )
331 /* Allocate the memory needed to store the encoder's structure */
332 p_enc->p_sys = malloc( sizeof(encoder_sys_t) );
333 if( p_enc->p_sys == NULL )
336 p_enc->p_sys->p_obj = p_this;
338 p_enc->p_sys->i_blocksize = 3 * p_enc->fmt_in.video.i_visible_width *
339 p_enc->fmt_in.video.i_visible_height;
341 p_enc->fmt_in.i_codec = VLC_CODEC_RGB24;
342 p_enc->fmt_in.video.i_rmask = 0x000000ff;
343 p_enc->fmt_in.video.i_gmask = 0x0000ff00;
344 p_enc->fmt_in.video.i_bmask = 0x00ff0000;
345 p_enc->pf_encode_video = EncodeBlock;
353 static block_t *EncodeBlock(encoder_t *p_enc, picture_t *p_pic)
355 encoder_sys_t *p_sys = p_enc->p_sys;
357 if( unlikely( !p_pic ) )
362 block_t *p_block = block_Alloc( p_sys->i_blocksize );
363 if( p_block == NULL )
368 png_structp p_png = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
371 block_Release( p_block );
375 /* save buffer start */
376 uint8_t *p_start = p_block->p_buffer;
377 size_t i_start = p_block->i_buffer;
379 p_sys->b_error = false;
380 png_infop p_info = NULL;
382 /* libpng longjmp's there in case of error */
383 if( setjmp( png_jmpbuf( p_png ) ) )
386 png_set_write_fn( p_png, p_block, user_write, user_flush );
387 png_set_error_fn( p_png, p_enc, user_error, user_warning );
389 p_info = png_create_info_struct( p_png );
393 png_infop p_end_info = png_create_info_struct( p_png );
394 if( p_end_info == NULL ) goto error;
396 const unsigned i_width = p_enc->fmt_in.video.i_visible_width;
397 const unsigned i_height = p_enc->fmt_in.video.i_visible_height;
399 png_set_IHDR( p_png, p_info,
402 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
403 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
404 if( p_sys->b_error ) goto error;
406 png_write_info( p_png, p_info );
407 if( p_sys->b_error ) goto error;
411 for( unsigned i = 0; i < i_height; i++ )
413 png_write_row( p_png, p_pic->p->p_pixels + (i_width * i * 3));
414 if( p_sys->b_error ) goto error;
417 png_write_end( p_png, p_end_info );
418 if( p_sys->b_error ) goto error;
420 png_destroy_write_struct( &p_png, &p_info );
422 /* restore original buffer position */
423 p_block->p_buffer = p_start;
424 p_block->i_buffer = i_start;
429 png_destroy_write_struct( &p_png, p_info ? &p_info : NULL );
431 block_Release(p_block);
435 /*****************************************************************************
436 * CloseEncoder: png encoder destruction
437 *****************************************************************************/
438 static void CloseEncoder( vlc_object_t *p_this )
440 encoder_t *p_enc = (encoder_t *)p_this;
441 encoder_sys_t *p_sys = p_enc->p_sys;