qml: remove context indicator
[vlc.git] / modules / codec / png.c
blob07ef26d346b4585477d289bcfec6101789e44bb6
1 /*****************************************************************************
2 * png.c: png decoder module making use of libpng.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
33 #include <png.h>
35 /* PNG_SYS_COMMON_MEMBERS:
36 * members common to encoder and decoder descriptors
38 #define PNG_SYS_COMMON_MEMBERS \
39 /**@{*/ \
40 bool b_error; \
41 vlc_object_t *p_obj; \
42 /**@}*/ \
45 * png common descriptor
47 struct png_sys_t
49 PNG_SYS_COMMON_MEMBERS
52 typedef struct png_sys_t png_sys_t;
54 /*****************************************************************************
55 * decoder_sys_t : png decoder descriptor
56 *****************************************************************************/
57 typedef struct
59 PNG_SYS_COMMON_MEMBERS
60 } decoder_sys_t;
62 /*****************************************************************************
63 * Local prototypes
64 *****************************************************************************/
65 static int OpenDecoder ( vlc_object_t * );
66 static void CloseDecoder ( vlc_object_t * );
68 static int DecodeBlock ( decoder_t *, block_t * );
71 * png encoder descriptor
73 typedef struct
75 PNG_SYS_COMMON_MEMBERS
76 int i_blocksize;
77 } encoder_sys_t;
79 static int OpenEncoder(vlc_object_t *);
80 static void CloseEncoder(vlc_object_t *);
82 static block_t *EncodeBlock(encoder_t *, picture_t *);
84 /*****************************************************************************
85 * Module descriptor
86 *****************************************************************************/
87 vlc_module_begin ()
88 set_category( CAT_INPUT )
89 set_subcategory( SUBCAT_INPUT_VCODEC )
90 set_description( N_("PNG video decoder") )
91 set_capability( "video decoder", 1000 )
92 set_callbacks( OpenDecoder, CloseDecoder )
93 add_shortcut( "png" )
95 /* encoder submodule */
96 add_submodule()
97 add_shortcut("png")
98 set_section(N_("Encoding"), NULL)
99 set_description(N_("PNG video encoder"))
100 set_capability("encoder", 1000)
101 set_callbacks(OpenEncoder, CloseEncoder)
102 vlc_module_end ()
104 /*****************************************************************************
105 * OpenDecoder: probe the decoder and return score
106 *****************************************************************************/
107 static int OpenDecoder( vlc_object_t *p_this )
109 decoder_t *p_dec = (decoder_t*)p_this;
111 if( p_dec->fmt_in.i_codec != VLC_CODEC_PNG &&
112 p_dec->fmt_in.i_codec != VLC_FOURCC('M','P','N','G') )
114 return VLC_EGENERIC;
117 /* Allocate the memory needed to store the decoder's structure */
118 decoder_sys_t *p_sys = malloc( sizeof(decoder_sys_t) );
119 if( p_sys == NULL )
120 return VLC_ENOMEM;
121 p_dec->p_sys = p_sys;
123 p_sys->p_obj = p_this;
125 /* Set output properties */
126 p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
127 p_dec->fmt_out.video.transfer = TRANSFER_FUNC_SRGB;
128 p_dec->fmt_out.video.space = COLOR_SPACE_SRGB;
129 p_dec->fmt_out.video.primaries = COLOR_PRIMARIES_SRGB;
130 p_dec->fmt_out.video.color_range = COLOR_RANGE_FULL;
132 /* Set callbacks */
133 p_dec->pf_decode = DecodeBlock;
135 return VLC_SUCCESS;
138 static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
140 block_t *p_block = (block_t *)png_get_io_ptr( p_png );
141 if( i_length > p_block->i_buffer ) {
142 png_error( p_png, "not enough data" );
143 return;
146 memcpy( data, p_block->p_buffer, i_length );
147 p_block->p_buffer += i_length;
148 p_block->i_buffer -= i_length;
151 static void user_flush( png_structp p_png )
153 /* noop */
154 (void) p_png;
157 static void user_write( png_structp p_png, png_bytep data, png_size_t i_length )
159 block_t *p_block = (block_t *)png_get_io_ptr( p_png );
160 if( i_length > p_block->i_buffer ) {
161 char err_str[64];
162 snprintf( err_str, sizeof(err_str),
163 "block size %zu too small for %zu encoded bytes",
164 p_block->i_buffer, i_length );
165 png_error( p_png, err_str );
166 return;
169 memcpy( p_block->p_buffer, data, i_length );
170 p_block->p_buffer += i_length;
171 p_block->i_buffer -= i_length;
174 static void user_error( png_structp p_png, png_const_charp error_msg )
176 png_sys_t *p_sys = (png_sys_t *)png_get_error_ptr( p_png );
177 p_sys->b_error = true;
178 msg_Err( p_sys->p_obj, "%s", error_msg );
181 static void user_warning( png_structp p_png, png_const_charp warning_msg )
183 png_sys_t *p_sys = (png_sys_t *)png_get_error_ptr( p_png );
184 msg_Warn( p_sys->p_obj, "%s", warning_msg );
187 /****************************************************************************
188 * DecodeBlock: the whole thing
189 ****************************************************************************
190 * This function must be fed with a complete compressed frame.
191 ****************************************************************************/
192 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
194 decoder_sys_t *p_sys = p_dec->p_sys;
195 picture_t *p_pic = 0;
197 png_uint_32 i_width, i_height;
198 int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
199 int i_bit_depth, i;
201 png_structp p_png;
202 png_infop p_info, p_end_info;
203 png_bytep *volatile p_row_pointers = NULL;
205 if( !p_block ) /* No Drain */
206 return VLCDEC_SUCCESS;
208 p_sys->b_error = false;
210 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
212 block_Release( p_block );
213 return VLCDEC_SUCCESS;
216 p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
217 if( p_png == NULL )
219 block_Release( p_block );
220 return VLCDEC_SUCCESS;
223 p_info = png_create_info_struct( p_png );
224 if( p_info == NULL )
226 png_destroy_read_struct( &p_png, NULL, NULL );
227 block_Release( p_block );
228 return VLCDEC_SUCCESS;
231 p_end_info = png_create_info_struct( p_png );
232 if( p_end_info == NULL )
234 png_destroy_read_struct( &p_png, &p_info, NULL );
235 block_Release( p_block );
236 return VLCDEC_SUCCESS;
239 /* libpng longjmp's there in case of error */
240 if( setjmp( png_jmpbuf( p_png ) ) )
241 goto error;
243 png_set_read_fn( p_png, p_block, user_read );
244 png_set_error_fn( p_png, p_sys, user_error, user_warning );
246 png_read_info( p_png, p_info );
247 if( p_sys->b_error ) goto error;
249 png_get_IHDR( p_png, p_info, &i_width, &i_height,
250 &i_bit_depth, &i_color_type, &i_interlace_type,
251 &i_compression_type, &i_filter_type);
252 if( p_sys->b_error ) goto error;
254 /* Set output properties */
255 p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
256 p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width = i_width;
257 p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height = i_height;
258 p_dec->fmt_out.video.i_sar_num = 1;
259 p_dec->fmt_out.video.i_sar_den = 1;
261 if( i_color_type == PNG_COLOR_TYPE_PALETTE )
262 png_set_palette_to_rgb( p_png );
264 if( i_color_type == PNG_COLOR_TYPE_GRAY ||
265 i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
266 png_set_gray_to_rgb( p_png );
267 if( i_color_type & PNG_COLOR_MASK_ALPHA )
268 png_set_alpha_mode( p_png, PNG_ALPHA_OPTIMIZED, PNG_DEFAULT_sRGB );
270 /* Strip to 8 bits per channel */
271 if( i_bit_depth == 16 )
273 #if PNG_LIBPNG_VER >= 10504
274 png_set_scale_16( p_png );
275 #else
276 png_set_strip_16( p_png );
277 #endif
280 if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
282 png_set_tRNS_to_alpha( p_png );
284 else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
286 p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
289 /* Get a new picture */
290 if( decoder_UpdateVideoFormat( p_dec ) )
291 goto error;
292 p_pic = decoder_NewPicture( p_dec );
293 if( !p_pic ) goto error;
295 /* Decode picture */
296 p_row_pointers = vlc_alloc( i_height, sizeof(png_bytep) );
297 if( !p_row_pointers )
298 goto error;
299 for( i = 0; i < (int)i_height; i++ )
300 p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
302 png_read_image( p_png, p_row_pointers );
303 if( p_sys->b_error ) goto error;
304 png_read_end( p_png, p_end_info );
305 if( p_sys->b_error ) goto error;
307 png_destroy_read_struct( &p_png, &p_info, &p_end_info );
308 free( p_row_pointers );
310 p_pic->date = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : p_block->i_dts;
312 block_Release( p_block );
313 decoder_QueueVideo( p_dec, p_pic );
314 return VLCDEC_SUCCESS;
316 error:
318 free( p_row_pointers );
319 png_destroy_read_struct( &p_png, &p_info, &p_end_info );
320 block_Release( p_block );
321 return VLCDEC_SUCCESS;
324 /*****************************************************************************
325 * CloseDecoder: png decoder destruction
326 *****************************************************************************/
327 static void CloseDecoder( vlc_object_t *p_this )
329 decoder_t *p_dec = (decoder_t *)p_this;
330 decoder_sys_t *p_sys = p_dec->p_sys;
332 free( p_sys );
335 static int OpenEncoder(vlc_object_t *p_this)
337 encoder_t *p_enc = (encoder_t *) p_this;
339 if( p_enc->fmt_out.i_codec != VLC_CODEC_PNG )
340 return VLC_EGENERIC;
342 /* Allocate the memory needed to store the encoder's structure */
343 encoder_sys_t *p_sys = malloc( sizeof(encoder_sys_t) );
344 if( p_sys == NULL )
345 return VLC_ENOMEM;
346 p_enc->p_sys = p_sys;
348 p_sys->p_obj = p_this;
350 p_sys->i_blocksize = 3 * p_enc->fmt_in.video.i_visible_width *
351 p_enc->fmt_in.video.i_visible_height;
353 p_enc->fmt_in.i_codec = VLC_CODEC_RGB24;
354 p_enc->fmt_in.video.i_bmask = 0;
355 video_format_FixRgb( &p_enc->fmt_in.video );
357 p_enc->pf_encode_video = EncodeBlock;
359 return VLC_SUCCESS;
363 * EncodeBlock
365 static block_t *EncodeBlock(encoder_t *p_enc, picture_t *p_pic)
367 encoder_sys_t *p_sys = p_enc->p_sys;
369 if( unlikely( !p_pic ) )
371 return NULL;
374 block_t *p_block = block_Alloc( p_sys->i_blocksize );
375 if( p_block == NULL )
377 return NULL;
380 png_structp p_png = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
381 if( p_png == NULL )
383 block_Release( p_block );
384 return NULL;
387 /* Disable filtering to speed-up encoding */
388 png_set_filter( p_png, 0, PNG_NO_FILTERS );
389 /* 1 == best speed */
390 png_set_compression_level( p_png, 1 );
392 /* save buffer start */
393 uint8_t *p_start = p_block->p_buffer;
394 size_t i_start = p_block->i_buffer;
396 p_sys->b_error = false;
397 png_infop p_info = NULL;
399 /* libpng longjmp's there in case of error */
400 if( setjmp( png_jmpbuf( p_png ) ) )
401 goto error;
403 png_set_write_fn( p_png, p_block, user_write, user_flush );
404 png_set_error_fn( p_png, p_sys, user_error, user_warning );
406 p_info = png_create_info_struct( p_png );
407 if( p_info == NULL )
408 goto error;
410 png_set_IHDR( p_png, p_info,
411 p_enc->fmt_in.video.i_visible_width,
412 p_enc->fmt_in.video.i_visible_height,
413 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
414 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
415 if( p_sys->b_error ) goto error;
417 png_write_info( p_png, p_info );
418 if( p_sys->b_error ) goto error;
420 /* Encode picture */
422 for( int i = 0; i < p_pic->p->i_visible_lines; i++ )
424 png_write_row( p_png, p_pic->p->p_pixels + (i * p_pic->p->i_pitch) );
425 if( p_sys->b_error ) goto error;
428 png_write_end( p_png, p_info );
429 if( p_sys->b_error ) goto error;
431 png_destroy_write_struct( &p_png, &p_info );
433 /* restore original buffer position */
434 p_block->p_buffer = p_start;
435 p_block->i_buffer = i_start - p_block->i_buffer;
437 p_block->i_dts = p_block->i_pts = p_pic->date;
439 return p_block;
441 error:
443 png_destroy_write_struct( &p_png, p_info ? &p_info : NULL );
445 block_Release(p_block);
446 return NULL;
449 /*****************************************************************************
450 * CloseEncoder: png encoder destruction
451 *****************************************************************************/
452 static void CloseEncoder( vlc_object_t *p_this )
454 encoder_t *p_enc = (encoder_t *)p_this;
455 encoder_sys_t *p_sys = p_enc->p_sys;
457 free( p_sys );