postproc.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:14k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * postproc.c: video postprocessing using libpostproc
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2009 the VideoLAN team
  5.  * $Id: 08fc1c12f781582107472edc468bd7c8536d782e $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *          Gildas Bazin <gbazin@netcourrier.com>
  9.  *          Antoine Cellerier <dionoea at videolan dot org>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24.  *****************************************************************************/
  25. #ifdef HAVE_CONFIG_H
  26. # include "config.h"
  27. #endif
  28. #include <vlc_common.h>
  29. #include <vlc_plugin.h>
  30. #include <vlc_vout.h>
  31. #include "filter_picture.h"
  32. #ifdef HAVE_POSTPROC_POSTPROCESS_H
  33. #   include <postproc/postprocess.h>
  34. #else
  35. #   include <libpostproc/postprocess.h>
  36. #endif
  37. #ifndef PP_CPU_CAPS_ALTIVEC
  38. #   define PP_CPU_CAPS_ALTIVEC 0
  39. #endif
  40. /*****************************************************************************
  41.  * Local prototypes
  42.  *****************************************************************************/
  43. static int OpenPostproc( vlc_object_t * );
  44. static void ClosePostproc( vlc_object_t * );
  45. static picture_t *PostprocPict( filter_t *, picture_t * );
  46. static int PPQCallback( vlc_object_t *, char const *,
  47.                         vlc_value_t, vlc_value_t, void * );
  48. static int PPNameCallback( vlc_object_t *, char const *,
  49.                            vlc_value_t, vlc_value_t, void * );
  50. #define Q_TEXT N_("Post processing quality")
  51. #define Q_LONGTEXT N_( 
  52.     "Quality of post processing. Valid range is 0 to 6n" 
  53.     "Higher levels require considerable more CPU power, but produce " 
  54.     "better looking pictures." )
  55. #define NAME_TEXT N_("FFmpeg post processing filter chains")
  56. #define NAME_LONGTEXT NAME_TEXT
  57. #define FILTER_PREFIX "postproc-"
  58. /*****************************************************************************
  59.  * Module descriptor
  60.  *****************************************************************************/
  61. vlc_module_begin ()
  62.     set_description( N_("Video post processing filter") )
  63.     set_shortname( N_("Postproc" ) )
  64.     add_shortcut( "postprocess" ) /* name is "postproc" */
  65.     add_shortcut( "pp" )
  66.     set_category( CAT_VIDEO )
  67.     set_subcategory( SUBCAT_VIDEO_VFILTER )
  68.     set_capability( "video filter2", 0 )
  69.     set_callbacks( OpenPostproc, ClosePostproc )
  70.     add_integer_with_range( FILTER_PREFIX "q", PP_QUALITY_MAX, 0,
  71.                             PP_QUALITY_MAX, NULL, Q_TEXT, Q_LONGTEXT, false )
  72.         add_deprecated_alias( "ffmpeg-pp-q" )
  73.         change_safe()
  74.     add_string( FILTER_PREFIX "name", "default", NULL, NAME_TEXT,
  75.                 NAME_LONGTEXT, true )
  76.         add_deprecated_alias( "ffmpeg-pp-name" )
  77. vlc_module_end ()
  78. static const char *const ppsz_filter_options[] = {
  79.     "q", "name", NULL
  80. };
  81. /*****************************************************************************
  82.  * filter_sys_t : libpostproc video postprocessing descriptor
  83.  *****************************************************************************/
  84. struct filter_sys_t
  85. {
  86.     /* Never changes after init */
  87.     pp_context_t *pp_context;
  88.     /* Set to NULL if post processing is disabled */
  89.     pp_mode_t    *pp_mode;
  90.     /* Set to true if previous pic had a quant matrix
  91.        (used to prevent spamming warning messages) */
  92.     bool b_had_matrix;
  93.     /* Lock when using or changing pp_mode */
  94.     vlc_mutex_t lock;
  95. };
  96. /*****************************************************************************
  97.  * OpenPostproc: probe and open the postproc
  98.  *****************************************************************************/
  99. static int OpenPostproc( vlc_object_t *p_this )
  100. {
  101.     filter_t *p_filter = (filter_t *)p_this;
  102.     filter_sys_t *p_sys;
  103.     vlc_value_t val, val_orig, text;
  104.     unsigned i_cpu = vlc_CPU();
  105.     int i_flags = 0;
  106.     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma ||
  107.         p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height ||
  108.         p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width )
  109.     {
  110.         msg_Err( p_filter, "Filter input and output formats must be identical" );
  111.         return VLC_EGENERIC;
  112.     }
  113.     /* Set CPU capabilities */
  114.     if( i_cpu & CPU_CAPABILITY_MMX )
  115.         i_flags |= PP_CPU_CAPS_MMX;
  116.     if( i_cpu & CPU_CAPABILITY_MMXEXT )
  117.         i_flags |= PP_CPU_CAPS_MMX2;
  118.     if( i_cpu & CPU_CAPABILITY_3DNOW )
  119.         i_flags |= PP_CPU_CAPS_3DNOW;
  120.     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
  121.         i_flags |= PP_CPU_CAPS_ALTIVEC;
  122.     switch( p_filter->fmt_in.video.i_chroma )
  123.     {
  124.         case VLC_FOURCC('I','4','4','4'):
  125.         case VLC_FOURCC('J','4','4','4'):
  126.         /* case VLC_CODEC_YUVA:
  127.            FIXME: Should work but alpha plane needs to be copied manually and
  128.                   I'm kind of feeling too lazy to write the code to do that ATM
  129.                   (i_pitch vs i_visible_pitch...). */
  130.             i_flags |= PP_FORMAT_444;
  131.             break;
  132.         case VLC_FOURCC('I','4','2','2'):
  133.         case VLC_FOURCC('J','4','2','2'):
  134.             i_flags |= PP_FORMAT_422;
  135.             break;
  136.         case VLC_FOURCC('I','4','1','1'):
  137.             i_flags |= PP_FORMAT_411;
  138.             break;
  139.         case VLC_FOURCC('I','4','2','0'):
  140.         case VLC_FOURCC('I','Y','U','V'):
  141.         case VLC_FOURCC('J','4','2','0'):
  142.         case VLC_FOURCC('Y','V','1','2'):
  143.             i_flags |= PP_FORMAT_420;
  144.             break;
  145.         default:
  146.             msg_Err( p_filter, "Unsupported input chroma (%4s)",
  147.                       (char*)&p_filter->fmt_in.video.i_chroma );
  148.             return VLC_EGENERIC;
  149.     }
  150.     p_sys = malloc( sizeof( filter_sys_t ) );
  151.     if( !p_sys ) return VLC_ENOMEM;
  152.     p_filter->p_sys = p_sys;
  153.     p_sys->pp_context = pp_get_context( p_filter->fmt_in.video.i_width,
  154.                                         p_filter->fmt_in.video.i_height,
  155.                                         i_flags );
  156.     if( !p_sys->pp_context )
  157.     {
  158.         msg_Err( p_filter, "Error while creating post processing context." );
  159.         free( p_sys );
  160.         return VLC_EGENERIC;
  161.     }
  162.     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
  163.                        p_filter->p_cfg );
  164.     var_Create( p_filter, FILTER_PREFIX "q",
  165.                 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT |
  166.                 VLC_VAR_ISCOMMAND );
  167.     /* For some obscure reason the VLC_VAR_ISCOMMAND isn't taken into account
  168.        in during var_Create */
  169.     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_SETISCOMMAND, NULL, NULL );
  170.     text.psz_string = _("Post processing");
  171.     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_SETTEXT, &text, NULL );
  172.     var_Get( p_filter, FILTER_PREFIX "q", &val_orig );
  173.     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_DELCHOICE, &val_orig, NULL );
  174.     val.psz_string = var_CreateGetNonEmptyStringCommand(
  175.                                             p_filter, FILTER_PREFIX "name" );
  176.     if( val_orig.i_int )
  177.     {
  178.         p_sys->pp_mode = pp_get_mode_by_name_and_quality( val.psz_string ?
  179.                                                           val.psz_string :
  180.                                                           "default",
  181.                                                           val_orig.i_int );
  182.         if( !p_sys->pp_mode )
  183.         {
  184.             msg_Err( p_filter, "Error while creating post processing mode." );
  185.             free( val.psz_string );
  186.             var_Destroy( p_filter, FILTER_PREFIX "q" );
  187.             pp_free_context( p_sys->pp_context );
  188.             free( p_sys );
  189.             return VLC_EGENERIC;
  190.         }
  191.     }
  192.     else
  193.     {
  194.         p_sys->pp_mode = NULL;
  195.     }
  196.     free( val.psz_string );
  197.     for( val.i_int = 0; val.i_int <= PP_QUALITY_MAX; val.i_int++ )
  198.     {
  199.         switch( val.i_int )
  200.         {
  201.             case 0:
  202.                 text.psz_string = _("Disable");
  203.                 break;
  204.             case 1:
  205.                 text.psz_string = _("Lowest");
  206.                 break;
  207.             case PP_QUALITY_MAX:
  208.                 text.psz_string = _("Highest");
  209.                 break;
  210.             default:
  211.                 text.psz_string = NULL;
  212.                 break;
  213.         }
  214.         var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_ADDCHOICE,
  215.                     &val, text.psz_string?&text:NULL );
  216.     }
  217.     vlc_mutex_init( &p_sys->lock );
  218.     /* Add the callback at the end to prevent crashes */
  219.     var_AddCallback( p_filter, FILTER_PREFIX "q", PPQCallback, NULL );
  220.     var_AddCallback( p_filter, FILTER_PREFIX "name", PPNameCallback, NULL );
  221.     p_filter->pf_video_filter = PostprocPict;
  222.     p_sys->b_had_matrix = true;
  223.     return VLC_SUCCESS;
  224. }
  225. /*****************************************************************************
  226.  * ClosePostproc
  227.  *****************************************************************************/
  228. static void ClosePostproc( vlc_object_t *p_this )
  229. {
  230.     filter_t *p_filter = (filter_t *)p_this;
  231.     filter_sys_t *p_sys = p_filter->p_sys;
  232.     /* delete the callback before destroying the mutex */
  233.     var_DelCallback( p_filter, FILTER_PREFIX "q", PPQCallback, NULL );
  234.     var_DelCallback( p_filter, FILTER_PREFIX "name", PPNameCallback, NULL );
  235.     /* Destroy the resources */
  236.     vlc_mutex_destroy( &p_sys->lock );
  237.     pp_free_context( p_sys->pp_context );
  238.     if( p_sys->pp_mode ) pp_free_mode( p_sys->pp_mode );
  239.     free( p_sys );
  240. }
  241. /*****************************************************************************
  242.  * PostprocPict
  243.  *****************************************************************************/
  244. static picture_t *PostprocPict( filter_t *p_filter, picture_t *p_pic )
  245. {
  246.     filter_sys_t *p_sys = p_filter->p_sys;
  247.     const uint8_t *src[3];
  248.     uint8_t *dst[3];
  249.     int i_plane;
  250.     int i_src_stride[3], i_dst_stride[3];
  251.     /* Lock to prevent issues if pp_mode is changed */
  252.     vlc_mutex_lock( &p_sys->lock );
  253.     if( !p_sys->pp_mode )
  254.     {
  255.         vlc_mutex_unlock( &p_sys->lock );
  256.         return p_pic;
  257.     }
  258.     picture_t *p_outpic = filter_NewPicture( p_filter );
  259.     if( !p_outpic )
  260.     {
  261.         picture_Release( p_pic );
  262.         vlc_mutex_unlock( &p_sys->lock );
  263.         return NULL;
  264.     }
  265.     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
  266.     {
  267.         src[i_plane] = p_pic->p[i_plane].p_pixels;
  268.         dst[i_plane] = p_outpic->p[i_plane].p_pixels;
  269.         /* I'm not sure what happens if i_pitch != i_visible_pitch ...
  270.          * at least it shouldn't crash. */
  271.         i_src_stride[i_plane] = p_pic->p[i_plane].i_pitch;
  272.         i_dst_stride[i_plane] = p_outpic->p[i_plane].i_pitch;
  273.     }
  274.     if( !p_pic->p_q && p_sys->b_had_matrix )
  275.     {
  276.         msg_Warn( p_filter, "Quantification table was not set by video decoder. Postprocessing won't look good." );
  277.         p_sys->b_had_matrix = false;
  278.     }
  279.     else if( p_pic->p_q )
  280.     {
  281.         p_sys->b_had_matrix = true;
  282.     }
  283.     pp_postprocess( src, i_src_stride, dst, i_dst_stride,
  284.                     p_filter->fmt_in.video.i_width,
  285.                     p_filter->fmt_in.video.i_height,
  286.                     p_pic->p_q, p_pic->i_qstride,
  287.                     p_sys->pp_mode, p_sys->pp_context,
  288.                     p_pic->i_qtype == QTYPE_MPEG2 ? PP_PICT_TYPE_QP2 : 0 );
  289.     vlc_mutex_unlock( &p_sys->lock );
  290.     return CopyInfoAndRelease( p_outpic, p_pic );
  291. }
  292. /*****************************************************************************
  293.  * PPChangeMode: change the current mode and quality
  294.  *****************************************************************************/
  295. static void PPChangeMode( filter_t *p_filter, const char *psz_name,
  296.                           int i_quality )
  297. {
  298.     filter_sys_t *p_sys = p_filter->p_sys;
  299.     vlc_mutex_lock( &p_sys->lock );
  300.     if( i_quality > 0 )
  301.     {
  302.         pp_mode_t *pp_mode = pp_get_mode_by_name_and_quality( psz_name ?
  303.                                                               psz_name :
  304.                                                               "default",
  305.                                                               i_quality );
  306.         if( pp_mode )
  307.         {
  308.             pp_free_mode( p_sys->pp_mode );
  309.             p_sys->pp_mode = pp_mode;
  310.         }
  311.         else
  312.             msg_Warn( p_filter, "Error while changing post processing mode. "
  313.                       "Keeping previous mode." );
  314.     }
  315.     else
  316.     {
  317.         pp_free_mode( p_sys->pp_mode );
  318.         p_sys->pp_mode = NULL;
  319.     }
  320.     vlc_mutex_unlock( &p_sys->lock );
  321. }
  322. static int PPQCallback( vlc_object_t *p_this, const char *psz_var,
  323.                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
  324. {
  325.     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
  326.     filter_t *p_filter = (filter_t *)p_this;
  327.     char *psz_name = var_GetNonEmptyString( p_filter, FILTER_PREFIX "name" );
  328.     PPChangeMode( p_filter, psz_name, newval.i_int );
  329.     free( psz_name );
  330.     return VLC_SUCCESS;
  331. }
  332. static int PPNameCallback( vlc_object_t *p_this, const char *psz_var,
  333.                            vlc_value_t oldval, vlc_value_t newval, void *p_data )
  334. {
  335.     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
  336.     filter_t *p_filter = (filter_t *)p_this;
  337.     int i_quality = var_GetInteger( p_filter, FILTER_PREFIX "q" );
  338.     PPChangeMode( p_filter, *newval.psz_string ? newval.psz_string : NULL,
  339.                   i_quality );
  340.     return VLC_SUCCESS;
  341. }