]> git.sesse.net Git - vlc/blobdiff - modules/codec/png.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / codec / png.c
index 80e85b0f1c6621c231d345308ac90b638cca5bad..acd569481013008c063b7054009e9f721979eafb 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * png.c: png decoder module making use of libpng.
  *****************************************************************************
- * Copyright (C) 1999-2001 VideoLAN
+ * Copyright (C) 1999-2001 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
  *
  * 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 <png.h>
 
 /*****************************************************************************
@@ -34,7 +38,7 @@
  *****************************************************************************/
 struct decoder_sys_t
 {
-    vlc_bool_t b_error;
+    bool b_error;
 };
 
 /*****************************************************************************
@@ -45,28 +49,17 @@ static void CloseDecoder  ( vlc_object_t * );
 
 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
 
-static int  OpenEncoder   ( vlc_object_t * );
-static void CloseEncoder  ( vlc_object_t * );
-static block_t *Encode( encoder_t *p_enc, picture_t *p_pic );
-
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-vlc_module_begin();
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_VCODEC );
-    set_shortname( _("PNG" ) );
-    set_description( _("PNG image decoder") );
-    set_capability( "decoder", 1000 );
-    set_callbacks( OpenDecoder, CloseDecoder );
-    add_shortcut( "png" );
-
-    add_submodule();
-    set_description( _( "PNG image encoder" ) );
-    set_capability( "encoder", 100 );
-    set_callbacks( OpenEncoder, CloseEncoder );
-    add_shortcut( "png" );
-vlc_module_end();
+vlc_module_begin ()
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_VCODEC )
+    set_description( N_("PNG video decoder") )
+    set_capability( "decoder", 1000 )
+    set_callbacks( OpenDecoder, CloseDecoder )
+    add_shortcut( "png" )
+vlc_module_end ()
 
 /*****************************************************************************
  * OpenDecoder: probe the decoder and return score
@@ -74,24 +67,20 @@ vlc_module_end();
 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_CODEC_PNG &&
+        p_dec->fmt_in.i_codec != VLC_FOURCC('M','P','N','G') )
     {
         return VLC_EGENERIC;
     }
 
     /* 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;
-    }
+    if( ( p_dec->p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
+        return VLC_ENOMEM;
 
     /* Set output properties */
     p_dec->fmt_out.i_cat = VIDEO_ES;
-    p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
+    p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
 
     /* Set callbacks */
     p_dec->pf_decode_video = DecodeBlock;
@@ -102,7 +91,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;
@@ -113,14 +102,14 @@ 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;
-    msg_Err( p_dec, error_msg );
+    p_dec->p_sys->b_error = true;
+    msg_Err( p_dec, "%s", error_msg );
 }
 
 static void user_warning( png_structp p_png, png_const_charp warning_msg )
 {
     decoder_t *p_dec = (decoder_t *)png_get_error_ptr( p_png );
-    msg_Warn( p_dec, warning_msg );
+    msg_Warn( p_dec, "%s", warning_msg );
 }
 
 /****************************************************************************
@@ -145,11 +134,40 @@ 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;
+
+    if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
+    {
+        block_Release( p_block ); *pp_block = NULL;
+        return NULL;
+    }
 
     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, NULL, 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, 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 );
@@ -163,10 +181,14 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
     if( p_sys->b_error ) goto error;
 
     /* Set output properties */
-    p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
+    p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
     p_dec->fmt_out.video.i_width = i_width;
     p_dec->fmt_out.video.i_height = i_height;
-    p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
+    p_dec->fmt_out.video.i_sar_num = 1;
+    p_dec->fmt_out.video.i_sar_den = 1;
+    p_dec->fmt_out.video.i_rmask = 0x000000ff;
+    p_dec->fmt_out.video.i_gmask = 0x0000ff00;
+    p_dec->fmt_out.video.i_bmask = 0x00ff0000;
 
     if( i_color_type == PNG_COLOR_TYPE_PALETTE )
         png_set_palette_to_rgb( p_png );
@@ -184,21 +206,17 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
     }
     else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
     {
-        p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','2','4');
-    }
-    if( i_color_type & PNG_COLOR_MASK_COLOR &&
-        p_dec->fmt_out.i_codec != VLC_FOURCC('R','V','2','4') )
-    {
-        /* Invert colors */
-        png_set_bgr( p_png );
+        p_dec->fmt_out.i_codec = VLC_CODEC_RGB24;
     }
 
     /* Get a new picture */
