]> git.sesse.net Git - vlc/commitdiff
PNG image encoder and imagefile video output
authorClément Stenac <zorglub@videolan.org>
Wed, 29 Dec 2004 15:29:20 +0000 (15:29 +0000)
committerClément Stenac <zorglub@videolan.org>
Wed, 29 Dec 2004 15:29:20 +0000 (15:29 +0000)
configure.ac
modules/codec/png.c
modules/video_output/image.c [new file with mode: 0644]

index 108753074935eef61b8dcae32b11f3e933875feb..1956033fd83535206f170918bff224acdb542f9a 100644 (file)
@@ -954,6 +954,7 @@ VLC_ADD_PLUGINS([trivial_channel_mixer simple_channel_mixer headphone_channel_mi
 VLC_ADD_PLUGINS([trivial_mixer spdif_mixer float32_mixer])
 VLC_ADD_PLUGINS([aout_file equalizer])
 VLC_ADD_PLUGINS([i420_rgb i420_yuy2 i422_yuy2 i420_ymga])
+VLC_ADD_PLUGINS([image])
 VLC_ADD_PLUGINS([id3 playlist export sgimb m3u xtag])
 VLC_ADD_PLUGINS([rawvideo blend scale time marq logo])
 VLC_ADD_PLUGINS([wav araw subtitle vobsub adpcm a52sys dtssys au])
index 68397fa16b18e7b16450f2370aa75b854b7721ee..80e85b0f1c6621c231d345308ac90b638cca5bad 100644 (file)
@@ -45,16 +45,27 @@ 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_description( _("PNG video decoder") );
+    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();
 
 /*****************************************************************************
@@ -139,7 +150,7 @@ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
     p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
     p_info = png_create_info_struct( p_png );
     p_end_info = png_create_info_struct( p_png );
+
     png_set_read_fn( p_png, (void *)p_block, user_read );
     png_set_error_fn( p_png, (void *)p_dec, user_error, user_warning );
 
@@ -220,3 +231,119 @@ 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;
+}
diff --git a/modules/video_output/image.c b/modules/video_output/image.c
new file mode 100644 (file)
index 0000000..40d2620
--- /dev/null
@@ -0,0 +1,240 @@
+/*****************************************************************************
+ * image.c : image video output
+ *****************************************************************************
+ * Copyright (C) 2004 VideoLAN
+ * $Id: snapshot.c 8644 2004-09-05 16:53:04Z fkuehne $
+ *
+ * Authors: Clément Stenac <zorglub@videolan.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * 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.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+#include <stdlib.h>
+
+#include <vlc/vlc.h>
+#include <vlc/vout.h>
+#include <vlc/intf.h>
+
+#include "vlc_image.h"
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int  Create    ( vlc_object_t * );
+static void Destroy   ( vlc_object_t * );
+
+static int  Init      ( vout_thread_t * );
+static void End       ( vout_thread_t *p_vout );
+static void Display   ( vout_thread_t *, picture_t * );
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+#define FORMAT_TEXT N_( "Image format" )
+#define FORMAT_LONGTEXT N_( "Set the format of the output image." )
+
+#define RATIO_TEXT N_( "Recording ratio" )
+#define RATIO_LONGTEXT N_( "Set the ratio of images that are recorded. "\
+                           "3 means that one image out of three is recorded." )
+
+#define PREFIX_TEXT N_( "Filename prefix" )
+#define PREFIX_LONGTEXT N_( "Set the prefix of the filename. Output filename "\
+                            "will have the form prefixNUMBER.format" )
+
+static char *psz_format_list[] = { "png" };
+static char *psz_format_list_text[] = { N_("PNG") };
+
+vlc_module_begin( );
+    set_shortname( _( "Image" ) );
+    set_description( _( "Image video output" ) );
+    set_category( CAT_VIDEO );
+    set_subcategory( SUBCAT_VIDEO_VOUT );
+    set_capability( "video output", 0 );
+
+    add_string( "image-out-format", "png", NULL,  FORMAT_TEXT, FORMAT_LONGTEXT,
+                                                  VLC_FALSE );
+        change_string_list( psz_format_list, psz_format_list_text, 0 );
+    add_integer( "image-out-ratio", 3 , NULL,  RATIO_TEXT, RATIO_LONGTEXT,
+                                                  VLC_FALSE );
+    add_string( "image-out-prefix", "img", NULL, PREFIX_TEXT, PREFIX_LONGTEXT,
+                                                 VLC_FALSE );
+    set_callbacks( Create, Destroy );
+vlc_module_end();
+
+/*****************************************************************************
+ * vout_sys_t: video output descriptor
+ *****************************************************************************/
+struct vout_sys_t
+{
+    char        *psz_prefix;    /* Prefix */
+    int         i_ratio;    /* Image ratio */
+
+    int         i_current;  /* Current image */
+    int         i_frames;  /* Number of frames */
+
+    image_handler_t *p_image;
+};
+
+#define FREE( p ) if( p ) { free( p ); p = NULL; }
+
+/*****************************************************************************
+ * Create: allocates video thread
+ *****************************************************************************
+ * This function allocates and initializes a vout method.
+ *****************************************************************************/
+static int Create( vlc_object_t *p_this )
+{
+    vout_thread_t *p_vout = ( vout_thread_t * )p_this;
+
+    /* Allocate instance and initialize some members */
+    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
+    if( ! p_vout->p_sys )
+        return VLC_ENOMEM;
+
+    p_vout->p_sys->psz_prefix =
+            var_CreateGetString( p_this, "image-out-prefix" );
+    p_vout->p_sys->i_ratio =
+            var_CreateGetInteger( p_this, "image-out-ratio" );
+    p_vout->p_sys->i_current = 0;
+    p_vout->p_sys->p_image = image_HandlerCreate( p_vout );
+
+    if( !p_vout->p_sys->p_image )
+    {
+        msg_Err( p_this, "unable to create image handler") ;
+        FREE( p_vout->p_sys->psz_prefix );
+        FREE( p_vout->p_sys );
+        return VLC_EGENERIC;
+    }
+
+    p_vout->pf_init = Init;
+    p_vout->pf_end = End;
+    p_vout->pf_manage = NULL;
+    p_vout->pf_render = Display;
+    p_vout->pf_display = NULL;
+
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * Init: initialize video thread
+ *****************************************************************************/
+static int Init( vout_thread_t *p_vout )
+{
+    int i_index;
+    picture_t *p_pic;
+
+    /* Initialize the output structure */
+    p_vout->output.i_chroma = VLC_FOURCC( 'R', 'V', '2', '4' );
+    p_vout->output.pf_setpalette = NULL;
+    p_vout->output.i_width = p_vout->render.i_width;
+    p_vout->output.i_height = p_vout->render.i_height;
+    p_vout->output.i_aspect = p_vout->output.i_width
+                               * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
+
+    p_vout->output.i_rmask = 0xff0000;
+    p_vout->output.i_gmask = 0x00ff00;
+    p_vout->output.i_bmask = 0x0000ff;
+
+    /* Try to initialize 1 direct buffer */
+    p_pic = NULL;
+
+    /* Find an empty picture slot */
+    for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
+    {
+        if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
+        {
+            p_pic = p_vout->p_picture + i_index;
+            break;
+        }
+    }
+
+    /* Allocate the picture */
+    if( p_pic == NULL )
+    {
+        return VLC_EGENERIC;
+    }
+
+    vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
+                          p_vout->output.i_width, p_vout->output.i_height,
+                          p_vout->output.i_aspect );
+
+    if( p_pic->i_planes == 0 )
+    {
+        return VLC_EGENERIC;
+    }
+
+    p_pic->i_status = DESTROYED_PICTURE;
+    p_pic->i_type   = DIRECT_PICTURE;
+
+    PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
+    I_OUTPUTPICTURES++;
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * Destroy: destroy video thread
+ *****************************************************************************
+ * Terminate an output method created by Create
+ *****************************************************************************/
+static void Destroy( vlc_object_t *p_this )
+{
+    vout_thread_t *p_vout = ( vout_thread_t * )p_this;
+
+    /* Destroy structure */
+    image_HandlerDelete( p_vout->p_sys->p_image );
+    FREE( p_vout->p_sys->psz_prefix );
+    FREE( p_vout->p_sys );
+}
+
+/*****************************************************************************
+ * Display: displays previously rendered output
+ *****************************************************************************
+ * This function copies the rendered picture into our circular buffer.
+ *****************************************************************************/
+static void Display( vout_thread_t *p_vout, picture_t *p_pic )
+{
+    video_format_t fmt_in = {0}, fmt_out = {0};
+
+    char *psz_filename;
+
+    if( p_vout->p_sys->i_frames % p_vout->p_sys->i_ratio != 0 )
+    {
+        p_vout->p_sys->i_frames++;
+        return;
+    }
+    p_vout->p_sys->i_frames++;
+    fmt_out.i_chroma = VLC_FOURCC( 'p', 'n', 'g', ' ');
+    psz_filename = (char *)malloc( strlen( p_vout->p_sys->psz_prefix ) +
+                                         10 );
+
+    p_vout->p_sys->i_current++;
+
+    sprintf( psz_filename, "%s%.6i.%s", p_vout->p_sys->psz_prefix,
+                                      p_vout->p_sys->i_current,
+                                      "png" );
+
+    image_WriteUrl( p_vout->p_sys->p_image, p_pic,
+                    &fmt_in, &fmt_out, psz_filename ) ;
+    free( psz_filename );
+    return;
+}
+
+
+static void End( vout_thread_t *p_vout )
+{
+}