]> git.sesse.net Git - vlc/blobdiff - modules/codec/png.c
lua/intf: Fix a memleak.
[vlc] / modules / codec / png.c
index 06c6b12466c9f0ace298382ae5ef641e37c5d23e..2b04b965ea53f494e3c8b96cf360799506a1b1c3 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
-#include <vlc/decoder.h>
-
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_codec.h>
+#include <vlc_vout.h>
 #include <png.h>
 
 /*****************************************************************************
@@ -34,7 +39,7 @@
  *****************************************************************************/
 struct decoder_sys_t
 {
-    vlc_bool_t b_error;
+    bool b_error;
 };
 
 /*****************************************************************************
@@ -51,7 +56,7 @@ static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
 vlc_module_begin();
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_VCODEC );
-    set_description( _("PNG video decoder") );
+    set_description( N_("PNG video decoder") );
     set_capability( "decoder", 1000 );
     set_callbacks( OpenDecoder, CloseDecoder );
     add_shortcut( "png" );
@@ -65,7 +70,8 @@ static int OpenDecoder( vlc_object_t *p_this )
     decoder_t *p_dec = (decoder_t*)p_this;
     decoder_sys_t *p_sys;
 
-    if( p_dec->fmt_in.i_codec != VLC_FOURCC('p','n','g',' ') )
+    if( p_dec->fmt_in.i_codec != VLC_FOURCC('p','n','g',' ') &&
+        p_dec->fmt_in.i_codec != VLC_FOURCC('M','P','N','G') )
     {
         return VLC_EGENERIC;
     }
@@ -73,10 +79,7 @@ static int OpenDecoder( vlc_object_t *p_this )
     /* Allocate the memory needed to store the decoder's structure */
     if( ( p_dec->p_sys = p_sys =
           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
-    {
-        msg_Err( p_dec, "out of memory" );
-        return VLC_EGENERIC;
-    }
+        return VLC_ENOMEM;
 
     /* Set output properties */
     p_dec->fmt_out.i_cat = VIDEO_ES;
@@ -91,7 +94,7 @@ static int OpenDecoder( vlc_object_t *p_this )
 static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
 {
     block_t *p_block = (block_t *)png_get_io_ptr( p_png );
-    png_size_t i_read = __MIN( p_block->i_buffer, (int)i_length );
+    png_size_t i_read = __MIN( p_block->i_buffer, i_length );
     memcpy( data, p_block->p_buffer, i_length );
     p_block->p_buffer += i_length;
     p_block->i_buffer -= i_length;
@@ -102,7 +105,7 @@ static void user_read( png_structp p_png, png_bytep data, png_size_t i_length )
 static void user_error( png_structp p_png, png_const_charp error_msg )
 {
     decoder_t *p_dec = (decoder_t *)png_get_error_ptr( p_png );
-    p_dec->p_sys->b_error = VLC_TRUE;
+    p_dec->p_sys->b_error = true;
     msg_Err( p_dec, error_msg );
 }
 
@@ -134,12 +137,35 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
     if( !pp_block || !*pp_block ) return NULL;
 
     p_block = *pp_block;
-    p_sys->b_error = VLC_FALSE;
+    p_sys->b_error = false;
 
     p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
+    if( p_png == NULL )
+    {
+        block_Release( p_block ); *pp_block = NULL;
+        return NULL;
+    }
     p_info = png_create_info_struct( p_png );
+    if( p_info == NULL )
+    {
+        png_destroy_read_struct( &p_png, png_infopp_NULL, png_infopp_NULL );
+        block_Release( p_block ); *pp_block = NULL;
+        return NULL;
+    }
+
     p_end_info = png_create_info_struct( p_png );
+    if( p_end_info == NULL )
+    {
+        png_destroy_read_struct( &p_png, &p_info, png_infopp_NULL );
+        block_Release( p_block ); *pp_block = NULL;
+        return NULL;
+    }
  
+    /* libpng longjmp's there in case of error */
+    if( setjmp( png_jmpbuf( p_png ) ) )
+        goto error;
+
     png_set_read_fn( p_png, (void *)p_block, user_read );
     png_set_error_fn( p_png, (void *)p_dec, user_error, user_warning );
 
@@ -188,6 +214,8 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
 
     /* Decode picture */
     p_row_pointers = malloc( sizeof(png_bytep) * i_height );
+    if( !p_row_pointers )
+        goto error;
     for( i = 0; i < (int)i_height; i++ )
         p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
 
@@ -199,12 +227,14 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
     free( p_row_pointers );
 
+    p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
+
     block_Release( p_block ); *pp_block = NULL;
     return p_pic;
 
  error:
 
-    if( p_row_pointers ) free( p_row_pointers );
+    free( p_row_pointers );
     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
     block_Release( p_block ); *pp_block = NULL;
     return NULL;