1 /*****************************************************************************
2 * png.c: png decoder module making use of libpng.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VLC authors and VideoLAN
5 * $Id: 5ef76f0a07753ebc9f382434ba5fbd1a7a114dd2 $
6 *
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 *
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.
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 Lesser General Public License for more details.
18 *
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 *****************************************************************************/
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 <png.h>
35
36 /* PNG_SYS_COMMON_MEMBERS:
37 * members common to encoder and decoder descriptors
38 */
39 #define PNG_SYS_COMMON_MEMBERS \
40 /**@{*/ \
41 bool b_error; \
42 vlc_object_t *p_obj; \
43 /**@}*/ \
44
45 /*
46 * png common descriptor
47 */
48 struct png_sys_t
49 {
50 PNG_SYS_COMMON_MEMBERS
51 };
52
53 typedef struct png_sys_t png_sys_t;
54
55 /*****************************************************************************
56 * decoder_sys_t : png decoder descriptor
57 *****************************************************************************/
58 struct decoder_sys_t
59 {
60 PNG_SYS_COMMON_MEMBERS
61 };
62
63 /*****************************************************************************
64 * Local prototypes
65 *****************************************************************************/
66 static int OpenDecoder ( vlc_object_t * );
67 static void CloseDecoder ( vlc_object_t * );
68
69 static int DecodeBlock ( decoder_t *, block_t * );
70
71 /*
72 * png encoder descriptor
73 */
74 struct encoder_sys_t
75 {
76 PNG_SYS_COMMON_MEMBERS
77 int i_blocksize;
78 };
79
80 static int OpenEncoder(vlc_object_t *);
81 static void CloseEncoder(vlc_object_t *);
82
83 static block_t *EncodeBlock(encoder_t *, picture_t *);
84
85 /*****************************************************************************
86 * Module descriptor
87 *****************************************************************************/
88 vlc_module_begin ()
set_category(CAT_INPUT)89 set_category( CAT_INPUT )
90 set_subcategory( SUBCAT_INPUT_VCODEC )
91 set_description( N_("PNG video decoder") )
92 set_capability( "video decoder", 1000 )
93 set_callbacks( OpenDecoder, CloseDecoder )
94 add_shortcut( "png" )
95
96 /* encoder submodule */
97 add_submodule()
98 add_shortcut("png")
99 set_section(N_("Encoding"), NULL)
100 set_description(N_("PNG video encoder"))
101 set_capability("encoder", 1000)
102 set_callbacks(OpenEncoder, CloseEncoder)
103 vlc_module_end ()
104
105 /*****************************************************************************
106 * OpenDecoder: probe the decoder and return score
107 *****************************************************************************/
108 static int OpenDecoder( vlc_object_t *p_this )
109 {
110 decoder_t *p_dec = (decoder_t*)p_this;
111
112 if( p_dec->fmt_in.i_codec != VLC_CODEC_PNG &&
113 p_dec->fmt_in.i_codec != VLC_FOURCC('M','P','N','G') )
114 {
115 return VLC_EGENERIC;
116 }
117
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 )
121 return VLC_ENOMEM;
122
123 p_dec->p_sys->p_obj = p_this;
124
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.b_color_range_full = true;
131
132 /* Set callbacks */
133 p_dec->pf_decode = DecodeBlock;
134
135 return VLC_SUCCESS;
136 }
137
user_read(png_structp p_png,png_bytep data,png_size_t i_length)138 static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
139 {
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;
144 }
145
146 memcpy( data, p_block->p_buffer, i_length );
147 p_block->p_buffer += i_length;
148 p_block->i_buffer -= i_length;
149 }
150
user_flush(png_structp p_png)151 static void user_flush( png_structp p_png )
152 {
153 /* noop */
154 (void) p_png;
155 }
156
user_write(png_structp p_png,png_bytep data,png_size_t i_length)157 static void user_write( png_structp p_png, png_bytep data, png_size_t i_length )
158 {
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;
167 }
168
169 memcpy( p_block->p_buffer, data, i_length );
170 p_block->p_buffer += i_length;
171 p_block->i_buffer -= i_length;
172 }
173
user_error(png_structp p_png,png_const_charp error_msg)174 static void user_error( png_structp p_png, png_const_charp error_msg )
175 {
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 );
179 }
180
user_warning(png_structp p_png,png_const_charp warning_msg)181 static void user_warning( png_structp p_png, png_const_charp warning_msg )
182 {
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 );
185 }
186
187 /****************************************************************************
188 * DecodeBlock: the whole thing
189 ****************************************************************************
190 * This function must be fed with a complete compressed frame.
191 ****************************************************************************/
DecodeBlock(decoder_t * p_dec,block_t * p_block)192 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
193 {
194 decoder_sys_t *p_sys = p_dec->p_sys;
195 picture_t *p_pic = 0;
196
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;
200
201 png_structp p_png;
202 png_infop p_info, p_end_info;
203 png_bytep *volatile p_row_pointers = NULL;
204
205 if( !p_block ) /* No Drain */
206 return VLCDEC_SUCCESS;
207
208 p_sys->b_error = false;
209
210 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
211 {
212 block_Release( p_block );
213 return VLCDEC_SUCCESS;
214 }
215
216 p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
217 if( p_png == NULL )
218 {
219 block_Release( p_block );
220 return VLCDEC_SUCCESS;
221 }
222
223 p_info = png_create_info_struct( p_png );
224 if( p_info == NULL )
225 {
226 png_destroy_read_struct( &p_png, NULL, NULL );
227 block_Release( p_block );
228 return VLCDEC_SUCCESS;
229 }
230
231 p_end_info = png_create_info_struct( p_png );
232 if( p_end_info == NULL )
233 {
234 png_destroy_read_struct( &p_png, &p_info, NULL );
235 block_Release( p_block );
236 return VLCDEC_SUCCESS;
237 }
238
239 /* libpng longjmp's there in case of error */
240 if( setjmp( png_jmpbuf( p_png ) ) )
241 goto error;
242
243 png_set_read_fn( p_png, (void *)p_block, user_read );
244 png_set_error_fn( p_png, (void *)p_dec, user_error, user_warning );
245
246 png_read_info( p_png, p_info );
247 if( p_sys->b_error ) goto error;
248
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;
253
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;
260
261 if( i_color_type == PNG_COLOR_TYPE_PALETTE )
262 png_set_palette_to_rgb( p_png );
263
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 );
269
270 /* Strip to 8 bits per channel */
271 if( i_bit_depth == 16 )
272 {
273 #if PNG_LIBPNG_VER >= 10504
274 png_set_scale_16( p_png );
275 #else
276 png_set_strip_16( p_png );
277 #endif
278 }
279
280 if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
281 {
282 png_set_tRNS_to_alpha( p_png );
283 }
284 else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
285 {
286 p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
287 }
288
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;
294
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;
301
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;
306
307 png_destroy_read_struct( &p_png, &p_info, &p_end_info );
308 free( p_row_pointers );
309
310 p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
311
312 block_Release( p_block );
313 decoder_QueueVideo( p_dec, p_pic );
314 return VLCDEC_SUCCESS;
315
316 error:
317
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;
322 }
323
324 /*****************************************************************************
325 * CloseDecoder: png decoder destruction
326 *****************************************************************************/
CloseDecoder(vlc_object_t * p_this)327 static void CloseDecoder( vlc_object_t *p_this )
328 {
329 decoder_t *p_dec = (decoder_t *)p_this;
330 decoder_sys_t *p_sys = p_dec->p_sys;
331
332 free( p_sys );
333 }
334
OpenEncoder(vlc_object_t * p_this)335 static int OpenEncoder(vlc_object_t *p_this)
336 {
337 encoder_t *p_enc = (encoder_t *) p_this;
338
339 if( p_enc->fmt_out.i_codec != VLC_CODEC_PNG )
340 return VLC_EGENERIC;
341
342 /* Allocate the memory needed to store the encoder's structure */
343 p_enc->p_sys = malloc( sizeof(encoder_sys_t) );
344 if( p_enc->p_sys == NULL )
345 return VLC_ENOMEM;
346
347 p_enc->p_sys->p_obj = p_this;
348
349 p_enc->p_sys->i_blocksize = 3 * p_enc->fmt_in.video.i_visible_width *
350 p_enc->fmt_in.video.i_visible_height;
351
352 p_enc->fmt_in.i_codec = VLC_CODEC_RGB24;
353 p_enc->fmt_in.video.i_bmask = 0;
354 video_format_FixRgb( &p_enc->fmt_in.video );
355
356 p_enc->pf_encode_video = EncodeBlock;
357
358 return VLC_SUCCESS;
359 }
360
361 /*
362 * EncodeBlock
363 */
EncodeBlock(encoder_t * p_enc,picture_t * p_pic)364 static block_t *EncodeBlock(encoder_t *p_enc, picture_t *p_pic)
365 {
366 encoder_sys_t *p_sys = p_enc->p_sys;
367
368 if( unlikely( !p_pic ) )
369 {
370 return NULL;
371 }
372
373 block_t *p_block = block_Alloc( p_sys->i_blocksize );
374 if( p_block == NULL )
375 {
376 return NULL;
377 }
378
379 png_structp p_png = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
380 if( p_png == NULL )
381 {
382 block_Release( p_block );
383 return NULL;
384 }
385
386 /* Disable filtering to speed-up encoding */
387 png_set_filter( p_png, 0, PNG_NO_FILTERS );
388 /* 1 == best speed */
389 png_set_compression_level( p_png, 1 );
390
391 /* save buffer start */
392 uint8_t *p_start = p_block->p_buffer;
393 size_t i_start = p_block->i_buffer;
394
395 p_sys->b_error = false;
396 png_infop p_info = NULL;
397
398 /* libpng longjmp's there in case of error */
399 if( setjmp( png_jmpbuf( p_png ) ) )
400 goto error;
401
402 png_set_write_fn( p_png, p_block, user_write, user_flush );
403 png_set_error_fn( p_png, p_enc, user_error, user_warning );
404
405 p_info = png_create_info_struct( p_png );
406 if( p_info == NULL )
407 goto error;
408
409 png_set_IHDR( p_png, p_info,
410 p_enc->fmt_in.video.i_visible_width,
411 p_enc->fmt_in.video.i_visible_height,
412 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
413 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
414 if( p_sys->b_error ) goto error;
415
416 png_write_info( p_png, p_info );
417 if( p_sys->b_error ) goto error;
418
419 /* Encode picture */
420
421 for( int i = 0; i < p_pic->p->i_visible_lines; i++ )
422 {
423 png_write_row( p_png, p_pic->p->p_pixels + (i * p_pic->p->i_pitch) );
424 if( p_sys->b_error ) goto error;
425 }
426
427 png_write_end( p_png, p_info );
428 if( p_sys->b_error ) goto error;
429
430 png_destroy_write_struct( &p_png, &p_info );
431
432 /* restore original buffer position */
433 p_block->p_buffer = p_start;
434 p_block->i_buffer = i_start - p_block->i_buffer;
435
436 p_block->i_dts = p_block->i_pts = p_pic->date;
437
438 return p_block;
439
440 error:
441
442 png_destroy_write_struct( &p_png, p_info ? &p_info : NULL );
443
444 block_Release(p_block);
445 return NULL;
446 }
447
448 /*****************************************************************************
449 * CloseEncoder: png encoder destruction
450 *****************************************************************************/
CloseEncoder(vlc_object_t * p_this)451 static void CloseEncoder( vlc_object_t *p_this )
452 {
453 encoder_t *p_enc = (encoder_t *)p_this;
454 encoder_sys_t *p_sys = p_enc->p_sys;
455
456 free( p_sys );
457 }
458