motionblur.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:13k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * motion_blur.c : motion blur filter for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
  5.  * $Id: motionblur.c 8551 2004-08-28 17:36:02Z gbazin $
  6.  *
  7.  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 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 General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #include <stdlib.h>                                      /* malloc(), free() */
  27. #include <string.h>
  28. #include <vlc/vlc.h>
  29. #include <vlc/vout.h>
  30. #include "filter_common.h"
  31. /*****************************************************************************
  32.  * Local protypes
  33.  *****************************************************************************/
  34. static int  Create    ( vlc_object_t * );
  35. static void Destroy   ( vlc_object_t * );
  36. static int  Init      ( vout_thread_t * );
  37. static void End       ( vout_thread_t * );
  38. static void Render    ( vout_thread_t *, picture_t * );
  39. static void RenderBlur    ( vout_thread_t *, picture_t *, picture_t *, picture_t * );
  40. static void CopyPicture ( vout_thread_t*, picture_t *, picture_t * );
  41. static int  SendEvents( vlc_object_t *, char const *,
  42.                         vlc_value_t, vlc_value_t, void * );
  43. /*****************************************************************************
  44.  * Module descriptor
  45.  *****************************************************************************/
  46. #define MODE_TEXT N_("Blur factor (1-127)")
  47. #define MODE_LONGTEXT N_("The degree of blurring from 1 to 127.")
  48. vlc_module_begin();
  49.     set_description( _("Motion blur filter") );
  50.     set_capability( "video filter", 0 );
  51.     add_integer_with_range( "blur-factor", 80, 1, 127, NULL,
  52.         MODE_TEXT, MODE_LONGTEXT, VLC_FALSE );
  53.     
  54.     set_callbacks( Create, Destroy );
  55. vlc_module_end();
  56. /*****************************************************************************
  57.  * vout_sys_t: Deinterlace video output method descriptor
  58.  *****************************************************************************
  59.  * This structure is part of the video output thread descriptor.
  60.  * It describes the Deinterlace specific properties of an output thread.
  61.  *****************************************************************************/
  62. struct vout_sys_t
  63. {
  64.     int        i_factor;        /* Deinterlace mode */
  65.     vlc_bool_t b_double_rate; /* Shall we double the framerate? */
  66.     mtime_t    last_date;
  67.     mtime_t    next_date;
  68.     vout_thread_t *p_vout;
  69.     picture_t *p_lastpic;
  70. };
  71. /*****************************************************************************
  72.  * Control: control facility for the vout (forwards to child vout)
  73.  *****************************************************************************/
  74. static int Control( vout_thread_t *p_vout, int i_query, va_list args )
  75. {
  76.     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
  77. }
  78. /*****************************************************************************
  79.  * Create: allocates Deinterlace video thread output method
  80.  *****************************************************************************
  81.  * This function allocates and initializes a Deinterlace vout method.
  82.  *****************************************************************************/
  83. static int Create( vlc_object_t *p_this )
  84. {
  85.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  86.     /* Allocate structure */
  87.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  88.     if( p_vout->p_sys == NULL )
  89.     {
  90.         msg_Err( p_vout, "out of memory" );
  91.         return VLC_ENOMEM;
  92.     }
  93.     p_vout->pf_init = Init;
  94.     p_vout->pf_end = End;
  95.     p_vout->pf_manage = NULL;
  96.     p_vout->pf_render = Render;
  97.     p_vout->pf_display = NULL;
  98.     p_vout->pf_control = Control;
  99.     p_vout->p_sys->i_factor = config_GetInt( p_vout, "blur-factor" );
  100.     p_vout->p_sys->b_double_rate = 0;
  101.     p_vout->p_sys->last_date = 0;
  102.     p_vout->p_sys->p_lastpic = NULL;
  103.     return VLC_SUCCESS;
  104. }
  105. /*****************************************************************************
  106.  * Init: initialize Deinterlace video thread output method
  107.  *****************************************************************************/
  108. static int Init( vout_thread_t *p_vout )
  109. {
  110.     int i_index;
  111.     picture_t *p_pic;
  112.     I_OUTPUTPICTURES = 0;
  113.     /* Initialize the output structure, full of directbuffers since we want
  114.      * the decoder to output directly to our structures. */
  115.     switch( p_vout->render.i_chroma )
  116.     {
  117.         case VLC_FOURCC('I','4','2','0'):
  118.         case VLC_FOURCC('I','Y','U','V'):
  119.         case VLC_FOURCC('Y','V','1','2'):
  120.         case VLC_FOURCC('I','4','2','2'):
  121.             p_vout->output.i_chroma = p_vout->render.i_chroma;
  122.             p_vout->output.i_width  = p_vout->render.i_width;
  123.             p_vout->output.i_height = p_vout->render.i_height;
  124.             p_vout->output.i_aspect = p_vout->render.i_aspect;
  125.             break;
  126.         default:
  127.             return VLC_EGENERIC; /* unknown chroma */
  128.             break;
  129.     }
  130.     msg_Dbg( p_vout, "spawning the real video output" );
  131.     switch( p_vout->render.i_chroma )
  132.     {
  133.     case VLC_FOURCC('I','4','2','0'):
  134.     case VLC_FOURCC('I','Y','U','V'):
  135.     case VLC_FOURCC('Y','V','1','2'):
  136.         p_vout->p_sys->p_vout = vout_Create( p_vout,
  137.                            p_vout->output.i_width, p_vout->output.i_height,
  138.                            p_vout->output.i_chroma, p_vout->output.i_aspect );
  139.         break;
  140.     default:
  141.         break;
  142.     }
  143.     /* Everything failed */
  144.     if( p_vout->p_sys->p_vout == NULL )
  145.     {
  146.         msg_Err( p_vout, "cannot open vout, aborting" );
  147.         return VLC_EGENERIC;
  148.     }
  149.     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
  150.     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
  151.     ADD_PARENT_CALLBACKS( SendEventsToChild );
  152.     return VLC_SUCCESS;
  153. }
  154. /*****************************************************************************
  155.  * End: terminate Deinterlace video thread output method
  156.  *****************************************************************************/
  157. static void End( vout_thread_t *p_vout )
  158. {
  159.     int i_index;
  160.     /* Free the fake output buffers we allocated */
  161.     for( i_index = I_OUTPUTPICTURES ; i_index ; )
  162.     {
  163.         i_index--;
  164.         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
  165.     }
  166. }
  167. /*****************************************************************************
  168.  * Destroy: destroy Deinterlace video thread output method
  169.  *****************************************************************************
  170.  * Terminate an output method created by DeinterlaceCreateOutputMethod
  171.  *****************************************************************************/
  172. static void Destroy( vlc_object_t *p_this )
  173. {
  174.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  175.     if( p_vout->p_sys->p_vout )
  176.     {
  177.         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
  178.         vlc_object_detach( p_vout->p_sys->p_vout );
  179.         vout_Destroy( p_vout->p_sys->p_vout );
  180.     }
  181.     DEL_PARENT_CALLBACKS( SendEventsToChild );
  182.     free( p_vout->p_sys );
  183. }
  184. /*****************************************************************************
  185.  * Render: displays previously rendered output
  186.  *****************************************************************************
  187.  * This function send the currently rendered image to Deinterlace image,
  188.  * waits until it is displayed and switch the two rendering buffers, preparing
  189.  * next frame.
  190.  *****************************************************************************/
  191. static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
  192. {
  193.     picture_t * p_outpic;
  194.     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
  195.            == NULL )
  196.     {
  197.         if( p_vout->b_die || p_vout->b_error )
  198.         {
  199.             return;
  200.         }
  201.         msleep( VOUT_OUTMEM_SLEEP );
  202.     }
  203.     vout_DatePicture( p_vout, p_outpic, p_pic->date );
  204.     if ( p_vout->p_sys->p_lastpic == NULL )
  205.     {
  206.         /* Get a new picture */
  207.         while( ( p_vout->p_sys->p_lastpic =
  208.                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
  209.                == NULL )
  210.         {
  211.             if( p_vout->b_die || p_vout->b_error )
  212.             {
  213.                 return;
  214.             }
  215.             msleep( VOUT_OUTMEM_SLEEP );
  216.         }
  217.         CopyPicture( p_vout, p_vout->p_sys->p_lastpic, p_pic );
  218.         CopyPicture( p_vout, p_outpic, p_pic );
  219.         vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
  220.         return;
  221.     }
  222.     /* Get a new picture */
  223.     RenderBlur( p_vout, p_vout->p_sys->p_lastpic, p_pic, p_outpic );
  224.     vout_DestroyPicture( p_vout, p_vout->p_sys->p_lastpic );
  225.     /* Get a new picture */
  226.     while( ( p_vout->p_sys->p_lastpic =
  227.              vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
  228.            == NULL )
  229.     {
  230.         if( p_vout->b_die || p_vout->b_error )
  231.         {
  232.             return;
  233.         }
  234.         msleep( VOUT_OUTMEM_SLEEP );
  235.     }
  236.     CopyPicture( p_vout, p_vout->p_sys->p_lastpic, p_outpic );
  237.     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
  238. }
  239. /* FIXME: this is a verbatim copy from src/video_output/vout_pictures.c */
  240. /* XXX: the order is fucked up!! */
  241. static void CopyPicture( vout_thread_t * p_vout,
  242.                          picture_t *p_dest, picture_t *p_src )
  243. {
  244.     int i;
  245.     for( i = 0; i < p_src->i_planes ; i++ )
  246.     {
  247.         if( p_src->p[i].i_pitch == p_dest->p[i].i_pitch )
  248.         {
  249.             /* There are margins, but with the same width : perfect ! */
  250.             p_vout->p_vlc->pf_memcpy(
  251.                          p_dest->p[i].p_pixels, p_src->p[i].p_pixels,
  252.                          p_src->p[i].i_pitch * p_src->p[i].i_visible_lines );
  253.         }
  254.         else
  255.         {
  256.             /* We need to proceed line by line */
  257.             uint8_t *p_in = p_src->p[i].p_pixels;
  258.             uint8_t *p_out = p_dest->p[i].p_pixels;
  259.             int i_line;
  260.             for( i_line = p_src->p[i].i_visible_lines; i_line--; )
  261.             {
  262.                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
  263.                                           p_src->p[i].i_visible_pitch );
  264.                 p_in += p_src->p[i].i_pitch;
  265.                 p_out += p_dest->p[i].i_pitch;
  266.             }
  267.         }
  268.     }
  269. }
  270. /*****************************************************************************
  271.  * RenderBlur: renders a blurred picture
  272.  *****************************************************************************/
  273. static void RenderBlur( vout_thread_t *p_vout, picture_t *p_oldpic,
  274.                         picture_t *p_newpic, picture_t *p_outpic )
  275. {
  276.     int i_plane;
  277.     int i_oldfactor = p_vout->p_sys->i_factor;
  278.     int i_newfactor = 128 - p_vout->p_sys->i_factor;
  279.     for( i_plane = 0; i_plane < p_outpic->i_planes; i_plane++ )
  280.     {
  281.         uint8_t *p_old, *p_new, *p_out, *p_out_end, *p_out_line_end;
  282.         p_out = p_outpic->p[i_plane].p_pixels;
  283.         p_new = p_newpic->p[i_plane].p_pixels;
  284.         p_old = p_oldpic->p[i_plane].p_pixels;
  285.         p_out_end = p_out + p_outpic->p[i_plane].i_pitch *
  286.                              p_outpic->p[i_plane].i_visible_lines;
  287.         while ( p_out < p_out_end )
  288.         {
  289.             p_out_line_end = p_out + p_outpic->p[i_plane].i_visible_pitch;
  290.             while ( p_out < p_out_line_end )
  291.             {
  292.                 *p_out++ = (((*p_old++) * i_oldfactor) +
  293.                             ((*p_new++) * i_newfactor)) >> 7;
  294. //                *p_out++ = (*p_old++ >> 1) + (*p_new++ >> 1);
  295.             }
  296.             p_old += p_oldpic->p[i_plane].i_pitch
  297.                       - p_oldpic->p[i_plane].i_visible_pitch;
  298.             p_new += p_newpic->p[i_plane].i_pitch
  299.                       - p_newpic->p[i_plane].i_visible_pitch;
  300.             p_out += p_outpic->p[i_plane].i_pitch
  301.                       - p_outpic->p[i_plane].i_visible_pitch;
  302.         }
  303.     }
  304. }
  305. /*****************************************************************************
  306.  * SendEvents: forward mouse and keyboard events to the parent p_vout
  307.  *****************************************************************************/
  308. static int SendEvents( vlc_object_t *p_this, char const *psz_var,
  309.                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
  310. {
  311.     var_Set( (vlc_object_t *)p_data, psz_var, newval );
  312.     return VLC_SUCCESS;
  313. }
  314. /*****************************************************************************
  315.  * SendEventsToChild: forward events to the child/children vout
  316.  *****************************************************************************/
  317. static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
  318.                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
  319. {
  320.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  321.     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
  322.     return VLC_SUCCESS;
  323. }