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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * video_output.c : video output thread
  3.  *
  4.  * This module describes the programming interface for video output threads.
  5.  * It includes functions allowing to open a new thread, send pictures to a
  6.  * thread, and destroy a previously oppened video output thread.
  7.  *****************************************************************************
  8.  * Copyright (C) 2000-2007 the VideoLAN team
  9.  * $Id: 79791f17e60279d89ced02831c36ee8067a9a523 $
  10.  *
  11.  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  12.  *          Gildas Bazin <gbazin@videolan.org>
  13.  *
  14.  * This program is free software; you can redistribute it and/or modify
  15.  * it under the terms of the GNU General Public License as published by
  16.  * the Free Software Foundation; either version 2 of the License, or
  17.  * (at your option) any later version.
  18.  *
  19.  * This program is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  * GNU General Public License for more details.
  23.  *
  24.  * You should have received a copy of the GNU General Public License
  25.  * along with this program; if not, write to the Free Software
  26.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  27.  *****************************************************************************/
  28. /*****************************************************************************
  29.  * Preamble
  30.  *****************************************************************************/
  31. #ifdef HAVE_CONFIG_H
  32. # include "config.h"
  33. #endif
  34. #include <vlc_common.h>
  35. #include <stdlib.h>                                                /* free() */
  36. #include <string.h>
  37. #ifdef HAVE_SYS_TIMES_H
  38. #   include <sys/times.h>
  39. #endif
  40. #include <vlc_vout.h>
  41. #include <vlc_filter.h>
  42. #include <vlc_osd.h>
  43. #include <assert.h>
  44. #if defined( __APPLE__ )
  45. /* Include darwin_specific.h here if needed */
  46. #endif
  47. /** FIXME This is quite ugly but needed while we don't have counters
  48.  * helpers */
  49. //#include "input/input_internal.h"
  50. #include <libvlc.h>
  51. #include <vlc_input.h>
  52. #include "vout_pictures.h"
  53. #include "vout_internal.h"
  54. /*****************************************************************************
  55.  * Local prototypes
  56.  *****************************************************************************/
  57. static int      InitThread        ( vout_thread_t * );
  58. static void*    RunThread         ( void *  );
  59. static void     ErrorThread       ( vout_thread_t * );
  60. static void     CleanThread       ( vout_thread_t * );
  61. static void     EndThread         ( vout_thread_t * );
  62. static void     AspectRatio       ( int, int *, int * );
  63. static void VideoFormatImportRgb( video_format_t *, const picture_heap_t * );
  64. static void PictureHeapFixRgb( picture_heap_t * );
  65. static void     vout_Destructor   ( vlc_object_t * p_this );
  66. /* Object variables callbacks */
  67. static int FilterCallback( vlc_object_t *, char const *,
  68.                            vlc_value_t, vlc_value_t, void * );
  69. static int VideoFilter2Callback( vlc_object_t *, char const *,
  70.                                  vlc_value_t, vlc_value_t, void * );
  71. /* */
  72. static void PostProcessEnable( vout_thread_t * );
  73. static void PostProcessDisable( vout_thread_t * );
  74. static void PostProcessSetFilterQuality( vout_thread_t *p_vout );
  75. static int  PostProcessCallback( vlc_object_t *, char const *,
  76.                                  vlc_value_t, vlc_value_t, void * );
  77. /* */
  78. static void DeinterlaceEnable( vout_thread_t * );
  79. /* From vout_intf.c */
  80. int vout_Snapshot( vout_thread_t *, picture_t * );
  81. /* Display media title in OSD */
  82. static void DisplayTitleOnOSD( vout_thread_t *p_vout );
  83. /* Time during which the thread will sleep if it has nothing to
  84.  * display (in micro-seconds) */
  85. #define VOUT_IDLE_SLEEP                 ((int)(0.020*CLOCK_FREQ))
  86. /* Maximum lap of time allowed between the beginning of rendering and
  87.  * display. If, compared to the current date, the next image is too
  88.  * late, the thread will perform an idle loop. This time should be
  89.  * at least VOUT_IDLE_SLEEP plus the time required to render a few
  90.  * images, to avoid trashing of decoded images */
  91. #define VOUT_DISPLAY_DELAY              ((int)(0.200*CLOCK_FREQ))
  92. /* Better be in advance when awakening than late... */
  93. #define VOUT_MWAIT_TOLERANCE            ((mtime_t)(0.020*CLOCK_FREQ))
  94. /* Minimum number of direct pictures the video output will accept without
  95.  * creating additional pictures in system memory */
  96. #ifdef OPTIMIZE_MEMORY
  97. #   define VOUT_MIN_DIRECT_PICTURES        (VOUT_MAX_PICTURES/2)
  98. #else
  99. #   define VOUT_MIN_DIRECT_PICTURES        (3*VOUT_MAX_PICTURES/4)
  100. #endif
  101. /*****************************************************************************
  102.  * Video Filter2 functions
  103.  *****************************************************************************/
  104. static picture_t *video_new_buffer_filter( filter_t *p_filter )
  105. {
  106.     vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
  107.     picture_t *p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
  108.     p_picture->i_status = READY_PICTURE;
  109.     return p_picture;
  110. }
  111. static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
  112. {
  113.     vout_thread_t *p_vout = (vout_thread_t*)p_filter->p_owner;
  114.     vlc_mutex_lock( &p_vout->picture_lock );
  115.     vout_UsePictureLocked( p_vout, p_pic );
  116.     vlc_mutex_unlock( &p_vout->picture_lock );
  117. }
  118. static int video_filter_buffer_allocation_init( filter_t *p_filter, void *p_data )
  119. {
  120.     p_filter->pf_vout_buffer_new = video_new_buffer_filter;
  121.     p_filter->pf_vout_buffer_del = video_del_buffer_filter;
  122.     p_filter->p_owner = p_data; /* p_vout */
  123.     return VLC_SUCCESS;
  124. }
  125. /*****************************************************************************
  126.  * vout_Request: find a video output thread, create one, or destroy one.
  127.  *****************************************************************************
  128.  * This function looks for a video output thread matching the current
  129.  * properties. If not found, it spawns a new one.
  130.  *****************************************************************************/
  131. vout_thread_t *__vout_Request( vlc_object_t *p_this, vout_thread_t *p_vout,
  132.                                video_format_t *p_fmt )
  133. {
  134.     if( !p_fmt )
  135.     {
  136.         /* Video output is no longer used.
  137.          * TODO: support for reusing video outputs with proper _thread-safe_
  138.          * reference handling. */
  139.         if( p_vout )
  140.             vout_CloseAndRelease( p_vout );
  141.         return NULL;
  142.     }
  143.     /* If a video output was provided, lock it, otherwise look for one. */
  144.     if( p_vout )
  145.     {
  146.         vlc_object_hold( p_vout );
  147.     }
  148.     /* TODO: find a suitable unused video output */
  149.     /* If we now have a video output, check it has the right properties */
  150.     if( p_vout )
  151.     {
  152.         vlc_mutex_lock( &p_vout->change_lock );
  153.         /* We don't directly check for the "vout-filter" variable for obvious
  154.          * performance reasons. */
  155.         if( p_vout->p->b_filter_change )
  156.         {
  157.             char *psz_filter_chain = var_GetString( p_vout, "vout-filter" );
  158.             if( psz_filter_chain && !*psz_filter_chain )
  159.             {
  160.                 free( psz_filter_chain );
  161.                 psz_filter_chain = NULL;
  162.             }
  163.             if( p_vout->p->psz_filter_chain && !*p_vout->p->psz_filter_chain )
  164.             {
  165.                 free( p_vout->p->psz_filter_chain );
  166.                 p_vout->p->psz_filter_chain = NULL;
  167.             }
  168.             if( !psz_filter_chain && !p_vout->p->psz_filter_chain )
  169.             {
  170.                 p_vout->p->b_filter_change = false;
  171.             }
  172.             free( psz_filter_chain );
  173.         }
  174.         if( p_vout->fmt_render.i_chroma != p_fmt->i_chroma ||
  175.             p_vout->fmt_render.i_width != p_fmt->i_width ||
  176.             p_vout->fmt_render.i_height != p_fmt->i_height ||
  177.             p_vout->p->b_filter_change )
  178.         {
  179.             vlc_mutex_unlock( &p_vout->change_lock );
  180.             /* We are not interested in this format, close this vout */
  181.             vout_CloseAndRelease( p_vout );
  182.             vlc_object_release( p_vout );
  183.             p_vout = NULL;
  184.         }
  185.         else
  186.         {
  187.             /* This video output is cool! Hijack it. */
  188.             if( p_vout->fmt_render.i_aspect != p_fmt->i_aspect )
  189.             {
  190.                 /* Correct aspect ratio on change
  191.                  * FIXME factorize this code with other aspect ration related code */
  192.                 unsigned int i_sar_num;
  193.                 unsigned int i_sar_den;
  194.                 unsigned int i_aspect;
  195.                 i_aspect = p_fmt->i_aspect;
  196.                 vlc_ureduce( &i_sar_num, &i_sar_den,
  197.                              p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
  198. #if 0
  199.                 /* What's that, it does not seems to be used correcly everywhere
  200.                  * beside the previous p_vout->fmt_render.i_aspect != p_fmt->i_aspect
  201.                  * should be fixed to use it too then */
  202.                 if( p_vout->i_par_num > 0 && p_vout->i_par_den > 0 )
  203.                 {
  204.                     i_sar_num *= p_vout->i_par_den;
  205.                     i_sar_den *= p_vout->i_par_num;
  206.                     i_aspect = i_aspect * p_vout->i_par_den / p_vout->i_par_num;
  207.                 }
  208. #endif
  209.                 if( i_sar_num > 0 && i_sar_den > 0 && i_aspect > 0 )
  210.                 {
  211.                     p_vout->fmt_in.i_sar_num = i_sar_num;
  212.                     p_vout->fmt_in.i_sar_den = i_sar_den;
  213.                     p_vout->fmt_in.i_aspect  = i_aspect;
  214.                     p_vout->fmt_render.i_sar_num = i_sar_num;
  215.                     p_vout->fmt_render.i_sar_den = i_sar_den;
  216.                     p_vout->fmt_render.i_aspect  = i_aspect;
  217.                     p_vout->render.i_aspect   = i_aspect;
  218.                     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
  219.                 }
  220.             }
  221.             vlc_mutex_unlock( &p_vout->change_lock );
  222.             vlc_object_release( p_vout );
  223.         }
  224.         if( p_vout )
  225.         {
  226.             msg_Dbg( p_this, "reusing provided vout" );
  227.             spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
  228.             vlc_object_detach( p_vout );
  229.             vlc_object_attach( p_vout, p_this );
  230.             spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), true );
  231.         }
  232.     }
  233.     if( !p_vout )
  234.     {
  235.         msg_Dbg( p_this, "no usable vout present, spawning one" );
  236.         p_vout = vout_Create( p_this, p_fmt );
  237.     }
  238.     return p_vout;
  239. }
  240. /*****************************************************************************
  241.  * vout_Create: creates a new video output thread
  242.  *****************************************************************************
  243.  * This function creates a new video output thread, and returns a pointer
  244.  * to its description. On error, it returns NULL.
  245.  *****************************************************************************/
  246. vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
  247. {
  248.     vout_thread_t  * p_vout;                            /* thread descriptor */
  249.     int              i_index;                               /* loop variable */
  250.     vlc_value_t      text;
  251.     unsigned int i_width = p_fmt->i_width;
  252.     unsigned int i_height = p_fmt->i_height;
  253.     vlc_fourcc_t i_chroma = p_fmt->i_chroma;
  254.     unsigned int i_aspect = p_fmt->i_aspect;
  255.     config_chain_t *p_cfg;
  256.     char *psz_parser;
  257.     char *psz_name;
  258.     if( i_width <= 0 || i_height <= 0 || i_aspect <= 0 )
  259.         return NULL;
  260.     vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
  261.                  p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
  262.     if( p_fmt->i_sar_num <= 0 || p_fmt->i_sar_den <= 0 )
  263.         return NULL;
  264.     /* Allocate descriptor */
  265.     static const char typename[] = "video output";
  266.     p_vout = vlc_custom_create( p_parent, sizeof( *p_vout ), VLC_OBJECT_VOUT,
  267.                                 typename );
  268.     if( p_vout == NULL )
  269.         return NULL;
  270.     /* */
  271.     p_vout->p = calloc( 1, sizeof(*p_vout->p) );
  272.     if( !p_vout->p )
  273.     {
  274.         vlc_object_release( p_vout );
  275.         return NULL;
  276.     }
  277.     /* Initialize pictures - translation tables and functions
  278.      * will be initialized later in InitThread */
  279.     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
  280.     {
  281.         p_vout->p_picture[i_index].pf_lock = NULL;
  282.         p_vout->p_picture[i_index].pf_unlock = NULL;
  283.         p_vout->p_picture[i_index].i_status = FREE_PICTURE;
  284.         p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
  285.         p_vout->p_picture[i_index].b_slow   = 0;
  286.     }
  287.     /* No images in the heap */
  288.     p_vout->i_heap_size = 0;
  289.     /* Initialize the rendering heap */
  290.     I_RENDERPICTURES = 0;
  291.     p_vout->fmt_render        = *p_fmt;   /* FIXME palette */
  292.     p_vout->fmt_in            = *p_fmt;   /* FIXME palette */
  293.     p_vout->render.i_width    = i_width;
  294.     p_vout->render.i_height   = i_height;
  295.     p_vout->render.i_chroma   = i_chroma;
  296.     p_vout->render.i_aspect   = i_aspect;
  297.     p_vout->render.i_rmask    = p_fmt->i_rmask;
  298.     p_vout->render.i_gmask    = p_fmt->i_gmask;
  299.     p_vout->render.i_bmask    = p_fmt->i_bmask;
  300.     p_vout->render.i_last_used_pic = -1;
  301.     p_vout->render.b_allow_modify_pics = 1;
  302.     /* Zero the output heap */
  303.     I_OUTPUTPICTURES = 0;
  304.     p_vout->output.i_width    = 0;
  305.     p_vout->output.i_height   = 0;
  306.     p_vout->output.i_chroma   = 0;
  307.     p_vout->output.i_aspect   = 0;
  308.     p_vout->output.i_rmask    = 0;
  309.     p_vout->output.i_gmask    = 0;
  310.     p_vout->output.i_bmask    = 0;
  311.     /* Initialize misc stuff */
  312.     p_vout->i_changes    = 0;
  313.     p_vout->b_autoscale  = 1;
  314.     p_vout->i_zoom      = ZOOM_FP_FACTOR;
  315.     p_vout->b_fullscreen = 0;
  316.     p_vout->i_alignment  = 0;
  317.     p_vout->p->render_time  = 10;
  318.     p_vout->p->c_fps_samples = 0;
  319.     p_vout->p->i_picture_lost = 0;
  320.     p_vout->p->i_picture_displayed = 0;
  321.     p_vout->p->b_filter_change = 0;
  322.     p_vout->p->b_paused = false;
  323.     p_vout->p->i_pause_date = 0;
  324.     p_vout->pf_control = NULL;
  325.     p_vout->p->i_par_num =
  326.     p_vout->p->i_par_den = 1;
  327.     p_vout->p->p_picture_displayed = NULL;
  328.     p_vout->p->i_picture_displayed_date = 0;
  329.     p_vout->p->b_picture_displayed = false;
  330.     p_vout->p->b_picture_empty = false;
  331.     p_vout->p->i_picture_qtype = QTYPE_NONE;
  332.     p_vout->p->snapshot.b_available = true;
  333.     p_vout->p->snapshot.i_request = 0;
  334.     p_vout->p->snapshot.p_picture = NULL;
  335.     vlc_mutex_init( &p_vout->p->snapshot.lock );
  336.     vlc_cond_init( &p_vout->p->snapshot.wait );
  337.     /* Initialize locks */
  338.     vlc_mutex_init( &p_vout->picture_lock );
  339.     vlc_cond_init( &p_vout->p->picture_wait );
  340.     vlc_mutex_init( &p_vout->change_lock );
  341.     vlc_mutex_init( &p_vout->p->vfilter_lock );
  342.     /* Mouse coordinates */
  343.     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
  344.     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
  345.     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
  346.     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
  347.     var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
  348.     /* Initialize subpicture unit */
  349.     p_vout->p_spu = spu_Create( p_vout );
  350.     /* Attach the new object now so we can use var inheritance below */
  351.     vlc_object_attach( p_vout, p_parent );
  352.     /* */
  353.     spu_Init( p_vout->p_spu );
  354.     spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), true );
  355.     /* Take care of some "interface/control" related initialisations */
  356.     vout_IntfInit( p_vout );
  357.     /* If the parent is not a VOUT object, that means we are at the start of
  358.      * the video output pipe */
  359.     if( vlc_internals( p_parent )->i_object_type != VLC_OBJECT_VOUT )
  360.     {
  361.         /* Look for the default filter configuration */
  362.         p_vout->p->psz_filter_chain =
  363.             var_CreateGetStringCommand( p_vout, "vout-filter" );
  364.         /* Apply video filter2 objects on the first vout */
  365.         p_vout->p->psz_vf2 =
  366.             var_CreateGetStringCommand( p_vout, "video-filter" );
  367.         p_vout->p->b_first_vout = true;
  368.     }
  369.     else
  370.     {
  371.         /* continue the parent's filter chain */
  372.         char *psz_tmp;
  373.         /* Ugly hack to jump to our configuration chain */
  374.         p_vout->p->psz_filter_chain
  375.             = ((vout_thread_t *)p_parent)->p->psz_filter_chain;
  376.         p_vout->p->psz_filter_chain
  377.             = config_ChainCreate( &psz_tmp, &p_cfg, p_vout->p->psz_filter_chain );
  378.         config_ChainDestroy( p_cfg );
  379.         free( psz_tmp );
  380.         /* Create a video filter2 var ... but don't inherit values */
  381.         var_Create( p_vout, "video-filter",
  382.                     VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
  383.         p_vout->p->psz_vf2 = var_GetString( p_vout, "video-filter" );
  384.         /* */
  385.         p_vout->p->b_first_vout = false;
  386.     }
  387.     var_AddCallback( p_vout, "video-filter", VideoFilter2Callback, NULL );
  388.     p_vout->p->p_vf2_chain = filter_chain_New( p_vout, "video filter2",
  389.         false, video_filter_buffer_allocation_init, NULL, p_vout );
  390.     /* Choose the video output module */
  391.     if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
  392.     {
  393.         psz_parser = var_CreateGetString( p_vout, "vout" );
  394.     }
  395.     else
  396.     {
  397.         psz_parser = strdup( p_vout->p->psz_filter_chain );
  398.         p_vout->p->b_title_show = false;
  399.     }
  400.     /* Create the vout thread */
  401.     char* psz_tmp = config_ChainCreate( &psz_name, &p_cfg, psz_parser );
  402.     free( psz_parser );
  403.     free( psz_tmp );
  404.     p_vout->p_cfg = p_cfg;
  405.     p_vout->p_module = module_need( p_vout,
  406.         ( p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain ) ?
  407.         "video filter" : "video output", psz_name, p_vout->p->psz_filter_chain && *p_vout->p->psz_filter_chain );
  408.     free( psz_name );
  409.     vlc_object_set_destructor( p_vout, vout_Destructor );
  410.     if( p_vout->p_module == NULL )
  411.     {
  412.         msg_Err( p_vout, "no suitable vout module" );
  413.         spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
  414.         spu_Destroy( p_vout->p_spu );
  415.         p_vout->p_spu = NULL;
  416.         vlc_object_release( p_vout );
  417.         return NULL;
  418.     }
  419.     /* Create a few object variables for interface interaction */
  420.     var_Create( p_vout, "vout-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
  421.     text.psz_string = _("Filters");
  422.     var_Change( p_vout, "vout-filter", VLC_VAR_SETTEXT, &text, NULL );
  423.     var_AddCallback( p_vout, "vout-filter", FilterCallback, NULL );
  424.     /* */
  425.     DeinterlaceEnable( p_vout );
  426.     /* */
  427.     vlc_cond_init( &p_vout->p->change_wait );
  428.     if( vlc_clone( &p_vout->p->thread, RunThread, p_vout,
  429.                    VLC_THREAD_PRIORITY_OUTPUT ) )
  430.     {
  431.         module_unneed( p_vout, p_vout->p_module );
  432.         p_vout->p_module = NULL;
  433.         spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
  434.         spu_Destroy( p_vout->p_spu );
  435.         p_vout->p_spu = NULL;
  436.         vlc_object_release( p_vout );
  437.         return NULL;
  438.     }
  439.     vlc_mutex_lock( &p_vout->change_lock );
  440.     while( !p_vout->p->b_ready )
  441.     {   /* We are (ab)using the same condition in opposite directions for
  442.          * b_ready and b_done. This works because of the strict ordering. */
  443.         assert( !p_vout->p->b_done );
  444.         vlc_cond_wait( &p_vout->p->change_wait, &p_vout->change_lock );
  445.     }
  446.     vlc_mutex_unlock( &p_vout->change_lock );
  447.     if( p_vout->b_error )
  448.     {
  449.         msg_Err( p_vout, "video output creation failed" );
  450.         vout_CloseAndRelease( p_vout );
  451.         return NULL;
  452.     }
  453.     return p_vout;
  454. }
  455. /*****************************************************************************
  456.  * vout_Close: Close a vout created by vout_Create.
  457.  *****************************************************************************
  458.  * You HAVE to call it on vout created by vout_Create before vlc_object_release.
  459.  * You should NEVER call it on vout not obtained through vout_Create
  460.  * (like with vout_Request or vlc_object_find.)
  461.  * You can use vout_CloseAndRelease() as a convenience method.
  462.  *****************************************************************************/
  463. void vout_Close( vout_thread_t *p_vout )
  464. {
  465.     assert( p_vout );
  466.     vlc_mutex_lock( &p_vout->change_lock );
  467.     p_vout->p->b_done = true;
  468.     vlc_cond_signal( &p_vout->p->change_wait );
  469.     vlc_mutex_unlock( &p_vout->change_lock );
  470.     vlc_mutex_lock( &p_vout->p->snapshot.lock );
  471.     p_vout->p->snapshot.b_available = false;
  472.     vlc_cond_broadcast( &p_vout->p->snapshot.wait );
  473.     vlc_mutex_unlock( &p_vout->p->snapshot.lock );
  474.     vlc_join( p_vout->p->thread, NULL );
  475.     module_unneed( p_vout, p_vout->p_module );
  476.     p_vout->p_module = NULL;
  477. }
  478. /* */
  479. static void vout_Destructor( vlc_object_t * p_this )
  480. {
  481.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  482.     /* Make sure the vout was stopped first */
  483.     assert( !p_vout->p_module );
  484.     /* */
  485.     if( p_vout->p_spu )
  486.         spu_Destroy( p_vout->p_spu );
  487.     /* Destroy the locks */
  488.     vlc_cond_destroy( &p_vout->p->change_wait );
  489.     vlc_cond_destroy( &p_vout->p->picture_wait );
  490.     vlc_mutex_destroy( &p_vout->picture_lock );
  491.     vlc_mutex_destroy( &p_vout->change_lock );
  492.     vlc_mutex_destroy( &p_vout->p->vfilter_lock );
  493.     /* */
  494.     for( ;; )
  495.     {
  496.         picture_t *p_picture = p_vout->p->snapshot.p_picture;
  497.         if( !p_picture )
  498.             break;
  499.         p_vout->p->snapshot.p_picture = p_picture->p_next;
  500.         picture_Release( p_picture );
  501.     }
  502.     vlc_cond_destroy( &p_vout->p->snapshot.wait );
  503.     vlc_mutex_destroy( &p_vout->p->snapshot.lock );
  504.     /* */
  505.     free( p_vout->p->psz_filter_chain );
  506.     free( p_vout->p->psz_title );
  507.     config_ChainDestroy( p_vout->p_cfg );
  508.     free( p_vout->p );
  509. #ifndef __APPLE__
  510.     vout_thread_t *p_another_vout;
  511.     /* This is a dirty hack mostly for Linux, where there is no way to get the
  512.      * GUI back if you closed it while playing video. This is solved in
  513.      * Mac OS X, where we have this novelty called menubar, that will always
  514.      * allow you access to the applications main functionality. They should try
  515.      * that on linux sometime. */
  516.     p_another_vout = vlc_object_find( p_this->p_libvlc,
  517.                                       VLC_OBJECT_VOUT, FIND_ANYWHERE );
  518.     if( p_another_vout == NULL )
  519.         var_SetBool( p_this->p_libvlc, "intf-show", true );
  520.     else
  521.         vlc_object_release( p_another_vout );
  522. #endif
  523. }
  524. /* */
  525. void vout_ChangePause( vout_thread_t *p_vout, bool b_paused, mtime_t i_date )
  526. {
  527.     vlc_mutex_lock( &p_vout->change_lock );
  528.     assert( !p_vout->p->b_paused || !b_paused );
  529.     vlc_mutex_lock( &p_vout->picture_lock );
  530.     p_vout->p->i_picture_displayed_date = 0;
  531.     if( p_vout->p->b_paused )
  532.     {
  533.         const mtime_t i_duration = i_date - p_vout->p->i_pause_date;
  534.         for( int i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
  535.         {
  536.             picture_t *p_pic = PP_RENDERPICTURE[i_index];
  537.             if( p_pic->i_status == READY_PICTURE )
  538.                 p_pic->date += i_duration;
  539.         }
  540.         vlc_cond_signal( &p_vout->p->picture_wait );
  541.         vlc_mutex_unlock( &p_vout->picture_lock );
  542.         spu_OffsetSubtitleDate( p_vout->p_spu, i_duration );
  543.     }
  544.     else
  545.     {
  546.         vlc_mutex_unlock( &p_vout->picture_lock );
  547.     }
  548.     p_vout->p->b_paused = b_paused;
  549.     p_vout->p->i_pause_date = i_date;
  550.     vlc_mutex_unlock( &p_vout->change_lock );
  551. }
  552. void vout_GetResetStatistic( vout_thread_t *p_vout, int *pi_displayed, int *pi_lost )
  553. {
  554.     vlc_mutex_lock( &p_vout->change_lock );
  555.     *pi_displayed = p_vout->p->i_picture_displayed;
  556.     *pi_lost = p_vout->p->i_picture_lost;
  557.     p_vout->p->i_picture_displayed = 0;
  558.     p_vout->p->i_picture_lost = 0;
  559.     vlc_mutex_unlock( &p_vout->change_lock );
  560. }
  561. void vout_Flush( vout_thread_t *p_vout, mtime_t i_date )
  562. {
  563.     vlc_mutex_lock( &p_vout->picture_lock );
  564.     p_vout->p->i_picture_displayed_date = 0;
  565.     for( int i = 0; i < p_vout->render.i_pictures; i++ )
  566.     {
  567.         picture_t *p_pic = p_vout->render.pp_picture[i];
  568.         if( p_pic->i_status == READY_PICTURE ||
  569.             p_pic->i_status == DISPLAYED_PICTURE )
  570.         {
  571.             /* We cannot change picture status if it is in READY_PICTURE state,
  572.              * Just make sure they won't be displayed */
  573.             if( p_pic->date > i_date )
  574.                 p_pic->date = i_date;
  575.         }
  576.     }
  577.     vlc_cond_signal( &p_vout->p->picture_wait );
  578.     vlc_mutex_unlock( &p_vout->picture_lock );
  579. }
  580. void vout_FixLeaks( vout_thread_t *p_vout, bool b_forced )
  581. {
  582.     int i_pic, i_ready_pic;
  583.     vlc_mutex_lock( &p_vout->picture_lock );
  584.     for( i_pic = 0, i_ready_pic = 0; i_pic < p_vout->render.i_pictures && !b_forced; i_pic++ )
  585.     {
  586.         const picture_t *p_pic = p_vout->render.pp_picture[i_pic];
  587.         if( p_pic->i_status == READY_PICTURE )
  588.         {
  589.             i_ready_pic++;
  590.             /* If we have at least 2 ready pictures, wait for the vout thread to
  591.              * process one */
  592.             if( i_ready_pic >= 2 )
  593.                 break;
  594.             continue;
  595.         }
  596.         if( p_pic->i_status == DISPLAYED_PICTURE )
  597.         {
  598.             /* If at least one displayed picture is not referenced
  599.              * let vout free it */
  600.             if( p_pic->i_refcount == 0 )
  601.                 break;
  602.         }
  603.     }
  604.     if( i_pic < p_vout->render.i_pictures && !b_forced )
  605.     {
  606.         vlc_mutex_unlock( &p_vout->picture_lock );
  607.         return;
  608.     }
  609.     /* Too many pictures are still referenced, there is probably a bug
  610.      * with the decoder */
  611.     if( !b_forced )
  612.         msg_Err( p_vout, "pictures leaked, resetting the heap" );
  613.     /* Just free all the pictures */
  614.     for( i_pic = 0; i_pic < p_vout->render.i_pictures; i_pic++ )
  615.     {
  616.         picture_t *p_pic = p_vout->render.pp_picture[i_pic];
  617.         msg_Dbg( p_vout, "[%d] %d %d", i_pic, p_pic->i_status, p_pic->i_refcount );
  618.         p_pic->i_refcount = 0;
  619.         switch( p_pic->i_status )
  620.         {
  621.         case READY_PICTURE:
  622.         case DISPLAYED_PICTURE:
  623.         case RESERVED_PICTURE:
  624.             if( p_pic != p_vout->p->p_picture_displayed )
  625.                 vout_UsePictureLocked( p_vout, p_pic );
  626.             break;
  627.         }
  628.     }
  629.     vlc_cond_signal( &p_vout->p->picture_wait );
  630.     vlc_mutex_unlock( &p_vout->picture_lock );
  631. }
  632. void vout_NextPicture( vout_thread_t *p_vout, mtime_t *pi_duration )
  633. {
  634.     vlc_mutex_lock( &p_vout->picture_lock );
  635.     const mtime_t i_displayed_date = p_vout->p->i_picture_displayed_date;
  636.     p_vout->p->b_picture_displayed = false;
  637.     p_vout->p->b_picture_empty = false;
  638.     if( p_vout->p->p_picture_displayed )
  639.     {
  640.         p_vout->p->p_picture_displayed->date = 1;
  641.         vlc_cond_signal( &p_vout->p->picture_wait );
  642.     }
  643.     while( !p_vout->p->b_picture_displayed && !p_vout->p->b_picture_empty )
  644.         vlc_cond_wait( &p_vout->p->picture_wait, &p_vout->picture_lock );
  645.     *pi_duration = __MAX( p_vout->p->i_picture_displayed_date - i_displayed_date, 0 );
  646.     /* TODO advance subpicture by the duration ... */
  647.     vlc_mutex_unlock( &p_vout->picture_lock );
  648. }
  649. void vout_DisplayTitle( vout_thread_t *p_vout, const char *psz_title )
  650. {
  651.     assert( psz_title );
  652.     if( !config_GetInt( p_vout, "osd" ) )
  653.         return;
  654.     vlc_mutex_lock( &p_vout->change_lock );
  655.     free( p_vout->p->psz_title );
  656.     p_vout->p->psz_title = strdup( psz_title );
  657.     vlc_mutex_unlock( &p_vout->change_lock );
  658. }
  659. /*****************************************************************************
  660.  * InitThread: initialize video output thread
  661.  *****************************************************************************
  662.  * This function is called from RunThread and performs the second step of the
  663.  * initialization. It returns 0 on success. Note that the thread's flag are not
  664.  * modified inside this function.
  665.  * XXX You have to enter it with change_lock taken.
  666.  *****************************************************************************/
  667. static int ChromaCreate( vout_thread_t *p_vout );
  668. static void ChromaDestroy( vout_thread_t *p_vout );
  669. static bool ChromaIsEqual( const picture_heap_t *p_output, const picture_heap_t *p_render )
  670. {
  671.      if( !vout_ChromaCmp( p_output->i_chroma, p_render->i_chroma ) )
  672.          return false;
  673.      if( p_output->i_chroma != FOURCC_RV15 &&
  674.          p_output->i_chroma != FOURCC_RV16 &&
  675.          p_output->i_chroma != FOURCC_RV24 &&
  676.          p_output->i_chroma != FOURCC_RV32 )
  677.          return true;
  678.      return p_output->i_rmask == p_render->i_rmask &&
  679.             p_output->i_gmask == p_render->i_gmask &&
  680.             p_output->i_bmask == p_render->i_bmask;
  681. }
  682. static int InitThread( vout_thread_t *p_vout )
  683. {
  684.     int i, i_aspect_x, i_aspect_y;
  685.     /* Initialize output method, it allocates direct buffers for us */
  686.     if( p_vout->pf_init( p_vout ) )
  687.         return VLC_EGENERIC;
  688.     p_vout->p->p_picture_displayed = NULL;
  689.     if( !I_OUTPUTPICTURES )
  690.     {
  691.         msg_Err( p_vout, "plugin was unable to allocate at least "
  692.                          "one direct buffer" );
  693.         p_vout->pf_end( p_vout );
  694.         return VLC_EGENERIC;
  695.     }
  696.     if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
  697.     {
  698.         msg_Err( p_vout, "plugin allocated too many direct buffers, "
  699.                          "our internal buffers must have overflown." );
  700.         p_vout->pf_end( p_vout );
  701.         return VLC_EGENERIC;
  702.     }
  703.     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
  704.     AspectRatio( p_vout->fmt_render.i_aspect, &i_aspect_x, &i_aspect_y );
  705.     AspectRatio( p_vout->fmt_in.i_aspect, &i_aspect_x, &i_aspect_y );
  706.     if( !p_vout->fmt_out.i_width || !p_vout->fmt_out.i_height )
  707.     {
  708.         p_vout->fmt_out.i_width = p_vout->fmt_out.i_visible_width =
  709.             p_vout->output.i_width;
  710.         p_vout->fmt_out.i_height = p_vout->fmt_out.i_visible_height =
  711.             p_vout->output.i_height;
  712.         p_vout->fmt_out.i_x_offset =  p_vout->fmt_out.i_y_offset = 0;
  713.         p_vout->fmt_out.i_aspect = p_vout->output.i_aspect;
  714.         p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
  715.     }
  716.     if( !p_vout->fmt_out.i_sar_num || !p_vout->fmt_out.i_sar_num )
  717.     {
  718.         p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_aspect *
  719.             p_vout->fmt_out.i_height;
  720.         p_vout->fmt_out.i_sar_den = VOUT_ASPECT_FACTOR *
  721.             p_vout->fmt_out.i_width;
  722.     }
  723.     vlc_ureduce( &p_vout->fmt_out.i_sar_num, &p_vout->fmt_out.i_sar_den,
  724.                  p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den, 0 );
  725.     AspectRatio( p_vout->fmt_out.i_aspect, &i_aspect_x, &i_aspect_y );
  726.     /* FIXME removed the need of both fmt_* and heap infos */
  727.     /* Calculate shifts from system-updated masks */
  728.     PictureHeapFixRgb( &p_vout->render );
  729.     VideoFormatImportRgb( &p_vout->fmt_render, &p_vout->render );
  730.     PictureHeapFixRgb( &p_vout->output );
  731.     VideoFormatImportRgb( &p_vout->fmt_out, &p_vout->output );
  732.     /* print some usefull debug info about different vout formats
  733.      */
  734.     msg_Dbg( p_vout, "pic render sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
  735.              p_vout->fmt_render.i_width, p_vout->fmt_render.i_height,
  736.              p_vout->fmt_render.i_x_offset, p_vout->fmt_render.i_y_offset,
  737.              p_vout->fmt_render.i_visible_width,
  738.              p_vout->fmt_render.i_visible_height,
  739.              (char*)&p_vout->fmt_render.i_chroma,
  740.              i_aspect_x, i_aspect_y,
  741.              p_vout->fmt_render.i_sar_num, p_vout->fmt_render.i_sar_den,
  742.              p_vout->fmt_render.i_rmask, p_vout->fmt_render.i_gmask, p_vout->fmt_render.i_bmask );
  743.     msg_Dbg( p_vout, "pic in sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
  744.              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
  745.              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
  746.              p_vout->fmt_in.i_visible_width,
  747.              p_vout->fmt_in.i_visible_height,
  748.              (char*)&p_vout->fmt_in.i_chroma,
  749.              i_aspect_x, i_aspect_y,
  750.              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den,
  751.              p_vout->fmt_in.i_rmask, p_vout->fmt_in.i_gmask, p_vout->fmt_in.i_bmask );
  752.     msg_Dbg( p_vout, "pic out sz %ix%i, of (%i,%i), vsz %ix%i, 4cc %4.4s, ar %i:%i, sar %i:%i, msk r0x%x g0x%x b0x%x",
  753.              p_vout->fmt_out.i_width, p_vout->fmt_out.i_height,
  754.              p_vout->fmt_out.i_x_offset, p_vout->fmt_out.i_y_offset,
  755.              p_vout->fmt_out.i_visible_width,
  756.              p_vout->fmt_out.i_visible_height,
  757.              (char*)&p_vout->fmt_out.i_chroma,
  758.              i_aspect_x, i_aspect_y,
  759.              p_vout->fmt_out.i_sar_num, p_vout->fmt_out.i_sar_den,
  760.              p_vout->fmt_out.i_rmask, p_vout->fmt_out.i_gmask, p_vout->fmt_out.i_bmask );
  761.     /* Check whether we managed to create direct buffers similar to
  762.      * the render buffers, ie same size and chroma */
  763.     if( ( p_vout->output.i_width == p_vout->render.i_width )
  764.      && ( p_vout->output.i_height == p_vout->render.i_height )
  765.      && ( ChromaIsEqual( &p_vout->output, &p_vout->render ) ) )
  766.     {
  767.         /* Cool ! We have direct buffers, we can ask the decoder to
  768.          * directly decode into them ! Map the first render buffers to
  769.          * the first direct buffers, but keep the first direct buffer
  770.          * for memcpy operations */
  771.         p_vout->p->b_direct = true;
  772.         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
  773.         {
  774.             if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
  775.                 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
  776.                 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
  777.             {
  778.                 /* We have enough direct buffers so there's no need to
  779.                  * try to use system memory buffers. */
  780.                 break;
  781.             }
  782.             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
  783.             I_RENDERPICTURES++;
  784.         }
  785.         msg_Dbg( p_vout, "direct render, mapping "
  786.                  "render pictures 0-%i to system pictures 1-%i",
  787.                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
  788.     }
  789.     else
  790.     {
  791.         /* Rats... Something is wrong here, we could not find an output
  792.          * plugin able to directly render what we decode. See if we can
  793.          * find a chroma plugin to do the conversion */
  794.         p_vout->p->b_direct = false;
  795.         if( ChromaCreate( p_vout ) )
  796.         {
  797.             p_vout->pf_end( p_vout );
  798.             return VLC_EGENERIC;
  799.         }
  800.         msg_Dbg( p_vout, "indirect render, mapping "
  801.                  "render pictures 0-%i to system pictures %i-%i",
  802.                  VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
  803.                  I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
  804.         /* Append render buffers after the direct buffers */
  805.         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
  806.         {
  807.             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
  808.             I_RENDERPICTURES++;
  809.             /* Check if we have enough render pictures */
  810.             if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
  811.                 break;
  812.         }
  813.     }
  814.     /* Link pictures back to their heap */
  815.     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
  816.     {
  817.         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
  818.     }
  819.     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
  820.     {
  821.         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
  822.     }
  823.     return VLC_SUCCESS;
  824. }
  825. /*****************************************************************************
  826.  * RunThread: video output thread
  827.  *****************************************************************************
  828.  * Video output thread. This function does only returns when the thread is
  829.  * terminated. It handles the pictures arriving in the video heap and the
  830.  * display device events.
  831.  *****************************************************************************/
  832. static void* RunThread( void *p_this )
  833. {
  834.     vout_thread_t *p_vout = p_this;
  835.     int             i_idle_loops = 0;  /* loops without displaying a picture */
  836.     int             i_picture_qtype_last = QTYPE_NONE;
  837.     bool            b_drop_late;
  838.     /*
  839.      * Initialize thread
  840.      */
  841.     vlc_mutex_lock( &p_vout->change_lock );
  842.     p_vout->b_error = InitThread( p_vout );
  843.     b_drop_late = var_CreateGetBool( p_vout, "drop-late-frames" );
  844.     /* signal the creation of the vout */
  845.     p_vout->p->b_ready = true;
  846.     vlc_cond_signal( &p_vout->p->change_wait );
  847.     if( p_vout->b_error )
  848.     {
  849.         EndThread( p_vout );
  850.         vlc_mutex_unlock( &p_vout->change_lock );
  851.         return NULL;
  852.     }
  853.     /*
  854.      * Main loop - it is not executed if an error occurred during
  855.      * initialization
  856.      */
  857.     while( !p_vout->p->b_done && !p_vout->b_error )
  858.     {
  859.         /* Initialize loop variables */
  860.         const mtime_t current_date = mdate();
  861.         picture_t *p_picture;
  862.         picture_t *p_filtered_picture;
  863.         mtime_t display_date;
  864.         picture_t *p_directbuffer;
  865.         int i_index;
  866.         if( p_vout->p->b_title_show && p_vout->p->psz_title )
  867.             DisplayTitleOnOSD( p_vout );
  868.         vlc_mutex_lock( &p_vout->picture_lock );
  869.         /* Look for the earliest picture but after the last displayed one */
  870.         picture_t *p_last = p_vout->p->p_picture_displayed;;
  871.         p_picture = NULL;
  872.         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
  873.         {
  874.             picture_t *p_pic = PP_RENDERPICTURE[i_index];
  875.             if( p_pic->i_status != READY_PICTURE )
  876.                 continue;
  877.             if( p_vout->p->b_paused && p_last && p_last->date > 1 )
  878.                 continue;
  879.             if( p_last && p_pic != p_last && p_pic->date <= p_last->date )
  880.             {
  881.                 /* Drop old picture */
  882.                 vout_UsePictureLocked( p_vout, p_pic );
  883.             }
  884.             else if( !p_vout->p->b_paused && !p_pic->b_force && p_pic != p_last &&
  885.                      p_pic->date < current_date + p_vout->p->render_time &&
  886.                      b_drop_late )
  887.             {
  888.                 /* Picture is late: it will be destroyed and the thread
  889.                  * will directly choose the next picture */
  890.                 vout_UsePictureLocked( p_vout, p_pic );
  891.                 p_vout->p->i_picture_lost++;
  892.                 msg_Warn( p_vout, "late picture skipped (%"PRId64" > %d)",
  893.                                   current_date - p_pic->date, - p_vout->p->render_time );
  894.             }
  895.             else if( ( !p_last || p_last->date < p_pic->date ) &&
  896.                      ( p_picture == NULL || p_pic->date < p_picture->date ) )
  897.             {
  898.                 p_picture = p_pic;
  899.             }
  900.         }
  901.         if( !p_picture )
  902.         {
  903.             p_picture = p_last;
  904.             if( !p_vout->p->b_picture_empty )
  905.             {
  906.                 p_vout->p->b_picture_empty = true;
  907.                 vlc_cond_signal( &p_vout->p->picture_wait );
  908.             }
  909.         }
  910.         display_date = 0;
  911.         if( p_picture )
  912.         {
  913.             display_date = p_picture->date;
  914.             /* If we found better than the last picture, destroy it */
  915.             if( p_last && p_picture != p_last )
  916.             {
  917.                 vout_UsePictureLocked( p_vout, p_last );
  918.                 p_vout->p->p_picture_displayed = p_last = NULL;
  919.             }
  920.             /* Compute FPS rate */
  921.             p_vout->p->p_fps_sample[ p_vout->p->c_fps_samples++ % VOUT_FPS_SAMPLES ] = display_date;
  922.             if( !p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
  923.             {
  924.                 /* A picture is ready to be rendered, but its rendering date
  925.                  * is far from the current one so the thread will perform an
  926.                  * empty loop as if no picture were found. The picture state
  927.                  * is unchanged */
  928.                 p_picture    = NULL;
  929.                 display_date = 0;
  930.             }
  931.             else if( p_picture == p_last )
  932.             {
  933.                 /* We are asked to repeat the previous picture, but we first
  934.                  * wait for a couple of idle loops */
  935.                 if( i_idle_loops < 4 )
  936.                 {
  937.                     p_picture    = NULL;
  938.                     display_date = 0;
  939.                 }
  940.                 else
  941.                 {
  942.                     /* We set the display date to something high, otherwise
  943.                      * we'll have lots of problems with late pictures */
  944.                     display_date = current_date + p_vout->p->render_time;
  945.                 }
  946.             }
  947.             else if( p_vout->p->b_paused && display_date > current_date + VOUT_DISPLAY_DELAY )
  948.             {
  949.                 display_date = current_date + VOUT_DISPLAY_DELAY;
  950.             }
  951.             if( p_picture )
  952.             {
  953.                 if( p_picture->date > 1 )
  954.                 {
  955.                     p_vout->p->i_picture_displayed_date = p_picture->date;
  956.                     if( p_picture != p_last && !p_vout->p->b_picture_displayed )
  957.                     {
  958.                         p_vout->p->b_picture_displayed = true;
  959.                         vlc_cond_signal( &p_vout->p->picture_wait );
  960.                     }
  961.                 }
  962.                 p_vout->p->p_picture_displayed = p_picture;
  963.             }
  964.         }
  965.         /* */
  966.         const int i_postproc_type = p_vout->p->i_picture_qtype;
  967.         const int i_postproc_state = (p_vout->p->i_picture_qtype != QTYPE_NONE) - (i_picture_qtype_last != QTYPE_NONE);
  968.         vlc_mutex_unlock( &p_vout->picture_lock );
  969.         if( p_picture == NULL )
  970.             i_idle_loops++;
  971.         p_filtered_picture = NULL;
  972.         if( p_picture )
  973.             p_filtered_picture = filter_chain_VideoFilter( p_vout->p->p_vf2_chain,
  974.                                                            p_picture );
  975.         bool b_snapshot = false;
  976.         if( vlc_mutex_trylock( &p_vout->p->snapshot.lock ) == 0 )
  977.         {
  978.              b_snapshot = p_vout->p->snapshot.i_request > 0
  979.                        && p_picture != NULL;
  980.              vlc_mutex_unlock( &p_vout->p->snapshot.lock );
  981.         }
  982.         /*
  983.          * Check for subpictures to display
  984.          */
  985.         mtime_t spu_render_time;
  986.         if( p_vout->p->b_paused )
  987.             spu_render_time = p_vout->p->i_pause_date;
  988.         else if( p_picture )
  989.             spu_render_time = p_picture->date > 1 ? p_picture->date : mdate();
  990.         else
  991.             spu_render_time = 0;
  992.         subpicture_t *p_subpic = spu_SortSubpicturesNew( p_vout->p_spu,
  993.                                                          spu_render_time,
  994.                                                          b_snapshot );
  995.         /*
  996.          * Perform rendering
  997.          */
  998.         p_vout->p->i_picture_displayed++;
  999.         p_directbuffer = vout_RenderPicture( p_vout,
  1000.                                              p_filtered_picture, p_subpic,
  1001.                                              spu_render_time );
  1002.         /*
  1003.          * Take a snapshot if requested
  1004.          */
  1005.         if( p_directbuffer && b_snapshot )
  1006.         {
  1007.             vlc_mutex_lock( &p_vout->p->snapshot.lock );
  1008.             assert( p_vout->p->snapshot.i_request > 0 );
  1009.             while( p_vout->p->snapshot.i_request > 0 )
  1010.             {
  1011.                 picture_t *p_pic = picture_New( p_vout->fmt_out.i_chroma,
  1012.                                                 p_vout->fmt_out.i_width,
  1013.                                                 p_vout->fmt_out.i_height,
  1014.                                                 p_vout->fmt_out.i_aspect  );
  1015.                 if( !p_pic )
  1016.                     break;
  1017.                 picture_Copy( p_pic, p_directbuffer );
  1018.                 p_pic->format.i_sar_num = p_vout->fmt_out.i_sar_num;
  1019.                 p_pic->format.i_sar_den = p_vout->fmt_out.i_sar_den;
  1020.                 p_pic->p_next = p_vout->p->snapshot.p_picture;
  1021.                 p_vout->p->snapshot.p_picture = p_pic;
  1022.                 p_vout->p->snapshot.i_request--;
  1023.             }
  1024.             vlc_cond_broadcast( &p_vout->p->snapshot.wait );
  1025.             vlc_mutex_unlock( &p_vout->p->snapshot.lock );
  1026.         }
  1027.         /*
  1028.          * Call the plugin-specific rendering method if there is one
  1029.          */
  1030.         if( p_filtered_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
  1031.         {
  1032.             /* Render the direct buffer returned by vout_RenderPicture */
  1033.             p_vout->pf_render( p_vout, p_directbuffer );
  1034.         }
  1035.         /*
  1036.          * Sleep, wake up
  1037.          */
  1038.         if( display_date != 0 && p_directbuffer != NULL )
  1039.         {
  1040.             mtime_t current_render_time = mdate() - current_date;
  1041.             /* if render time is very large we don't include it in the mean */
  1042.             if( current_render_time < p_vout->p->render_time +
  1043.                 VOUT_DISPLAY_DELAY )
  1044.             {
  1045.                 /* Store render time using a sliding mean weighting to
  1046.                  * current value in a 3 to 1 ratio*/
  1047.                 p_vout->p->render_time *= 3;
  1048.                 p_vout->p->render_time += current_render_time;
  1049.                 p_vout->p->render_time >>= 2;
  1050.             }
  1051.             else
  1052.                 msg_Dbg( p_vout, "skipped big render time %d > %d", (int) current_render_time,
  1053.                  (int) (p_vout->p->render_time +VOUT_DISPLAY_DELAY ) ) ;
  1054.         }
  1055.         /* Give back change lock */
  1056.         vlc_mutex_unlock( &p_vout->change_lock );
  1057.         /* Sleep a while or until a given date */
  1058.         if( display_date != 0 )
  1059.         {
  1060.             /* If there are *vout* filters in the chain, better give them the picture
  1061.              * in advance */
  1062.             if( !p_vout->p->psz_filter_chain || !*p_vout->p->psz_filter_chain )
  1063.             {
  1064.                 mwait( display_date - VOUT_MWAIT_TOLERANCE );
  1065.             }
  1066.         }
  1067.         else
  1068.         {
  1069.             /* Wait until a frame is being sent or a spurious wakeup (not a problem here) */
  1070.             vlc_mutex_lock( &p_vout->picture_lock );
  1071.             vlc_cond_timedwait( &p_vout->p->picture_wait, &p_vout->picture_lock, current_date + VOUT_IDLE_SLEEP );
  1072.             vlc_mutex_unlock( &p_vout->picture_lock );
  1073.         }
  1074.         /* On awakening, take back lock and send immediately picture
  1075.          * to display. */
  1076.         /* Note: p_vout->p->b_done could be true here and now */
  1077.         vlc_mutex_lock( &p_vout->change_lock );
  1078.         /*
  1079.          * Display the previously rendered picture
  1080.          */
  1081.         if( p_filtered_picture != NULL && p_directbuffer != NULL )
  1082.         {
  1083.             /* Display the direct buffer returned by vout_RenderPicture */
  1084.             if( p_vout->pf_display )
  1085.                 p_vout->pf_display( p_vout, p_directbuffer );
  1086.             /* Tell the vout this was the last picture and that it does not
  1087.              * need to be forced anymore. */
  1088.             p_picture->b_force = false;
  1089.         }
  1090.         /* Drop the filtered picture if created by video filters */
  1091.         if( p_filtered_picture != NULL && p_filtered_picture != p_picture )
  1092.         {
  1093.             vlc_mutex_lock( &p_vout->picture_lock );
  1094.             vout_UsePictureLocked( p_vout, p_filtered_picture );
  1095.             vlc_mutex_unlock( &p_vout->picture_lock );
  1096.         }
  1097.         if( p_picture != NULL )
  1098.         {
  1099.             /* Reinitialize idle loop count */
  1100.             i_idle_loops = 0;
  1101.         }
  1102.         /*
  1103.          * Check events and manage thread
  1104.          */
  1105.         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
  1106.         {
  1107.             /* A fatal error occurred, and the thread must terminate
  1108.              * immediately, without displaying anything - setting b_error to 1
  1109.              * causes the immediate end of the main while() loop. */
  1110.             // FIXME pf_end
  1111.             p_vout->b_error = 1;
  1112.             break;
  1113.         }
  1114.         while( p_vout->i_changes & VOUT_ON_TOP_CHANGE )
  1115.         {
  1116.             p_vout->i_changes &= ~VOUT_ON_TOP_CHANGE;
  1117.             vlc_mutex_unlock( &p_vout->change_lock );
  1118.             vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, p_vout->b_on_top );
  1119.             vlc_mutex_lock( &p_vout->change_lock );
  1120.         }
  1121.         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
  1122.         {
  1123.             /* this must only happen when the vout plugin is incapable of
  1124.              * rescaling the picture itself. In this case we need to destroy
  1125.              * the current picture buffers and recreate new ones with the right
  1126.              * dimensions */
  1127.             int i;
  1128.             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
  1129.             assert( !p_vout->p->b_direct );
  1130.             ChromaDestroy( p_vout );
  1131.             vlc_mutex_lock( &p_vout->picture_lock );
  1132.             p_vout->pf_end( p_vout );
  1133.             p_vout->p->p_picture_displayed = NULL;
  1134.             for( i = 0; i < I_OUTPUTPICTURES; i++ )
  1135.                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
  1136.             vlc_cond_signal( &p_vout->p->picture_wait );
  1137.             I_OUTPUTPICTURES = 0;
  1138.             if( p_vout->pf_init( p_vout ) )
  1139.             {
  1140.                 msg_Err( p_vout, "cannot resize display" );
  1141.                 /* FIXME: pf_end will be called again in EndThread() */
  1142.                 p_vout->b_error = 1;
  1143.             }
  1144.             vlc_mutex_unlock( &p_vout->picture_lock );
  1145.             /* Need to reinitialise the chroma plugin. Since we might need
  1146.              * resizing too and it's not sure that we already had it,
  1147.              * recreate the chroma plugin chain from scratch. */
  1148.             /* dionoea */
  1149.             if( ChromaCreate( p_vout ) )
  1150.             {
  1151.                 msg_Err( p_vout, "WOW THIS SUCKS BIG TIME!!!!!" );
  1152.                 p_vout->b_error = 1;
  1153.             }
  1154.             if( p_vout->b_error )
  1155.                 break;
  1156.         }
  1157.         if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
  1158.         {
  1159.             /* This happens when the picture buffers need to be recreated.
  1160.              * This is useful on multimonitor displays for instance.
  1161.              *
  1162.              * Warning: This only works when the vout creates only 1 picture
  1163.              * buffer!! */
  1164.             p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
  1165.             if( !p_vout->p->b_direct )
  1166.                 ChromaDestroy( p_vout );
  1167.             vlc_mutex_lock( &p_vout->picture_lock );
  1168.             p_vout->pf_end( p_vout );
  1169.             I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
  1170.             p_vout->b_error = InitThread( p_vout );
  1171.             if( p_vout->b_error )
  1172.                 msg_Err( p_vout, "InitThread after VOUT_PICTURE_BUFFERS_CHANGE failed" );
  1173.             vlc_cond_signal( &p_vout->p->picture_wait );
  1174.             vlc_mutex_unlock( &p_vout->picture_lock );
  1175.             if( p_vout->b_error )
  1176.                 break;
  1177.         }
  1178.         /* Post processing */
  1179.         if( i_postproc_state == 1 )
  1180.             PostProcessEnable( p_vout );
  1181.         else if( i_postproc_state == -1 )
  1182.             PostProcessDisable( p_vout );
  1183.         if( i_postproc_state != 0 )
  1184.             i_picture_qtype_last = i_postproc_type;
  1185.         /* Check for "video filter2" changes */
  1186.         vlc_mutex_lock( &p_vout->p->vfilter_lock );
  1187.         if( p_vout->p->psz_vf2 )
  1188.         {
  1189.             es_format_t fmt;
  1190.             es_format_Init( &fmt, VIDEO_ES, p_vout->fmt_render.i_chroma );
  1191.             fmt.video = p_vout->fmt_render;
  1192.             filter_chain_Reset( p_vout->p->p_vf2_chain, &fmt, &fmt );
  1193.             if( filter_chain_AppendFromString( p_vout->p->p_vf2_chain,
  1194.                                                p_vout->p->psz_vf2 ) < 0 )
  1195.                 msg_Err( p_vout, "Video filter chain creation failed" );
  1196.             free( p_vout->p->psz_vf2 );
  1197.             p_vout->p->psz_vf2 = NULL;
  1198.             if( i_picture_qtype_last != QTYPE_NONE )
  1199.                 PostProcessSetFilterQuality( p_vout );
  1200.         }
  1201.         vlc_mutex_unlock( &p_vout->p->vfilter_lock );
  1202.     }
  1203.     /*
  1204.      * Error loop - wait until the thread destruction is requested
  1205.      */
  1206.     if( p_vout->b_error )
  1207.         ErrorThread( p_vout );
  1208.     /* End of thread */
  1209.     CleanThread( p_vout );
  1210.     EndThread( p_vout );
  1211.     vlc_mutex_unlock( &p_vout->change_lock );
  1212.     return NULL;
  1213. }
  1214. /*****************************************************************************
  1215.  * ErrorThread: RunThread() error loop
  1216.  *****************************************************************************
  1217.  * This function is called when an error occurred during thread main's loop.
  1218.  * The thread can still receive feed, but must be ready to terminate as soon
  1219.  * as possible.
  1220.  *****************************************************************************/
  1221. static void ErrorThread( vout_thread_t *p_vout )
  1222. {
  1223.     /* Wait until a `close' order */
  1224.     while( !p_vout->p->b_done )
  1225.         vlc_cond_wait( &p_vout->p->change_wait, &p_vout->change_lock );
  1226. }
  1227. /*****************************************************************************
  1228.  * CleanThread: clean up after InitThread
  1229.  *****************************************************************************
  1230.  * This function is called after a sucessful
  1231.  * initialization. It frees all resources allocated by InitThread.
  1232.  * XXX You have to enter it with change_lock taken.
  1233.  *****************************************************************************/
  1234. static void CleanThread( vout_thread_t *p_vout )
  1235. {
  1236.     int     i_index;                                        /* index in heap */
  1237.     if( !p_vout->p->b_direct )
  1238.         ChromaDestroy( p_vout );
  1239.     /* Destroy all remaining pictures */
  1240.     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
  1241.     {
  1242.         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
  1243.         {
  1244.             free( p_vout->p_picture[i_index].p_data_orig );
  1245.         }
  1246.     }
  1247.     /* Destroy translation tables */
  1248.     if( !p_vout->b_error )
  1249.         p_vout->pf_end( p_vout );
  1250. }
  1251. /*****************************************************************************
  1252.  * EndThread: thread destruction
  1253.  *****************************************************************************
  1254.  * This function is called when the thread ends.
  1255.  * It frees all resources not allocated by InitThread.
  1256.  * XXX You have to enter it with change_lock taken.
  1257.  *****************************************************************************/
  1258. static void EndThread( vout_thread_t *p_vout )
  1259. {
  1260. #ifdef STATS
  1261.     {
  1262.         struct tms cpu_usage;
  1263.         times( &cpu_usage );
  1264.         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
  1265.                  cpu_usage.tms_utime, cpu_usage.tms_stime );
  1266.     }
  1267. #endif
  1268.     /* FIXME does that function *really* need to be called inside the thread ? */
  1269.     /* Detach subpicture unit from both input and vout */
  1270.     spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), false );
  1271.     vlc_object_detach( p_vout->p_spu );
  1272.     /* Destroy the video filters2 */
  1273.     filter_chain_Delete( p_vout->p->p_vf2_chain );
  1274. }
  1275. /* Thread helpers */
  1276. static picture_t *ChromaGetPicture( filter_t *p_filter )
  1277. {
  1278.     picture_t *p_pic = (picture_t *)p_filter->p_owner;
  1279.     p_filter->p_owner = NULL;
  1280.     return p_pic;
  1281. }
  1282. static int ChromaCreate( vout_thread_t *p_vout )
  1283. {
  1284.     static const char typename[] = "chroma";
  1285.     filter_t *p_chroma;
  1286.     /* Choose the best module */
  1287.     p_chroma = p_vout->p->p_chroma =
  1288.         vlc_custom_create( p_vout, sizeof(filter_t), VLC_OBJECT_GENERIC,
  1289.                            typename );
  1290.     vlc_object_attach( p_chroma, p_vout );
  1291.     /* TODO: Set the fmt_in and fmt_out stuff here */
  1292.     p_chroma->fmt_in.video = p_vout->fmt_render;
  1293.     p_chroma->fmt_out.video = p_vout->fmt_out;
  1294.     VideoFormatImportRgb( &p_chroma->fmt_in.video, &p_vout->render );
  1295.     VideoFormatImportRgb( &p_chroma->fmt_out.video, &p_vout->output );
  1296.     p_chroma->p_module = module_need( p_chroma, "video filter2", NULL, false );
  1297.     if( p_chroma->p_module == NULL )
  1298.     {
  1299.         msg_Err( p_vout, "no chroma module for %4.4s to %4.4s i=%dx%d o=%dx%d",
  1300.                  (char*)&p_vout->render.i_chroma,
  1301.                  (char*)&p_vout->output.i_chroma,
  1302.                  p_chroma->fmt_in.video.i_width, p_chroma->fmt_in.video.i_height,
  1303.                  p_chroma->fmt_out.video.i_width, p_chroma->fmt_out.video.i_height
  1304.                  );
  1305.         vlc_object_release( p_vout->p->p_chroma );
  1306.         p_vout->p->p_chroma = NULL;
  1307.         return VLC_EGENERIC;
  1308.     }
  1309.     p_chroma->pf_vout_buffer_new = ChromaGetPicture;
  1310.     return VLC_SUCCESS;
  1311. }
  1312. static void ChromaDestroy( vout_thread_t *p_vout )
  1313. {
  1314.     assert( !p_vout->p->b_direct );
  1315.     if( !p_vout->p->p_chroma )
  1316.         return;
  1317.     module_unneed( p_vout->p->p_chroma, p_vout->p->p_chroma->p_module );
  1318.     vlc_object_release( p_vout->p->p_chroma );
  1319.     p_vout->p->p_chroma = NULL;
  1320. }
  1321. /* following functions are local */
  1322. static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
  1323. {
  1324.     const int i_pgcd = i_aspect ? GCD( i_aspect, VOUT_ASPECT_FACTOR ) : 1;
  1325.     *i_aspect_x = i_aspect / i_pgcd;
  1326.     *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
  1327. }
  1328. /**
  1329.  * This function copies all RGB informations from a picture_heap_t into
  1330.  * a video_format_t
  1331.  */
  1332. static void VideoFormatImportRgb( video_format_t *p_fmt, const picture_heap_t *p_heap )
  1333. {
  1334.     p_fmt->i_rmask = p_heap->i_rmask;
  1335.     p_fmt->i_gmask = p_heap->i_gmask;
  1336.     p_fmt->i_bmask = p_heap->i_bmask;
  1337.     p_fmt->i_rrshift = p_heap->i_rrshift;
  1338.     p_fmt->i_lrshift = p_heap->i_lrshift;
  1339.     p_fmt->i_rgshift = p_heap->i_rgshift;
  1340.     p_fmt->i_lgshift = p_heap->i_lgshift;
  1341.     p_fmt->i_rbshift = p_heap->i_rbshift;
  1342.     p_fmt->i_lbshift = p_heap->i_lbshift;
  1343. }
  1344. /**
  1345.  * This funtion copes all RGB informations from a video_format_t into
  1346.  * a picture_heap_t
  1347.  */
  1348. static void VideoFormatExportRgb( const video_format_t *p_fmt, picture_heap_t *p_heap )
  1349. {
  1350.     p_heap->i_rmask = p_fmt->i_rmask;
  1351.     p_heap->i_gmask = p_fmt->i_gmask;
  1352.     p_heap->i_bmask = p_fmt->i_bmask;
  1353.     p_heap->i_rrshift = p_fmt->i_rrshift;
  1354.     p_heap->i_lrshift = p_fmt->i_lrshift;
  1355.     p_heap->i_rgshift = p_fmt->i_rgshift;
  1356.     p_heap->i_lgshift = p_fmt->i_lgshift;
  1357.     p_heap->i_rbshift = p_fmt->i_rbshift;
  1358.     p_heap->i_lbshift = p_fmt->i_lbshift;
  1359. }
  1360. /**
  1361.  * This function computes rgb shifts from masks
  1362.  */
  1363. static void PictureHeapFixRgb( picture_heap_t *p_heap )
  1364. {
  1365.     video_format_t fmt;
  1366.     /* */
  1367.     fmt.i_chroma = p_heap->i_chroma;
  1368.     VideoFormatImportRgb( &fmt, p_heap );
  1369.     /* */
  1370.     video_format_FixRgb( &fmt );
  1371.     VideoFormatExportRgb( &fmt, p_heap );
  1372. }
  1373. /*****************************************************************************
  1374.  * object variables callbacks: a bunch of object variables are used by the
  1375.  * interfaces to interact with the vout.
  1376.  *****************************************************************************/
  1377. static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
  1378.                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
  1379. {
  1380.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  1381.     input_thread_t *p_input;
  1382.     (void)psz_cmd; (void)oldval; (void)p_data;
  1383.     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
  1384.                                                  FIND_PARENT );
  1385.     if (!p_input)
  1386.     {
  1387.         msg_Err( p_vout, "Input not found" );
  1388.         return VLC_EGENERIC;
  1389.     }
  1390.     var_SetBool( p_vout, "intf-change", true );
  1391.     /* Modify input as well because the vout might have to be restarted */
  1392.     var_Create( p_input, "vout-filter", VLC_VAR_STRING );
  1393.     var_SetString( p_input, "vout-filter", newval.psz_string );
  1394.     /* Now restart current video stream */
  1395.     input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
  1396.     vlc_object_release( p_input );
  1397.     return VLC_SUCCESS;
  1398. }
  1399. /*****************************************************************************
  1400.  * Video Filter2 stuff
  1401.  *****************************************************************************/
  1402. static int VideoFilter2Callback( vlc_object_t *p_this, char const *psz_cmd,
  1403.                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
  1404. {
  1405.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  1406.     (void)psz_cmd; (void)oldval; (void)p_data;
  1407.     vlc_mutex_lock( &p_vout->p->vfilter_lock );
  1408.     p_vout->p->psz_vf2 = strdup( newval.psz_string );
  1409.     vlc_mutex_unlock( &p_vout->p->vfilter_lock );
  1410.     return VLC_SUCCESS;
  1411. }
  1412. /*****************************************************************************
  1413.  * Post-processing
  1414.  *****************************************************************************/
  1415. static bool PostProcessIsPresent( const char *psz_filter )
  1416. {
  1417.     const char  *psz_pp = "postproc";
  1418.     const size_t i_pp = strlen(psz_pp);
  1419.     return psz_filter &&
  1420.            !strncmp( psz_filter, psz_pp, strlen(psz_pp) ) &&
  1421.            ( psz_filter[i_pp] == '' || psz_filter[i_pp] == ':' );
  1422. }
  1423. static int PostProcessCallback( vlc_object_t *p_this, char const *psz_cmd,
  1424.                                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
  1425. {
  1426.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  1427.     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
  1428.     static const char *psz_pp = "postproc";
  1429.     char *psz_vf2 = var_GetString( p_vout, "video-filter" );
  1430.     if( newval.i_int <= 0 )
  1431.     {
  1432.         if( PostProcessIsPresent( psz_vf2 ) )
  1433.         {
  1434.             strcpy( psz_vf2, &psz_vf2[strlen(psz_pp)] );
  1435.             if( *psz_vf2 == ':' )
  1436.                 strcpy( psz_vf2, &psz_vf2[1] );
  1437.         }
  1438.     }
  1439.     else
  1440.     {
  1441.         if( !PostProcessIsPresent( psz_vf2 ) )
  1442.         {
  1443.             if( psz_vf2 )
  1444.             {
  1445.                 char *psz_tmp = psz_vf2;
  1446.                 if( asprintf( &psz_vf2, "%s:%s", psz_pp, psz_tmp ) < 0 )
  1447.                     psz_vf2 = psz_tmp;
  1448.                 else
  1449.                     free( psz_tmp );
  1450.             }
  1451.             else
  1452.             {
  1453.                 psz_vf2 = strdup( psz_pp );
  1454.             }
  1455.         }
  1456.     }
  1457.     if( psz_vf2 )
  1458.     {
  1459.         var_SetString( p_vout, "video-filter", psz_vf2 );
  1460.         free( psz_vf2 );
  1461.     }
  1462.     return VLC_SUCCESS;
  1463. }
  1464. static void PostProcessEnable( vout_thread_t *p_vout )
  1465. {
  1466.     vlc_value_t text;
  1467.     msg_Dbg( p_vout, "Post-processing available" );
  1468.     var_Create( p_vout, "postprocess", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
  1469.     text.psz_string = _("Post processing");
  1470.     var_Change( p_vout, "postprocess", VLC_VAR_SETTEXT, &text, NULL );
  1471.     for( int i = 0; i <= 6; i++ )
  1472.     {
  1473.         vlc_value_t val;
  1474.         vlc_value_t text;
  1475.         char psz_text[1+1];
  1476.         val.i_int = i;
  1477.         snprintf( psz_text, sizeof(psz_text), "%d", i );
  1478.         if( i == 0 )
  1479.             text.psz_string = _("Disable");
  1480.         else
  1481.             text.psz_string = psz_text;
  1482.         var_Change( p_vout, "postprocess", VLC_VAR_ADDCHOICE, &val, &text );
  1483.     }
  1484.     var_AddCallback( p_vout, "postprocess", PostProcessCallback, NULL );
  1485.     /* */
  1486.     char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
  1487.     int i_postproc_q = 0;
  1488.     if( PostProcessIsPresent( psz_filter ) )
  1489.         i_postproc_q = var_CreateGetInteger( p_vout, "postproc-q" );
  1490.     var_SetInteger( p_vout, "postprocess", i_postproc_q );
  1491.     free( psz_filter );
  1492. }
  1493. static void PostProcessDisable( vout_thread_t *p_vout )
  1494. {
  1495.     msg_Dbg( p_vout, "Post-processing no more available" );
  1496.     var_Destroy( p_vout, "postprocess" );
  1497. }
  1498. static void PostProcessSetFilterQuality( vout_thread_t *p_vout )
  1499. {
  1500.     vlc_object_t *p_pp = vlc_object_find_name( p_vout, "postproc", FIND_CHILD );
  1501.     if( !p_pp )
  1502.         return;
  1503.     var_SetInteger( p_pp, "postproc-q", var_GetInteger( p_vout, "postprocess" ) );
  1504.     vlc_object_release( p_pp );
  1505. }
  1506. static void DisplayTitleOnOSD( vout_thread_t *p_vout )
  1507. {
  1508.     const mtime_t i_start = mdate();
  1509.     const mtime_t i_stop = i_start + INT64_C(1000) * p_vout->p->i_title_timeout;
  1510.     if( i_stop <= i_start )
  1511.         return;
  1512.     vlc_assert_locked( &p_vout->change_lock );
  1513.     vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
  1514.                            p_vout->p->psz_title, NULL,
  1515.                            p_vout->p->i_title_position,
  1516.                            30 + p_vout->fmt_in.i_width
  1517.                               - p_vout->fmt_in.i_visible_width
  1518.                               - p_vout->fmt_in.i_x_offset,
  1519.                            20 + p_vout->fmt_in.i_y_offset,
  1520.                            i_start, i_stop );
  1521.     free( p_vout->p->psz_title );
  1522.     p_vout->p->psz_title = NULL;
  1523. }
  1524. /*****************************************************************************
  1525.  * Deinterlacing
  1526.  *****************************************************************************/
  1527. typedef struct
  1528. {
  1529.     const char *psz_mode;
  1530.     const char *psz_description;
  1531.     bool       b_vout_filter;
  1532. } deinterlace_mode_t;
  1533. /* XXX
  1534.  * You can use the non vout filter if and only if the video properties stay the
  1535.  * same (width/height/chroma/fps), at least for now.
  1536.  */
  1537. static const deinterlace_mode_t p_deinterlace_mode[] = {
  1538.     { "",           "Disable",  false },
  1539.     { "discard",    "Discard",  true },
  1540.     { "blend",      "Blend",    false },
  1541.     { "mean",       "Mean",     true  },
  1542.     { "bob",        "Bob",      true },
  1543.     { "linear",     "Linear",   true },
  1544.     { "x",          "X",        false },
  1545.     { "yadif",      "Yadif",    true },
  1546.     { "yadif2x",    "Yadif (2x)", true },
  1547.     { NULL, NULL, true }
  1548. };
  1549. static char *FilterFind( char *psz_filter_base, const char *psz_module )
  1550. {
  1551.     const size_t i_module = strlen( psz_module );
  1552.     const char *psz_filter = psz_filter_base;
  1553.     if( !psz_filter || i_module <= 0 )
  1554.         return NULL;
  1555.     for( ;; )
  1556.     {
  1557.         char *psz_find = strstr( psz_filter, psz_module );
  1558.         if( !psz_find )
  1559.             return NULL;
  1560.         if( psz_find[i_module] == '' || psz_find[i_module] == ':' )
  1561.             return psz_find;
  1562.         psz_filter = &psz_find[i_module];
  1563.     }
  1564. }
  1565. static bool DeinterlaceIsPresent( vout_thread_t *p_vout, bool b_vout_filter )
  1566. {
  1567.     char *psz_filter = var_GetNonEmptyString( p_vout, b_vout_filter ? "vout-filter" : "video-filter" );
  1568.     bool b_found = FilterFind( psz_filter, "deinterlace" ) != NULL;
  1569.     free( psz_filter );
  1570.     return b_found;
  1571. }
  1572. static void DeinterlaceRemove( vout_thread_t *p_vout, bool b_vout_filter )
  1573. {
  1574.     const char *psz_variable = b_vout_filter ? "vout-filter" : "video-filter";
  1575.     char *psz_filter = var_GetNonEmptyString( p_vout, psz_variable );
  1576.     char *psz = FilterFind( psz_filter, "deinterlace" );
  1577.     if( !psz )
  1578.     {
  1579.         free( psz_filter );
  1580.         return;
  1581.     }
  1582.     /* */
  1583.     strcpy( &psz[0], &psz[strlen("deinterlace")] );
  1584.     if( *psz == ':' )
  1585.         strcpy( &psz[0], &psz[1] );
  1586.     var_SetString( p_vout, psz_variable, psz_filter );
  1587.     free( psz_filter );
  1588. }
  1589. static void DeinterlaceAdd( vout_thread_t *p_vout, bool b_vout_filter )
  1590. {
  1591.     const char *psz_variable = b_vout_filter ? "vout-filter" : "video-filter";
  1592.     char *psz_filter = var_GetNonEmptyString( p_vout, psz_variable );
  1593.     if( FilterFind( psz_filter, "deinterlace" ) )
  1594.     {
  1595.         free( psz_filter );
  1596.         return;
  1597.     }
  1598.     /* */
  1599.     if( psz_filter )
  1600.     {
  1601.         char *psz_tmp = psz_filter;
  1602.         if( asprintf( &psz_filter, "%s:%s", psz_tmp, "deinterlace" ) < 0 )
  1603.             psz_filter = psz_tmp;
  1604.         else
  1605.             free( psz_tmp );
  1606.     }
  1607.     else
  1608.     {
  1609.         psz_filter = strdup( "deinterlace" );
  1610.     }
  1611.     if( psz_filter )
  1612.     {
  1613.         var_SetString( p_vout, psz_variable, psz_filter );
  1614.         free( psz_filter );
  1615.     }
  1616. }
  1617. static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
  1618.                                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
  1619. {
  1620.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  1621.     /* */
  1622.     const deinterlace_mode_t *p_mode;
  1623.     for( p_mode = &p_deinterlace_mode[0]; p_mode->psz_mode; p_mode++ )
  1624.     {
  1625.         if( !strcmp( p_mode->psz_mode,
  1626.                      newval.psz_string ? newval.psz_string : "" ) )
  1627.             break;
  1628.     }
  1629.     if( !p_mode->psz_mode )
  1630.     {
  1631.         msg_Err( p_this, "Invalid value (%s) ignored", newval.psz_string );
  1632.         return VLC_EGENERIC;
  1633.     }
  1634.     /* We have to set input variable to ensure restart support
  1635.      * XXX it is only needed because of vout-filter but must be done
  1636.      * for non video filter anyway */
  1637.     input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
  1638.     if( p_input )
  1639.     {
  1640.         var_Create( p_input, "vout-deinterlace", VLC_VAR_STRING );
  1641.         var_SetString( p_input, "vout-deinterlace", p_mode->psz_mode );
  1642.         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
  1643.         var_SetString( p_input, "deinterlace-mode", p_mode->psz_mode );
  1644.         var_Create( p_input, "sout-deinterlace-mode", VLC_VAR_STRING );
  1645.         var_SetString( p_input, "sout-deinterlace-mode", p_mode->psz_mode );
  1646.         vlc_object_release( p_input );
  1647.     }
  1648.     char *psz_old;
  1649.     if( p_mode->b_vout_filter )
  1650.     {
  1651.         psz_old = var_CreateGetString( p_vout, "deinterlace-mode" );
  1652.     }
  1653.     else
  1654.     {
  1655.         psz_old = var_CreateGetString( p_vout, "sout-deinterlace-mode" );
  1656.         var_SetString( p_vout, "sout-deinterlace-mode", p_mode->psz_mode );
  1657.     }
  1658.     /* */
  1659.     if( !strcmp( p_mode->psz_mode, "" ) )
  1660.     {
  1661.         DeinterlaceRemove( p_vout, false );
  1662.         DeinterlaceRemove( p_vout, true );
  1663.     }
  1664.     else if( !DeinterlaceIsPresent( p_vout, p_mode->b_vout_filter ) )
  1665.     {
  1666.         DeinterlaceRemove( p_vout, !p_mode->b_vout_filter );
  1667.         DeinterlaceAdd( p_vout, p_mode->b_vout_filter );
  1668.     }
  1669.     else
  1670.     {
  1671.         DeinterlaceRemove( p_vout, !p_mode->b_vout_filter );
  1672.         if( psz_old && strcmp( psz_old, p_mode->psz_mode ) )
  1673.             var_TriggerCallback( p_vout, p_mode->b_vout_filter ? "vout-filter" : "video-filter" );
  1674.     }
  1675.     free( psz_old );
  1676.     (void)psz_cmd; (void) oldval; (void) p_data;
  1677.     return VLC_SUCCESS;
  1678. }
  1679. static void DeinterlaceEnable( vout_thread_t *p_vout )
  1680. {
  1681.     vlc_value_t val, text;
  1682.     if( !p_vout->p->b_first_vout )
  1683.         return;
  1684.     msg_Dbg( p_vout, "Deinterlacing available" );
  1685.     /* Create the configuration variable */
  1686.     var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
  1687.     text.psz_string = _("Deinterlace");
  1688.     var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
  1689.     for( int i = 0; p_deinterlace_mode[i].psz_mode; i++ )
  1690.     {
  1691.         val.psz_string  = (char*)p_deinterlace_mode[i].psz_mode;
  1692.         text.psz_string = (char*)vlc_gettext(p_deinterlace_mode[i].psz_description);
  1693.         var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
  1694.     }
  1695.     var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
  1696.     /* */
  1697.     char *psz_mode = NULL;
  1698.     if( var_Type( p_vout, "vout-deinterlace" ) != 0 )
  1699.         psz_mode = var_CreateGetNonEmptyString( p_vout, "vout-deinterlace" );
  1700.     if( !psz_mode )
  1701.     {
  1702.         /* Get the initial value */
  1703.         if( DeinterlaceIsPresent( p_vout, true ) )
  1704.             psz_mode = var_CreateGetNonEmptyString( p_vout, "deinterlace-mode" );
  1705.         else if( DeinterlaceIsPresent( p_vout, false ) )
  1706.             psz_mode = var_CreateGetNonEmptyString( p_vout, "sout-deinterlace-mode" );
  1707.     }
  1708.     var_SetString( p_vout, "deinterlace", psz_mode ? psz_mode : "" );
  1709.     free( psz_mode );
  1710. }