-    p_pic = p_dec->pf_vout_buffer_new( p_dec );
+    p_pic = decoder_NewPicture( p_dec );
     if( !p_pic ) goto error;
 
     /* 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;
 
@@ -210,12 +228,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 > VLC_TS_INVALID ? 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;
@@ -231,119 +251,3 @@ static void CloseDecoder( vlc_object_t *p_this )
 
     free( p_sys );
 }
-
-/*****************************************************************************
- * PNG Encoder
- *****************************************************************************/
-struct encoder_sys_t
-{
-    block_t *p_chain;
-    mtime_t date;
-    vlc_bool_t b_error;
-};
-
-
-static int OpenEncoder( vlc_object_t *p_this )
-{
-    encoder_t *p_enc = (encoder_t *)p_this;
-    encoder_sys_t *p_sys = p_enc->p_sys;
-
-    if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'p', 'n', 'g', ' ' ) &&
-        !p_enc->b_force )
-    {
-        return VLC_EGENERIC;
-    }
-
-    /* Allocate the memory needed to store the encoder structure */
-    if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
-    {
-        msg_Err( p_enc, "out of memory" );
-        return VLC_EGENERIC;
-    }
-
-    p_enc->p_sys = p_sys;
-    p_enc->p_sys->p_chain = NULL;
-
-    p_enc->pf_encode_video = Encode;
-
-    return VLC_SUCCESS;
-}
-
-static void CloseEncoder( vlc_object_t *p_this )
-{
-}
-
-static void user_write( png_structp p_png, png_bytep data, png_size_t i_length )
-{
-    encoder_t *p_enc = (encoder_t *)png_get_io_ptr( p_png );
-
-    block_t *p_block = block_New( p_enc, i_length );
-
-    memcpy( p_block->p_buffer, data, i_length );
-    p_block->i_dts = p_block->i_pts =  p_enc->p_sys->date;
-    block_ChainAppend( &p_enc->p_sys->p_chain, p_block );
-}
-
-static void user_flush( png_structp p_png )
-{
-}
-
-static void user_write_error( png_structp p_png, png_const_charp error_msg )
-{
-    encoder_t *p_enc = (encoder_t *)png_get_error_ptr( p_png );
-    p_enc->p_sys->b_error = VLC_TRUE;
-    msg_Err( p_enc, error_msg );
-}
-
-static void user_write_warning( png_structp p_png, png_const_charp warning_msg )
-{
-    encoder_t *p_enc = (encoder_t *)png_get_error_ptr( p_png );
-    msg_Warn( p_enc, warning_msg );
-}
-
-
-static block_t *Encode( encoder_t *p_enc, picture_t *p_pic )
-{
-    png_structp p_png;
-    png_infop p_info;
-    png_bytep *p_row_pointers = NULL;
-    int i;
-    block_t *p_gather = NULL;
-
-    p_enc->p_sys->date = p_pic->date;
-
-    p_png =  png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
-    p_info = png_create_info_struct( p_png );
-
-    png_set_write_fn( p_png, (void *)p_enc, user_write, user_flush );
-    png_set_error_fn( p_png, (void *)p_enc, user_write_error,
-                                            user_write_warning );
-
-    png_set_IHDR( p_png, p_info,
-                  p_pic->format.i_width,
-                  p_pic->format.i_height,
-                  8, PNG_COLOR_TYPE_RGB,
-                  PNG_INTERLACE_NONE,
-                  PNG_COMPRESSION_TYPE_DEFAULT,
-                  PNG_FILTER_TYPE_DEFAULT );
-
-    p_row_pointers = malloc( sizeof(png_bytep) * p_pic->format.i_height );
-    for( i = 0; i < (int)p_pic->format.i_height; i++ )
-        p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
-
-    png_write_info( p_png, p_info );
-    png_write_image( p_png, p_row_pointers );
-    png_write_end( p_png, p_info );
-
-    png_destroy_write_struct( &p_png, &p_info );
-
-    if( p_enc->p_sys->p_chain )
-    {
-        p_gather = block_ChainGather( p_enc->p_sys->p_chain );
-    }
-    p_enc->p_sys->p_chain = NULL;
-
-    free( p_row_pointers );
-
-    return p_gather;
-}