]> git.sesse.net Git - vlc/blob - modules/codec/png.c
mediacodec: fix warning
[vlc] / modules / codec / png.c
1 /*****************************************************************************
2  * png.c: png decoder module making use of libpng.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VLC authors and VideoLAN
5  * $Id$
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 picture_t *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 ()
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 )
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_cat = VIDEO_ES;
127     p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
128
129     /* Set callbacks */
130     p_dec->pf_decode_video = DecodeBlock;
131
132     return VLC_SUCCESS;
133 }
134
135 static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
136 {
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" );
140         return;
141     }
142
143     memcpy( data, p_block->p_buffer, i_length );
144     p_block->p_buffer += i_length;
145     p_block->i_buffer -= i_length;
146 }
147
148 static void user_flush( png_structp p_png )
149 {
150     /* noop */
151     (void) p_png;
152 }
153
154 static void user_write( png_structp p_png, png_bytep data, png_size_t i_length )
155 {
156     block_t *p_block = (block_t *)png_get_io_ptr( p_png );
157     if( i_length > p_block->i_buffer ) {
158         char err_str[64];
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 );
163         return;
164     }
165
166     memcpy( p_block->p_buffer, data, i_length );
167     p_block->p_buffer += i_length;
168     p_block->i_buffer -= i_length;
169 }
170
171 static void user_error( png_structp p_png, png_const_charp error_msg )
172 {
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 );
176 }
177
178 static void user_warning( png_structp p_png, png_const_charp warning_msg )
179 {
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 );
182 }
183
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 )
190 {
191     decoder_sys_t *p_sys = p_dec->p_sys;
192     block_t *p_block;
193     picture_t *p_pic = 0;
194
195     png_uint_32 i_width, i_height;
196     int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
197     int i_bit_depth, i;
198
199     png_structp p_png;
200     png_infop p_info, p_end_info;
201     png_bytep *p_row_pointers = NULL;
202
203     if( !pp_block || !*pp_block ) return NULL;
204
205     p_block = *pp_block;
206     p_sys->b_error = false;
207
208     if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
209     {
210         block_Release( p_block ); *pp_block = NULL;
211         return NULL;
212     }
213
214     p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
215     if( p_png == NULL )
216     {
217         block_Release( p_block ); *pp_block = NULL;
218         return NULL;
219     }
220
221     p_info = png_create_info_struct( p_png );
222     if( p_info == NULL )
223     {
224         png_destroy_read_struct( &p_png, NULL, NULL );
225         block_Release( p_block ); *pp_block = NULL;
226         return NULL;
227     }
228
229     p_end_info = png_create_info_struct( p_png );
230     if( p_end_info == NULL )
231     {
232         png_destroy_read_struct( &p_png, &p_info, NULL );
233         block_Release( p_block ); *pp_block = NULL;
234         return NULL;
235     }
236
237     /* libpng longjmp's there in case of error */
238     if( setjmp( png_jmpbuf( p_png ) ) )
239         goto error;
240
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 );
243
244     png_read_info( p_png, p_info );
245     if( p_sys->b_error ) goto error;
246
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;
251
252     /* Set output properties */
253     p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
254     p_dec->fmt_out.video.i_visible_width = p_dec->fmt_out.video.i_width = i_width;
255     p_dec->fmt_out.video.i_visible_height = 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;
261
262     if( i_color_type == PNG_COLOR_TYPE_PALETTE )
263         png_set_palette_to_rgb( p_png );
264
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 );
268
269     /* Strip to 8 bits per channel */
270     if( i_bit_depth == 16 ) png_set_strip_16( p_png );
271
272     if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
273     {
274         png_set_tRNS_to_alpha( p_png );
275     }
276     else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
277     {
278         p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
279     }
280
281     /* Get a new picture */
282     p_pic = decoder_NewPicture( p_dec );
283     if( !p_pic ) goto error;
284
285     /* Decode picture */
286     p_row_pointers = malloc( sizeof(png_bytep) * i_height );
287     if( !p_row_pointers )
288         goto error;
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;
291
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;
296
297     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
298     free( p_row_pointers );
299
300     p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
301
302     block_Release( p_block ); *pp_block = NULL;
303     return p_pic;
304
305  error:
306
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;
310     return NULL;
311 }
312
313 /*****************************************************************************
314  * CloseDecoder: png decoder destruction
315  *****************************************************************************/
316 static void CloseDecoder( vlc_object_t *p_this )
317 {
318     decoder_t *p_dec = (decoder_t *)p_this;
319     decoder_sys_t *p_sys = p_dec->p_sys;
320
321     free( p_sys );
322 }
323
324 static int OpenEncoder(vlc_object_t *p_this)
325 {
326     encoder_t *p_enc = (encoder_t *) p_this;
327
328     if( p_enc->fmt_out.i_codec != VLC_CODEC_PNG )
329         return VLC_EGENERIC;
330
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 )
334         return VLC_ENOMEM;
335
336     p_enc->p_sys->p_obj = p_this;
337
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;
340
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;
346
347     return VLC_SUCCESS;
348 }
349
350 /*
351  * EncodeBlock
352  */
353 static block_t *EncodeBlock(encoder_t *p_enc, picture_t *p_pic)
354 {
355     encoder_sys_t *p_sys = p_enc->p_sys;
356
357     if( unlikely( !p_pic ) )
358     {
359         return NULL;
360     }
361
362     block_t *p_block = block_Alloc( p_sys->i_blocksize );
363     if( p_block == NULL )
364     {
365         return NULL;
366     }
367
368     png_structp p_png = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
369     if( p_png == NULL )
370     {
371         block_Release( p_block );
372         return NULL;
373     }
374
375     /* save buffer start */
376     uint8_t *p_start = p_block->p_buffer;
377     size_t i_start = p_block->i_buffer;
378
379     p_sys->b_error = false;
380     png_infop p_info = NULL;
381
382     /* libpng longjmp's there in case of error */
383     if( setjmp( png_jmpbuf( p_png ) ) )
384         goto error;
385
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 );
388
389     p_info = png_create_info_struct( p_png );
390     if( p_info == NULL )
391         goto error;
392
393     png_infop p_end_info = png_create_info_struct( p_png );
394     if( p_end_info == NULL ) goto error;
395
396     png_set_IHDR( p_png, p_info,
397             p_enc->fmt_in.video.i_visible_width,
398             p_enc->fmt_in.video.i_visible_height,
399             8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
400             PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT );
401     if( p_sys->b_error ) goto error;
402
403     png_write_info( p_png, p_info );
404     if( p_sys->b_error ) goto error;
405
406     /* Encode picture */
407
408     for( int i = 0; i < p_pic->p->i_visible_lines; i++ )
409     {
410         png_write_row( p_png, p_pic->p->p_pixels + (i * p_pic->p->i_pitch) );
411         if( p_sys->b_error ) goto error;
412     }
413
414     png_write_end( p_png, p_end_info );
415     if( p_sys->b_error ) goto error;
416
417     png_destroy_write_struct( &p_png, &p_info );
418
419     /* restore original buffer position */
420     p_block->p_buffer = p_start;
421     p_block->i_buffer = i_start - p_block->i_buffer;
422
423     p_block->i_dts = p_block->i_pts = p_pic->date;
424
425     return p_block;
426
427  error:
428
429     png_destroy_write_struct( &p_png, p_info ? &p_info : NULL );
430
431     block_Release(p_block);
432     return NULL;
433 }
434
435 /*****************************************************************************
436  * CloseEncoder: png encoder destruction
437  *****************************************************************************/
438 static void CloseEncoder( vlc_object_t *p_this )
439 {
440     encoder_t *p_enc = (encoder_t *)p_this;
441     encoder_sys_t *p_sys = p_enc->p_sys;
442
443     free( p_sys );
444 }