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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * transcode.c: transcoding stream output module
  3.  *****************************************************************************
  4.  * Copyright (C) 2003-2008 the VideoLAN team
  5.  * $Id: 5fc0f6f9590c2f6ae681aeffdd546b19a8ab73e5 $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *          Gildas Bazin <gbazin@videolan.org>
  9.  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
  10.  *          Antoine Cellerier <dionoea at videolan dot org>
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25.  *****************************************************************************/
  26. /*****************************************************************************
  27.  * Preamble
  28.  *****************************************************************************/
  29. #ifdef HAVE_CONFIG_H
  30. # include "config.h"
  31. #endif
  32. #include <vlc_common.h>
  33. #include <vlc_plugin.h>
  34. #include <vlc_input.h>
  35. #include <vlc_sout.h>
  36. #include <vlc_aout.h>
  37. #include <vlc_vout.h>
  38. #include <vlc_codec.h>
  39. #include <vlc_meta.h>
  40. #include <vlc_block.h>
  41. #include <vlc_filter.h>
  42. #include <vlc_osd.h>
  43. #include <math.h>
  44. #define MASTER_SYNC_MAX_DRIFT 100000
  45. #include <assert.h>
  46. /*****************************************************************************
  47.  * Module descriptor
  48.  *****************************************************************************/
  49. #define VENC_TEXT N_("Video encoder")
  50. #define VENC_LONGTEXT N_( 
  51.     "This is the video encoder module that will be used (and its associated "
  52.     "options).")
  53. #define VCODEC_TEXT N_("Destination video codec")
  54. #define VCODEC_LONGTEXT N_( 
  55.     "This is the video codec that will be used.")
  56. #define VB_TEXT N_("Video bitrate")
  57. #define VB_LONGTEXT N_( 
  58.     "Target bitrate of the transcoded video stream." )
  59. #define SCALE_TEXT N_("Video scaling")
  60. #define SCALE_LONGTEXT N_( 
  61.     "Scale factor to apply to the video while transcoding (eg: 0.25)")
  62. #define FPS_TEXT N_("Video frame-rate")
  63. #define FPS_LONGTEXT N_( 
  64.     "Target output frame rate for the video stream." )
  65. #define DEINTERLACE_TEXT N_("Deinterlace video")
  66. #define DEINTERLACE_LONGTEXT N_( 
  67.     "Deinterlace the video before encoding." )
  68. #define DEINTERLACE_MODULE_TEXT N_("Deinterlace module")
  69. #define DEINTERLACE_MODULE_LONGTEXT N_( 
  70.     "Specify the deinterlace module to use." )
  71. #define WIDTH_TEXT N_("Video width")
  72. #define WIDTH_LONGTEXT N_( 
  73.     "Output video width." )
  74. #define HEIGHT_TEXT N_("Video height")
  75. #define HEIGHT_LONGTEXT N_( 
  76.     "Output video height." )
  77. #define MAXWIDTH_TEXT N_("Maximum video width")
  78. #define MAXWIDTH_LONGTEXT N_( 
  79.     "Maximum output video width." )
  80. #define MAXHEIGHT_TEXT N_("Maximum video height")
  81. #define MAXHEIGHT_LONGTEXT N_( 
  82.     "Maximum output video height." )
  83. #define VFILTER_TEXT N_("Video filter")
  84. #define VFILTER_LONGTEXT N_( 
  85.     "Video filters will be applied to the video streams (after overlays " 
  86.     "are applied). You must enter a comma-separated list of filters." )
  87. #define AENC_TEXT N_("Audio encoder")
  88. #define AENC_LONGTEXT N_( 
  89.     "This is the audio encoder module that will be used (and its associated "
  90.     "options).")
  91. #define ACODEC_TEXT N_("Destination audio codec")
  92. #define ACODEC_LONGTEXT N_( 
  93.     "This is the audio codec that will be used.")
  94. #define AB_TEXT N_("Audio bitrate")
  95. #define AB_LONGTEXT N_( 
  96.     "Target bitrate of the transcoded audio stream." )
  97. #define ARATE_TEXT N_("Audio sample rate")
  98. #define ARATE_LONGTEXT N_( 
  99.  "Sample rate of the transcoded audio stream (11250, 22500, 44100 or 48000).")
  100. #define ACHANS_TEXT N_("Audio channels")
  101. #define ACHANS_LONGTEXT N_( 
  102.     "Number of audio channels in the transcoded streams." )
  103. #define AFILTER_TEXT N_("Audio filter")
  104. #define AFILTER_LONGTEXT N_( 
  105.     "Audio filters will be applied to the audio streams (after conversion " 
  106.     "filters are applied). You must enter a comma-separated list of filters." )
  107. #define SENC_TEXT N_("Subtitles encoder")
  108. #define SENC_LONGTEXT N_( 
  109.     "This is the subtitles encoder module that will be used (and its " 
  110.     "associated options)." )
  111. #define SCODEC_TEXT N_("Destination subtitles codec")
  112. #define SCODEC_LONGTEXT N_( 
  113.     "This is the subtitles codec that will be used." )
  114. #define SFILTER_TEXT N_("Overlays")
  115. #define SFILTER_LONGTEXT N_( 
  116.     "This allows you to add overlays (also known as "subpictures" on the "
  117.     "transcoded video stream. The subpictures produced by the filters will "
  118.     "be overlayed directly onto the video. You must specify a comma-separated "
  119.     "list of subpicture modules" )
  120. #define OSD_TEXT N_("OSD menu")
  121. #define OSD_LONGTEXT N_(
  122.     "Stream the On Screen Display menu (using the osdmenu subpicture module)." )
  123. #define THREADS_TEXT N_("Number of threads")
  124. #define THREADS_LONGTEXT N_( 
  125.     "Number of threads used for the transcoding." )
  126. #define HP_TEXT N_("High priority")
  127. #define HP_LONGTEXT N_( 
  128.     "Runs the optional encoder thread at the OUTPUT priority instead of " 
  129.     "VIDEO." )
  130. #define ASYNC_TEXT N_("Synchronise on audio track")
  131. #define ASYNC_LONGTEXT N_( 
  132.     "This option will drop/duplicate video frames to synchronise the video " 
  133.     "track on the audio track." )
  134. #define HURRYUP_TEXT N_( "Hurry up" )
  135. #define HURRYUP_LONGTEXT N_( "The transcoder will drop frames if your CPU " 
  136.                 "can't keep up with the encoding rate." )
  137. static const char *const ppsz_deinterlace_type[] =
  138. {
  139.     "deinterlace", "ffmpeg-deinterlace"
  140. };
  141. static int  Open ( vlc_object_t * );
  142. static void Close( vlc_object_t * );
  143. #define SOUT_CFG_PREFIX "sout-transcode-"
  144. vlc_module_begin ()
  145.     set_shortname( N_("Transcode"))
  146.     set_description( N_("Transcode stream output") )
  147.     set_capability( "sout stream", 50 )
  148.     add_shortcut( "transcode" )
  149.     set_callbacks( Open, Close )
  150.     set_category( CAT_SOUT )
  151.     set_subcategory( SUBCAT_SOUT_STREAM )
  152.     set_section( N_("Video"), NULL )
  153.     add_module( SOUT_CFG_PREFIX "venc", "encoder", NULL, NULL, VENC_TEXT,
  154.                 VENC_LONGTEXT, false )
  155.     add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
  156.                 VCODEC_LONGTEXT, false )
  157.     add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
  158.                  VB_LONGTEXT, false )
  159.     add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
  160.                SCALE_LONGTEXT, false )
  161.     add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
  162.                FPS_LONGTEXT, false )
  163.     add_bool( SOUT_CFG_PREFIX "hurry-up", true, NULL, HURRYUP_TEXT,
  164.                HURRYUP_LONGTEXT, false )
  165.     add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
  166.               DEINTERLACE_LONGTEXT, false )
  167.     add_string( SOUT_CFG_PREFIX "deinterlace-module", "deinterlace", NULL,
  168.                 DEINTERLACE_MODULE_TEXT, DEINTERLACE_MODULE_LONGTEXT,
  169.                 false )
  170.         change_string_list( ppsz_deinterlace_type, 0, 0 )
  171.     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
  172.                  WIDTH_LONGTEXT, true )
  173.     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
  174.                  HEIGHT_LONGTEXT, true )
  175.     add_integer( SOUT_CFG_PREFIX "maxwidth", 0, NULL, MAXWIDTH_TEXT,
  176.                  MAXWIDTH_LONGTEXT, true )
  177.     add_integer( SOUT_CFG_PREFIX "maxheight", 0, NULL, MAXHEIGHT_TEXT,
  178.                  MAXHEIGHT_LONGTEXT, true )
  179.     add_module_list( SOUT_CFG_PREFIX "vfilter", "video filter2",
  180.                      NULL, NULL,
  181.                      VFILTER_TEXT, VFILTER_LONGTEXT, false )
  182.     set_section( N_("Audio"), NULL )
  183.     add_module( SOUT_CFG_PREFIX "aenc", "encoder", NULL, NULL, AENC_TEXT,
  184.                 AENC_LONGTEXT, false )
  185.     add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
  186.                 ACODEC_LONGTEXT, false )
  187.     add_integer( SOUT_CFG_PREFIX "ab", 0, NULL, AB_TEXT,
  188.                  AB_LONGTEXT, false )
  189.     add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
  190.                  ACHANS_LONGTEXT, false )
  191.     add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
  192.                  ARATE_LONGTEXT, true )
  193.     add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
  194.               ASYNC_LONGTEXT, false )
  195.     add_module_list( SOUT_CFG_PREFIX "afilter",  "audio filter2",
  196.                      NULL, NULL,
  197.                      AFILTER_TEXT, AFILTER_LONGTEXT, false )
  198.     set_section( N_("Overlays/Subtitles"), NULL )
  199.     add_module( SOUT_CFG_PREFIX "senc", "encoder", NULL, NULL, SENC_TEXT,
  200.                 SENC_LONGTEXT, false )
  201.     add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
  202.                 SCODEC_LONGTEXT, false )
  203.     add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
  204.                SCODEC_LONGTEXT, false )
  205.     add_module_list( SOUT_CFG_PREFIX "sfilter", "video filter",
  206.                      NULL, NULL,
  207.                      SFILTER_TEXT, SFILTER_LONGTEXT, false )
  208.     set_section( N_("On Screen Display"), NULL )
  209.     add_bool( SOUT_CFG_PREFIX "osd", 0, NULL, OSD_TEXT,
  210.               OSD_LONGTEXT, false )
  211.     set_section( N_("Miscellaneous"), NULL )
  212.     add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
  213.                  THREADS_LONGTEXT, true )
  214.     add_bool( SOUT_CFG_PREFIX "high-priority", 0, NULL, HP_TEXT, HP_LONGTEXT,
  215.               true )
  216. vlc_module_end ()
  217. static const char *const ppsz_sout_options[] = {
  218.     "venc", "vcodec", "vb",
  219.     "scale", "fps", "width", "height", "vfilter", "deinterlace",
  220.     "deinterlace-module", "threads", "hurry-up", "aenc", "acodec", "ab",
  221.     "afilter", "samplerate", "channels", "senc", "scodec", "soverlay",
  222.     "sfilter", "osd", "audio-sync", "high-priority", "maxwidth", "maxheight",
  223.     NULL
  224. };
  225. /*****************************************************************************
  226.  * Exported prototypes
  227.  *****************************************************************************/
  228. static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
  229. static int               Del ( sout_stream_t *, sout_stream_id_t * );
  230. static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
  231. static int  transcode_audio_new    ( sout_stream_t *, sout_stream_id_t * );
  232. static void transcode_audio_close  ( sout_stream_id_t * );
  233. static int  transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
  234.                                      block_t *, block_t ** );
  235. static aout_buffer_t *audio_new_buffer( decoder_t *, int );
  236. static void audio_del_buffer( decoder_t *, aout_buffer_t * );
  237. static int  transcode_video_new    ( sout_stream_t *, sout_stream_id_t * );
  238. static void transcode_video_close  ( sout_stream_t *, sout_stream_id_t * );
  239. static void transcode_video_encoder_init( sout_stream_t *, sout_stream_id_t *);
  240. static int  transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
  241. static int  transcode_video_process( sout_stream_t *, sout_stream_id_t *,
  242.                                      block_t *, block_t ** );
  243. static void video_del_buffer( vlc_object_t *, picture_t * );
  244. static picture_t *video_new_buffer_decoder( decoder_t * );
  245. static void video_del_buffer_decoder( decoder_t *, picture_t * );
  246. static void video_link_picture_decoder( decoder_t *, picture_t * );
  247. static void video_unlink_picture_decoder( decoder_t *, picture_t * );
  248. static picture_t *video_new_buffer_filter( filter_t * );
  249. static void video_del_buffer_filter( filter_t *, picture_t * );
  250. static int  transcode_spu_new    ( sout_stream_t *, sout_stream_id_t * );
  251. static void transcode_spu_close  ( sout_stream_id_t * );
  252. static int  transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
  253.                                    block_t *, block_t ** );
  254. static int  transcode_osd_new    ( sout_stream_t *, sout_stream_id_t * );
  255. static void transcode_osd_close  ( sout_stream_t *, sout_stream_id_t * );
  256. static int  transcode_osd_process( sout_stream_t *, sout_stream_id_t *,
  257.                                    block_t *, block_t ** );
  258. static void* EncoderThread( vlc_object_t * p_this );
  259. static const int pi_channels_maps[6] =
  260. {
  261.     0,
  262.     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
  263.     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
  264.     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
  265.      | AOUT_CHAN_REARRIGHT,
  266.     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
  267.      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  268. };
  269. #define PICTURE_RING_SIZE 64
  270. #define SUBPICTURE_RING_SIZE 20
  271. #define ENC_FRAMERATE (25 * 1000 + .5)
  272. #define ENC_FRAMERATE_BASE 1000
  273. struct sout_stream_sys_t
  274. {
  275.     VLC_COMMON_MEMBERS
  276.     sout_stream_t   *p_out;
  277.     sout_stream_id_t *id_video;
  278.     block_t         *p_buffers;
  279.     vlc_mutex_t     lock_out;
  280.     vlc_cond_t      cond;
  281.     picture_t *     pp_pics[PICTURE_RING_SIZE];
  282.     int             i_first_pic, i_last_pic;
  283.     /* Audio */
  284.     vlc_fourcc_t    i_acodec;   /* codec audio (0 if not transcode) */
  285.     char            *psz_aenc;
  286.     config_chain_t  *p_audio_cfg;
  287.     uint32_t        i_sample_rate;
  288.     uint32_t        i_channels;
  289.     int             i_abitrate;
  290.     char            *psz_af2;
  291.     /* Video */
  292.     vlc_fourcc_t    i_vcodec;   /* codec video (0 if not transcode) */
  293.     char            *psz_venc;
  294.     config_chain_t  *p_video_cfg;
  295.     int             i_vbitrate;
  296.     double          f_scale;
  297.     double          f_fps;
  298.     unsigned int    i_width, i_maxwidth;
  299.     unsigned int    i_height, i_maxheight;
  300.     bool            b_deinterlace;
  301.     char            *psz_deinterlace;
  302.     config_chain_t  *p_deinterlace_cfg;
  303.     int             i_threads;
  304.     bool            b_high_priority;
  305.     bool            b_hurry_up;
  306.     char            *psz_vf2;
  307.     /* SPU */
  308.     vlc_fourcc_t    i_scodec;   /* codec spu (0 if not transcode) */
  309.     char            *psz_senc;
  310.     bool            b_soverlay;
  311.     config_chain_t  *p_spu_cfg;
  312.     spu_t           *p_spu;
  313.     /* OSD Menu */
  314.     vlc_fourcc_t    i_osdcodec; /* codec osd menu (0 if not transcode) */
  315.     char            *psz_osdenc;
  316.     config_chain_t  *p_osd_cfg;
  317.     bool            b_osd;   /* true when osd es is registered */
  318.     /* Sync */
  319.     bool            b_master_sync;
  320.     mtime_t         i_master_drift;
  321. };
  322. struct decoder_owner_sys_t
  323. {
  324.     picture_t *pp_pics[PICTURE_RING_SIZE];
  325.     sout_stream_sys_t *p_sys;
  326. };
  327. struct filter_owner_sys_t
  328. {
  329.     picture_t *pp_pics[PICTURE_RING_SIZE];
  330.     sout_stream_sys_t *p_sys;
  331. };
  332. /*****************************************************************************
  333.  * Open:
  334.  *****************************************************************************/
  335. static int Open( vlc_object_t *p_this )
  336. {
  337.     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
  338.     sout_stream_sys_t *p_sys;
  339.     vlc_value_t       val;
  340.     p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
  341.     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
  342.     if( !p_sys->p_out )
  343.     {
  344.         msg_Err( p_stream, "cannot create chain" );
  345.         vlc_object_release( p_sys );
  346.         return VLC_EGENERIC;
  347.     }
  348.     p_sys->i_master_drift = 0;
  349.     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
  350.                    p_stream->p_cfg );
  351.     /* Audio transcoding parameters */
  352.     var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
  353.     p_sys->psz_aenc = NULL;
  354.     p_sys->p_audio_cfg = NULL;
  355.     if( val.psz_string && *val.psz_string )
  356.     {
  357.         char *psz_next;
  358.         psz_next = config_ChainCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
  359.                                        val.psz_string );
  360.         free( psz_next );
  361.     }
  362.     free( val.psz_string );
  363.     var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
  364.     p_sys->i_acodec = 0;
  365.     if( val.psz_string && *val.psz_string )
  366.     {
  367.         char fcc[4] = "    ";
  368.         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
  369.         p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
  370.     }
  371.     free( val.psz_string );
  372.     var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
  373.     p_sys->i_abitrate = val.i_int;
  374.     if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
  375.     var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
  376.     p_sys->i_sample_rate = val.i_int;
  377.     var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
  378.     p_sys->i_channels = val.i_int;
  379.     if( p_sys->i_acodec )
  380.     {
  381.         if( p_sys->i_acodec == VLC_FOURCC('m','p','3',0) &&
  382.             p_sys->i_channels > 2 )
  383.         {
  384.             msg_Warn( p_stream, "%d channels invalid for mp3, forcing to 2",
  385.                       p_sys->i_channels );
  386.             p_sys->i_channels = 2;
  387.         }
  388.         msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
  389.                  (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
  390.                  p_sys->i_channels, p_sys->i_abitrate / 1000 );
  391.     }
  392.     var_Get( p_stream, SOUT_CFG_PREFIX "afilter", &val );
  393.     if( val.psz_string && *val.psz_string )
  394.         p_sys->psz_af2 = val.psz_string;
  395.     else
  396.     {
  397.         free( val.psz_string );
  398.         p_sys->psz_af2 = NULL;
  399.     }
  400.     /* Video transcoding parameters */
  401.     var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
  402.     p_sys->psz_venc = NULL;
  403.     p_sys->p_video_cfg = NULL;
  404.     if( val.psz_string && *val.psz_string )
  405.     {
  406.         char *psz_next;
  407.         psz_next = config_ChainCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
  408.                                    val.psz_string );
  409.         free( psz_next );
  410.     }
  411.     free( val.psz_string );
  412.     var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
  413.     p_sys->i_vcodec = 0;
  414.     if( val.psz_string && *val.psz_string )
  415.     {
  416.         char fcc[4] = "    ";
  417.         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
  418.         p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
  419.     }
  420.     free( val.psz_string );
  421.     var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
  422.     p_sys->i_vbitrate = val.i_int;
  423.     if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
  424.     var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
  425.     p_sys->f_scale = val.f_float;
  426.     var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
  427.     p_sys->f_fps = val.f_float;
  428.     var_Get( p_stream, SOUT_CFG_PREFIX "hurry-up", &val );
  429.     p_sys->b_hurry_up = val.b_bool;
  430.     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
  431.     p_sys->i_width = val.i_int;
  432.     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
  433.     p_sys->i_height = val.i_int;
  434.     var_Get( p_stream, SOUT_CFG_PREFIX "maxwidth", &val );
  435.     p_sys->i_maxwidth = val.i_int;
  436.     var_Get( p_stream, SOUT_CFG_PREFIX "maxheight", &val );
  437.     p_sys->i_maxheight = val.i_int;
  438.     var_Get( p_stream, SOUT_CFG_PREFIX "vfilter", &val );
  439.     if( val.psz_string && *val.psz_string )
  440.         p_sys->psz_vf2 = val.psz_string;
  441.     else
  442.     {
  443.         free( val.psz_string );
  444.         p_sys->psz_vf2 = NULL;
  445.     }
  446.     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
  447.     p_sys->b_deinterlace = val.b_bool;
  448.     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace-module", &val );
  449.     p_sys->psz_deinterlace = NULL;
  450.     p_sys->p_deinterlace_cfg = NULL;
  451.     if( val.psz_string && *val.psz_string )
  452.     {
  453.         char *psz_next;
  454.         psz_next = config_ChainCreate( &p_sys->psz_deinterlace,
  455.                                    &p_sys->p_deinterlace_cfg,
  456.                                    val.psz_string );
  457.         free( psz_next );
  458.     }
  459.     free( val.psz_string );
  460.     var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
  461.     p_sys->i_threads = val.i_int;
  462.     var_Get( p_stream, SOUT_CFG_PREFIX "high-priority", &val );
  463.     p_sys->b_high_priority = val.b_bool;
  464.     if( p_sys->i_vcodec )
  465.     {
  466.         msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
  467.                  (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
  468.                  p_sys->f_scale, p_sys->i_vbitrate / 1000 );
  469.     }
  470.     /* Subpictures transcoding parameters */
  471.     p_sys->p_spu = NULL;
  472.     p_sys->psz_senc = NULL;
  473.     p_sys->p_spu_cfg = NULL;
  474.     p_sys->i_scodec = 0;
  475.     var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
  476.     if( val.psz_string && *val.psz_string )
  477.     {
  478.         char *psz_next;
  479.         psz_next = config_ChainCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
  480.                                    val.psz_string );
  481.         free( psz_next );
  482.     }
  483.     free( val.psz_string );
  484.     var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
  485.     if( val.psz_string && *val.psz_string )
  486.     {
  487.         char fcc[4] = "    ";
  488.         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
  489.         p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
  490.     }
  491.     free( val.psz_string );
  492.     if( p_sys->i_scodec )
  493.     {
  494.         msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_scodec );
  495.     }
  496.     var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
  497.     p_sys->b_soverlay = val.b_bool;
  498.     var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
  499.     if( val.psz_string && *val.psz_string )
  500.     {
  501.         p_sys->p_spu = spu_Create( p_stream );
  502.         var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
  503.         var_Set( p_sys->p_spu, "sub-filter", val );
  504.         spu_Init( p_sys->p_spu );
  505.     }
  506.     free( val.psz_string );
  507.     /* OSD menu transcoding parameters */
  508.     p_sys->psz_osdenc = NULL;
  509.     p_sys->p_osd_cfg  = NULL;
  510.     p_sys->i_osdcodec = 0;
  511.     p_sys->b_osd   = false;
  512.     var_Get( p_stream, SOUT_CFG_PREFIX "osd", &val );
  513.     if( val.b_bool )
  514.     {
  515.         vlc_value_t osd_val;
  516.         char *psz_next;
  517.         psz_next = config_ChainCreate( &p_sys->psz_osdenc,
  518.                                    &p_sys->p_osd_cfg, strdup( "dvbsub") );
  519.         free( psz_next );
  520.         p_sys->i_osdcodec = VLC_FOURCC('Y','U','V','P' );
  521.         msg_Dbg( p_stream, "codec osd=%4.4s", (char *)&p_sys->i_osdcodec );
  522.         if( !p_sys->p_spu )
  523.         {
  524.             osd_val.psz_string = strdup("osdmenu");
  525.             p_sys->p_spu = spu_Create( p_stream );
  526.             var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
  527.             var_Set( p_sys->p_spu, "sub-filter", osd_val );
  528.             spu_Init( p_sys->p_spu );
  529.             free( osd_val.psz_string );
  530.         }
  531.         else
  532.         {
  533.             osd_val.psz_string = strdup("osdmenu");
  534.             var_Set( p_sys->p_spu, "sub-filter", osd_val );
  535.             free( osd_val.psz_string );
  536.         }
  537.     }
  538.     /* Audio settings */
  539.     var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
  540.     p_sys->b_master_sync = val.b_bool;
  541.     if( p_sys->f_fps > 0 ) p_sys->b_master_sync = true;
  542.     p_stream->pf_add    = Add;
  543.     p_stream->pf_del    = Del;
  544.     p_stream->pf_send   = Send;
  545.     p_stream->p_sys     = p_sys;
  546.     return VLC_SUCCESS;
  547. }
  548. /*****************************************************************************
  549.  * Close:
  550.  *****************************************************************************/
  551. static void Close( vlc_object_t * p_this )
  552. {
  553.     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
  554.     sout_stream_sys_t   *p_sys = p_stream->p_sys;
  555.     sout_StreamDelete( p_sys->p_out );
  556.     free( p_sys->psz_af2 );
  557.     config_ChainDestroy( p_sys->p_audio_cfg );
  558.     free( p_sys->psz_aenc );
  559.     free( p_sys->psz_vf2 );
  560.     config_ChainDestroy( p_sys->p_video_cfg );
  561.     free( p_sys->psz_venc );
  562.     config_ChainDestroy( p_sys->p_deinterlace_cfg );
  563.     free( p_sys->psz_deinterlace );
  564.     config_ChainDestroy( p_sys->p_spu_cfg );
  565.     free( p_sys->psz_senc );
  566.     if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
  567.     config_ChainDestroy( p_sys->p_osd_cfg );
  568.     free( p_sys->psz_osdenc );
  569.     vlc_object_release( p_sys );
  570. }
  571. struct sout_stream_id_t
  572. {
  573.     bool            b_transcode;
  574.     /* id of the out stream */
  575.     void *id;
  576.     /* Decoder */
  577.     decoder_t       *p_decoder;
  578.     /* Filters */
  579.     filter_chain_t  *p_f_chain;
  580.     /* User specified filters */
  581.     filter_chain_t  *p_uf_chain;
  582.     /* Encoder */
  583.     encoder_t       *p_encoder;
  584.     /* Sync */
  585.     date_t          interpolated_pts;
  586. };
  587. static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
  588. {
  589.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  590.     sout_stream_id_t *id;
  591.     id = calloc( 1, sizeof( sout_stream_id_t ) );
  592.     if( !id )
  593.         goto error;
  594.     id->id = NULL;
  595.     id->p_decoder = NULL;
  596.     id->p_encoder = NULL;
  597.     /* Create decoder object */
  598.     id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
  599.     if( !id->p_decoder )
  600.         goto error;
  601.     vlc_object_attach( id->p_decoder, p_stream );
  602.     id->p_decoder->p_module = NULL;
  603.     id->p_decoder->fmt_in = *p_fmt;
  604.     id->p_decoder->b_pace_control = true;
  605.     /* Create encoder object */
  606.     id->p_encoder = sout_EncoderCreate( p_stream );
  607.     if( !id->p_encoder )
  608.         goto error;
  609.     vlc_object_attach( id->p_encoder, p_stream );
  610.     id->p_encoder->p_module = NULL;
  611.     /* Create destination format */
  612.     es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
  613.     id->p_encoder->fmt_out.i_id    = p_fmt->i_id;
  614.     id->p_encoder->fmt_out.i_group = p_fmt->i_group;
  615.     if( p_fmt->psz_language )
  616.         id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
  617.     if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
  618.     {
  619.         msg_Dbg( p_stream,
  620.                  "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
  621.                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
  622.         /* Complete destination format */
  623.         id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
  624.         id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
  625.             p_sys->i_sample_rate : p_fmt->audio.i_rate;
  626.         id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
  627.         id->p_encoder->fmt_out.audio.i_bitspersample =
  628.             p_fmt->audio.i_bitspersample;
  629.         id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
  630.             p_sys->i_channels : p_fmt->audio.i_channels;
  631.         /* Sanity check for audio channels */
  632.         id->p_encoder->fmt_out.audio.i_channels =
  633.             __MIN( id->p_encoder->fmt_out.audio.i_channels,
  634.                    id->p_decoder->fmt_in.audio.i_channels );
  635.         id->p_encoder->fmt_out.audio.i_original_channels =
  636.             id->p_decoder->fmt_in.audio.i_physical_channels;
  637.         if( id->p_decoder->fmt_in.audio.i_channels ==
  638.             id->p_encoder->fmt_out.audio.i_channels )
  639.         {
  640.             id->p_encoder->fmt_out.audio.i_physical_channels =
  641.                 id->p_decoder->fmt_in.audio.i_physical_channels;
  642.         }
  643.         else
  644.         {
  645.             id->p_encoder->fmt_out.audio.i_physical_channels =
  646.                 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
  647.         }
  648.         /* Build decoder -> filter -> encoder chain */
  649.         if( transcode_audio_new( p_stream, id ) )
  650.         {
  651.             msg_Err( p_stream, "cannot create audio chain" );
  652.             goto error;
  653.         }
  654.         /* Open output stream */
  655.         id->id = sout_StreamIdAdd( p_sys->p_out, &id->p_encoder->fmt_out );
  656.         id->b_transcode = true;
  657.         if( !id->id )
  658.         {
  659.             transcode_audio_close( id );
  660.             goto error;
  661.         }
  662.         date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
  663.     }
  664.     else if( p_fmt->i_cat == VIDEO_ES &&
  665.              (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
  666.     {
  667.         msg_Dbg( p_stream,
  668.                  "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
  669.                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
  670.         /* Complete destination format */
  671.         id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
  672.         id->p_encoder->fmt_out.video.i_width  = p_sys->i_width & ~1;
  673.         id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
  674.         id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
  675.         /* Build decoder -> filter -> encoder chain */
  676.         if( transcode_video_new( p_stream, id ) )
  677.         {
  678.             msg_Err( p_stream, "cannot create video chain" );
  679.             goto error;
  680.         }
  681.         /* Stream will be added later on because we don't know
  682.          * all the characteristics of the decoded stream yet */
  683.         id->b_transcode = true;
  684.         if( p_sys->f_fps > 0 )
  685.         {
  686.             id->p_encoder->fmt_out.video.i_frame_rate =
  687.                 (p_sys->f_fps * 1000) + 0.5;
  688.             id->p_encoder->fmt_out.video.i_frame_rate_base =
  689.                 ENC_FRAMERATE_BASE;
  690.         }
  691.     }
  692.     else if( ( p_fmt->i_cat == SPU_ES ) &&
  693.              ( p_sys->i_scodec || p_sys->psz_senc ) )
  694.     {
  695.         msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
  696.                  "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
  697.                  (char*)&p_sys->i_scodec );
  698.         /* Complete destination format */
  699.         id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
  700.         /* build decoder -> filter -> encoder */
  701.         if( transcode_spu_new( p_stream, id ) )
  702.         {
  703.             msg_Err( p_stream, "cannot create subtitles chain" );
  704.             goto error;
  705.         }
  706.         /* open output stream */
  707.         id->id = sout_StreamIdAdd( p_sys->p_out, &id->p_encoder->fmt_out );
  708.         id->b_transcode = true;
  709.         if( !id->id )
  710.         {
  711.             transcode_spu_close( id );
  712.             goto error;
  713.         }
  714.     }
  715.     else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
  716.     {
  717.         msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
  718.                  (char*)&p_fmt->i_codec );
  719.         id->b_transcode = true;
  720.         /* Build decoder -> filter -> overlaying chain */
  721.         if( transcode_spu_new( p_stream, id ) )
  722.         {
  723.             msg_Err( p_stream, "cannot create subtitles chain" );
  724.             goto error;
  725.         }
  726.     }
  727.     else if( !p_sys->b_osd && (p_sys->i_osdcodec != 0 || p_sys->psz_osdenc) )
  728.     {
  729.         msg_Dbg( p_stream, "creating osd transcoding from fcc=`%4.4s' "
  730.                  "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
  731.                  (char*)&p_sys->i_scodec );
  732.         id->b_transcode = true;
  733.         /* Create a fake OSD menu elementary stream */
  734.         if( transcode_osd_new( p_stream, id ) )
  735.         {
  736.             msg_Err( p_stream, "cannot create osd chain" );
  737.             goto error;
  738.         }
  739.         p_sys->b_osd = true;
  740.     }
  741.     else
  742.     {
  743.         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
  744.                  (char*)&p_fmt->i_codec );
  745.         id->id = sout_StreamIdAdd( p_sys->p_out, p_fmt );
  746.         id->b_transcode = false;
  747.         if( !id->id ) goto error;
  748.     }
  749.     return id;
  750. error:
  751.     if( id )
  752.     {
  753.         if( id->p_decoder )
  754.         {
  755.             vlc_object_detach( id->p_decoder );
  756.             vlc_object_release( id->p_decoder );
  757.             id->p_decoder = NULL;
  758.         }
  759.         if( id->p_encoder )
  760.         {
  761.             vlc_object_detach( id->p_encoder );
  762.             es_format_Clean( &id->p_encoder->fmt_out );
  763.             vlc_object_release( id->p_encoder );
  764.             id->p_encoder = NULL;
  765.         }
  766.         free( id );
  767.     }
  768.     return NULL;
  769. }
  770. static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
  771. {
  772.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  773.     if( id->b_transcode )
  774.     {
  775.         switch( id->p_decoder->fmt_in.i_cat )
  776.         {
  777.         case AUDIO_ES:
  778.             transcode_audio_close( id );
  779.             break;
  780.         case VIDEO_ES:
  781.             transcode_video_close( p_stream, id );
  782.             break;
  783.         case SPU_ES:
  784.             if( p_sys->b_osd )
  785.                 transcode_osd_close( p_stream, id );
  786.             else
  787.                 transcode_spu_close( id );
  788.             break;
  789.         }
  790.     }
  791.     if( id->id ) sout_StreamIdDel( p_sys->p_out, id->id );
  792.     if( id->p_decoder )
  793.     {
  794.         vlc_object_detach( id->p_decoder );
  795.         vlc_object_release( id->p_decoder );
  796.         id->p_decoder = NULL;
  797.     }
  798.     if( id->p_encoder )
  799.     {
  800.         vlc_object_detach( id->p_encoder );
  801.         es_format_Clean( &id->p_encoder->fmt_out );
  802.         vlc_object_release( id->p_encoder );
  803.         id->p_encoder = NULL;
  804.     }
  805.     free( id );
  806.     return VLC_SUCCESS;
  807. }
  808. static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
  809.                  block_t *p_buffer )
  810. {
  811.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  812.     block_t *p_out = NULL;
  813.     if( !id->b_transcode )
  814.     {
  815.         if( id->id )
  816.             return sout_StreamIdSend( p_sys->p_out, id->id, p_buffer );
  817.         block_Release( p_buffer );
  818.         return VLC_EGENERIC;
  819.     }
  820.     switch( id->p_decoder->fmt_in.i_cat )
  821.     {
  822.     case AUDIO_ES:
  823.         transcode_audio_process( p_stream, id, p_buffer, &p_out );
  824.         break;
  825.     case VIDEO_ES:
  826.         if( transcode_video_process( p_stream, id, p_buffer, &p_out )
  827.             != VLC_SUCCESS )
  828.         {
  829.             return VLC_EGENERIC;
  830.         }
  831.         break;
  832.     case SPU_ES:
  833.         /* Transcode OSD menu pictures. */
  834.         if( p_sys->b_osd )
  835.         {
  836.             if( transcode_osd_process( p_stream, id, p_buffer, &p_out ) !=
  837.                 VLC_SUCCESS )
  838.             {
  839.                 return VLC_EGENERIC;
  840.             }
  841.         }
  842.         else if ( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
  843.             VLC_SUCCESS )
  844.         {
  845.             return VLC_EGENERIC;
  846.         }
  847.         break;
  848.     default:
  849.         p_out = NULL;
  850.         block_Release( p_buffer );
  851.         break;
  852.     }
  853.     if( p_out )
  854.         return sout_StreamIdSend( p_sys->p_out, id->id, p_out );
  855.     return VLC_SUCCESS;
  856. }
  857. /****************************************************************************
  858.  * decoder helper
  859.  ****************************************************************************/
  860. static inline void video_timer_start( encoder_t * p_encoder )
  861. {
  862.     stats_TimerStart( p_encoder, "encoding video frame",
  863.                       STATS_TIMER_VIDEO_FRAME_ENCODING );
  864. }
  865. static inline void video_timer_stop( encoder_t * p_encoder )
  866. {
  867.     stats_TimerStop( p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
  868. }
  869. static inline void video_timer_close( encoder_t * p_encoder )
  870. {
  871.     stats_TimerDump(  p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
  872.     stats_TimerClean( p_encoder, STATS_TIMER_VIDEO_FRAME_ENCODING );
  873. }
  874. static inline void audio_timer_start( encoder_t * p_encoder )
  875. {
  876.     stats_TimerStart( p_encoder, "encoding audio frame",
  877.                       STATS_TIMER_AUDIO_FRAME_ENCODING );
  878. }
  879. static inline void audio_timer_stop( encoder_t * p_encoder )
  880. {
  881.     stats_TimerStop( p_encoder, STATS_TIMER_AUDIO_FRAME_ENCODING );
  882. }
  883. static inline void audio_timer_close( encoder_t * p_encoder )
  884. {
  885.     stats_TimerDump(  p_encoder, STATS_TIMER_AUDIO_FRAME_ENCODING );
  886.     stats_TimerClean( p_encoder, STATS_TIMER_AUDIO_FRAME_ENCODING );
  887. }
  888. /****************************************************************************
  889.  * decoder reencoder part
  890.  ****************************************************************************/
  891. static block_t *transcode_audio_alloc( filter_t *p_filter, int size )
  892. {
  893.     VLC_UNUSED( p_filter );
  894.     return block_Alloc( size );
  895. }
  896. static int transcode_audio_filter_allocation_init( filter_t *p_filter,
  897.                                                    void *data )
  898. {
  899.     VLC_UNUSED(data);
  900.     p_filter->pf_audio_buffer_new = transcode_audio_alloc;
  901.     return VLC_SUCCESS;
  902. }
  903. static bool transcode_audio_filter_needed( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
  904. {
  905.     if( p_fmt1->i_codec != p_fmt2->i_codec ||
  906.         p_fmt1->audio.i_channels != p_fmt2->audio.i_channels ||
  907.         p_fmt1->audio.i_rate != p_fmt2->audio.i_rate )
  908.         return true;
  909.     return false;
  910. }
  911. static int transcode_audio_filter_chain_build( sout_stream_t *p_stream, filter_chain_t *p_chain,
  912.                                                const es_format_t *p_dst, const es_format_t *p_src )
  913. {
  914.     if( !transcode_audio_filter_needed( p_dst, p_src ) )
  915.         return VLC_SUCCESS;
  916.     es_format_t current = *p_src;
  917.     msg_Dbg( p_stream, "Looking for filter "
  918.              "(%4.4s->%4.4s, channels %d->%d, rate %d->%d)",
  919.          (const char *)&p_src->i_codec,
  920.          (const char *)&p_dst->i_codec,
  921.          p_src->audio.i_channels,
  922.          p_dst->audio.i_channels,
  923.          p_src->audio.i_rate,
  924.          p_dst->audio.i_rate );
  925.     /* If any filter is needed, convert to fl32 */
  926.     if( current.i_codec != VLC_FOURCC('f','l','3','2') )
  927.     {
  928.         /* First step, convert to fl32 */
  929.         current.i_codec =
  930.         current.audio.i_format = VLC_FOURCC('f','l','3','2');
  931.         if( !filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, &current ) )
  932.         {
  933.             msg_Err( p_stream, "Failed to find conversion filter to fl32" );
  934.             return VLC_EGENERIC;
  935.         }
  936.         current = *filter_chain_GetFmtOut( p_chain );
  937.     }
  938.     /* Fix sample rate */
  939.     if( current.audio.i_rate != p_dst->audio.i_rate )
  940.     {
  941.         current.audio.i_rate = p_dst->audio.i_rate;
  942.         if( !filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, &current ) )
  943.         {
  944.             msg_Err( p_stream, "Failed to find conversion filter for resampling" );
  945.             return VLC_EGENERIC;
  946.         }
  947.         current = *filter_chain_GetFmtOut( p_chain );
  948.     }
  949.     /* Fix channels */
  950.     if( current.audio.i_channels != p_dst->audio.i_channels )
  951.     {
  952.         current.audio.i_channels = p_dst->audio.i_channels;
  953.         current.audio.i_physical_channels = p_dst->audio.i_physical_channels;
  954.         current.audio.i_original_channels = p_dst->audio.i_original_channels;
  955.         if( ( !current.audio.i_physical_channels || !current.audio.i_original_channels ) &&
  956.             current.audio.i_channels < 6 )
  957.             current.audio.i_physical_channels =
  958.             current.audio.i_original_channels = pi_channels_maps[current.audio.i_channels];
  959.         if( !filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, &current ) )
  960.         {
  961.             msg_Err( p_stream, "Failed to find conversion filter for channel mixing" );
  962.             return VLC_EGENERIC;
  963.         }
  964.         current = *filter_chain_GetFmtOut( p_chain );
  965.     }
  966.     /* And last step, convert to the requested codec */
  967.     if( current.i_codec != p_dst->i_codec )
  968.     {
  969.         current.i_codec = p_dst->i_codec;
  970.         if( !filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, &current ) )
  971.         {
  972.             msg_Err( p_stream, "Failed to find conversion filter to %4.4s",
  973.                      (const char*)&p_dst->i_codec);
  974.             return VLC_EGENERIC;
  975.         }
  976.         current = *filter_chain_GetFmtOut( p_chain );
  977.     }
  978.     if( transcode_audio_filter_needed( p_dst, &current ) )
  979.     {
  980.         /* Weird case, a filter has side effects, doomed */
  981.         msg_Err( p_stream, "Failed to create a valid audio filter chain" );
  982.         return VLC_EGENERIC;
  983.     }
  984.     msg_Dbg( p_stream, "Got complete audio filter chain" );
  985.     return VLC_SUCCESS;
  986. }
  987. static int transcode_audio_new( sout_stream_t *p_stream,
  988.                                 sout_stream_id_t *id )
  989. {
  990.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  991.     es_format_t fmt_last;
  992.     /*
  993.      * Open decoder
  994.      */
  995.     /* Initialization of decoder structures */
  996.     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
  997.     id->p_decoder->fmt_out.i_extra = 0;
  998.     id->p_decoder->fmt_out.p_extra = 0;
  999.     id->p_decoder->pf_decode_audio = NULL;
  1000.     id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
  1001.     id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
  1002.     /* id->p_decoder->p_cfg = p_sys->p_audio_cfg; */
  1003.     id->p_decoder->p_module =
  1004.         module_need( id->p_decoder, "decoder", "$codec", false );
  1005.     if( !id->p_decoder->p_module )
  1006.     {
  1007.         msg_Err( p_stream, "cannot find audio decoder" );
  1008.         return VLC_EGENERIC;
  1009.     }
  1010.     id->p_decoder->fmt_out.audio.i_bitspersample =
  1011.         aout_BitsPerSample( id->p_decoder->fmt_out.i_codec );
  1012.     fmt_last = id->p_decoder->fmt_out;
  1013.     /* Fix AAC SBR changing number of channels and sampling rate */
  1014.     if( !(id->p_decoder->fmt_in.i_codec == VLC_FOURCC('m','p','4','a') &&
  1015.         fmt_last.audio.i_rate != id->p_encoder->fmt_in.audio.i_rate &&
  1016.         fmt_last.audio.i_channels != id->p_encoder->fmt_in.audio.i_channels) )
  1017.         fmt_last.audio.i_rate = id->p_decoder->fmt_in.audio.i_rate;
  1018.     /*
  1019.      * Open encoder
  1020.      */
  1021.     /* Initialization of encoder format structures */
  1022.     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
  1023.                     id->p_decoder->fmt_out.i_codec );
  1024.     id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
  1025.     id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
  1026.     id->p_encoder->fmt_in.audio.i_physical_channels =
  1027.         id->p_encoder->fmt_out.audio.i_physical_channels;
  1028.     id->p_encoder->fmt_in.audio.i_original_channels =
  1029.         id->p_encoder->fmt_out.audio.i_original_channels;
  1030.     id->p_encoder->fmt_in.audio.i_channels =
  1031.         id->p_encoder->fmt_out.audio.i_channels;
  1032.     id->p_encoder->fmt_in.audio.i_bitspersample =
  1033.         aout_BitsPerSample( id->p_encoder->fmt_in.i_codec );
  1034.     id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
  1035.     id->p_encoder->p_module =
  1036.         module_need( id->p_encoder, "encoder", p_sys->psz_aenc, true );
  1037.     if( !id->p_encoder->p_module )
  1038.     {
  1039.         msg_Err( p_stream, "cannot find audio encoder (module:%s fourcc:%4.4s)",
  1040.                  p_sys->psz_aenc ? p_sys->psz_aenc : "any",
  1041.                  (char *)&p_sys->i_acodec );
  1042.         module_unneed( id->p_decoder, id->p_decoder->p_module );
  1043.         id->p_decoder->p_module = NULL;
  1044.         return VLC_EGENERIC;
  1045.     }
  1046.     id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
  1047.     id->p_encoder->fmt_in.audio.i_bitspersample =
  1048.         aout_BitsPerSample( id->p_encoder->fmt_in.i_codec );
  1049.     /* Load user specified audio filters */
  1050.     if( p_sys->psz_af2 )
  1051.     {
  1052.         es_format_t fmt_fl32 = fmt_last;
  1053.         fmt_fl32.i_codec =
  1054.         fmt_fl32.audio.i_format = VLC_FOURCC('f','l','3','2');
  1055.         if( transcode_audio_filter_chain_build( p_stream, id->p_uf_chain,
  1056.                                                 &fmt_fl32, &fmt_last ) )
  1057.         {
  1058.             transcode_audio_close( id );
  1059.             return VLC_EGENERIC;
  1060.         }
  1061.         fmt_last = fmt_fl32;
  1062.         id->p_uf_chain = filter_chain_New( p_stream, "audio filter2", false,
  1063.                                            transcode_audio_filter_allocation_init, NULL, NULL );
  1064.         filter_chain_Reset( id->p_uf_chain, &fmt_last, &fmt_fl32 );
  1065.         if( filter_chain_AppendFromString( id->p_uf_chain, p_sys->psz_af2 ) > 0 )
  1066.             fmt_last = *filter_chain_GetFmtOut( id->p_uf_chain );
  1067.     }
  1068.     /* Load conversion filters */
  1069.     id->p_f_chain = filter_chain_New( p_stream, "audio filter2", true,
  1070.                     transcode_audio_filter_allocation_init, NULL, NULL );
  1071.     filter_chain_Reset( id->p_f_chain, &fmt_last, &id->p_encoder->fmt_in );
  1072.     if( transcode_audio_filter_chain_build( p_stream, id->p_f_chain,
  1073.                                             &id->p_encoder->fmt_in, &fmt_last ) )
  1074.     {
  1075.         transcode_audio_close( id );
  1076.         return VLC_EGENERIC;
  1077.     }
  1078.     fmt_last = id->p_encoder->fmt_in;
  1079.     /* FIXME: Hack for mp3 transcoding support */
  1080.     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
  1081.         id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
  1082.     return VLC_SUCCESS;
  1083. }
  1084. static void transcode_audio_close( sout_stream_id_t *id )
  1085. {
  1086.     audio_timer_close( id->p_encoder );
  1087.     /* Close decoder */
  1088.     if( id->p_decoder->p_module )
  1089.         module_unneed( id->p_decoder, id->p_decoder->p_module );
  1090.     id->p_decoder->p_module = NULL;
  1091.     if( id->p_decoder->p_description )
  1092.         vlc_meta_Delete( id->p_decoder->p_description );
  1093.     id->p_decoder->p_description = NULL;
  1094.     /* Close encoder */
  1095.     if( id->p_encoder->p_module )
  1096.         module_unneed( id->p_encoder, id->p_encoder->p_module );
  1097.     id->p_encoder->p_module = NULL;
  1098.     /* Close filters */
  1099.     if( id->p_uf_chain )
  1100.         filter_chain_Delete( id->p_uf_chain );
  1101.     if( id->p_f_chain )
  1102.         filter_chain_Delete( id->p_f_chain );
  1103. }
  1104. static int transcode_audio_process( sout_stream_t *p_stream,
  1105.                                     sout_stream_id_t *id,
  1106.                                     block_t *in, block_t **out )
  1107. {
  1108.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1109.     aout_buffer_t *p_audio_buf;
  1110.     block_t *p_block, *p_audio_block;
  1111.     *out = NULL;
  1112.     while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
  1113.                                                           &in )) )
  1114.     {
  1115.         sout_UpdateStatistic( p_stream->p_sout, SOUT_STATISTIC_DECODED_AUDIO, 1 );
  1116.         if( p_sys->b_master_sync )
  1117.         {
  1118.             mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
  1119.             if ( p_audio_buf->start_date - i_dts > MASTER_SYNC_MAX_DRIFT
  1120.                   || p_audio_buf->start_date - i_dts < -MASTER_SYNC_MAX_DRIFT )
  1121.             {
  1122.                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
  1123.                 date_Set( &id->interpolated_pts, p_audio_buf->start_date );
  1124.                 i_dts = p_audio_buf->start_date + 1;
  1125.             }
  1126.             p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
  1127.             date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
  1128.             p_audio_buf->start_date -= p_sys->i_master_drift;
  1129.             p_audio_buf->end_date -= p_sys->i_master_drift;
  1130.         }
  1131.         p_audio_block = p_audio_buf->p_sys;
  1132.         p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
  1133.         p_audio_block->i_dts = p_audio_block->i_pts =
  1134.             p_audio_buf->start_date;
  1135.         p_audio_block->i_length = p_audio_buf->end_date -
  1136.             p_audio_buf->start_date;
  1137.         p_audio_block->i_samples = p_audio_buf->i_nb_samples;
  1138.         /* Run filter chain */
  1139.         if( id->p_uf_chain )
  1140.         {
  1141.             p_audio_block = filter_chain_AudioFilter( id->p_uf_chain, p_audio_block );
  1142.             assert( p_audio_block );
  1143.         }
  1144.         p_audio_block = filter_chain_AudioFilter( id->p_f_chain, p_audio_block );
  1145.         assert( p_audio_block );
  1146.         p_audio_buf->p_buffer = p_audio_block->p_buffer;
  1147.         p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
  1148.         p_audio_buf->i_nb_samples = p_audio_block->i_samples;
  1149.         p_audio_buf->start_date = p_audio_block->i_dts;
  1150.         p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
  1151.         audio_timer_start( id->p_encoder );
  1152.         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
  1153.         audio_timer_stop( id->p_encoder );
  1154.         block_ChainAppend( out, p_block );
  1155.         block_Release( p_audio_block );
  1156.         free( p_audio_buf );
  1157.     }
  1158.     return VLC_SUCCESS;
  1159. }
  1160. static void audio_release_buffer( aout_buffer_t *p_buffer )
  1161. {
  1162.     if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
  1163.     free( p_buffer );
  1164. }
  1165. static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
  1166. {
  1167.     aout_buffer_t *p_buffer;
  1168.     block_t *p_block;
  1169.     int i_size;
  1170.     if( p_dec->fmt_out.audio.i_bitspersample )
  1171.     {
  1172.         i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
  1173.             p_dec->fmt_out.audio.i_channels;
  1174.     }
  1175.     else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
  1176.              p_dec->fmt_out.audio.i_frame_length )
  1177.     {
  1178.         i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
  1179.             p_dec->fmt_out.audio.i_frame_length;
  1180.     }
  1181.     else
  1182.     {
  1183.         i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
  1184.     }
  1185.     p_buffer = malloc( sizeof(aout_buffer_t) );
  1186.     if( !p_buffer ) return NULL;
  1187.     p_buffer->b_discontinuity = false;
  1188.     p_buffer->pf_release = audio_release_buffer;
  1189.     p_buffer->p_sys = p_block = block_New( p_dec, i_size );
  1190.     p_buffer->p_buffer = p_block->p_buffer;
  1191.     p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
  1192.     p_buffer->i_nb_samples = i_samples;
  1193.     p_block->i_samples = i_samples;
  1194.     return p_buffer;
  1195. }
  1196. static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
  1197. {
  1198.     VLC_UNUSED(p_dec);
  1199.     if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
  1200.     free( p_buffer );
  1201. }
  1202. /*
  1203.  * video
  1204.  */
  1205. static int transcode_video_filter_allocation_init( filter_t *p_filter,
  1206.                                                    void *p_data )
  1207. {
  1208.     sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_data;
  1209.     int i;
  1210.     p_filter->pf_vout_buffer_new = video_new_buffer_filter;
  1211.     p_filter->pf_vout_buffer_del = video_del_buffer_filter;
  1212.     p_filter->p_owner = malloc( sizeof(filter_owner_sys_t) );
  1213.     if( !p_filter->p_owner )
  1214.         return VLC_EGENERIC;
  1215.     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1216.         p_filter->p_owner->pp_pics[i] = 0;
  1217.     p_filter->p_owner->p_sys = p_sys;
  1218.     return VLC_SUCCESS;
  1219. }
  1220. static void transcode_video_filter_allocation_clear( filter_t *p_filter )
  1221. {
  1222.     int j;
  1223.     /* Clean-up pictures ring buffer */
  1224.     for( j = 0; j < PICTURE_RING_SIZE; j++ )
  1225.     {
  1226.         if( p_filter->p_owner->pp_pics[j] )
  1227.             video_del_buffer( VLC_OBJECT(p_filter),
  1228.                               p_filter->p_owner->pp_pics[j] );
  1229.     }
  1230.     free( p_filter->p_owner );
  1231. }
  1232. static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
  1233. {
  1234.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1235.     int i;
  1236.     /* Open decoder
  1237.      * Initialization of decoder structures
  1238.      */
  1239.     id->p_decoder->fmt_out = id->p_decoder->fmt_in;
  1240.     id->p_decoder->fmt_out.i_extra = 0;
  1241.     id->p_decoder->fmt_out.p_extra = 0;
  1242.     id->p_decoder->pf_decode_video = NULL;
  1243.     id->p_decoder->pf_get_cc = NULL;
  1244.     id->p_decoder->pf_get_cc = 0;
  1245.     id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
  1246.     id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
  1247.     id->p_decoder->pf_picture_link    = video_link_picture_decoder;
  1248.     id->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
  1249.     id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
  1250.     if( !id->p_decoder->p_owner )
  1251.         return VLC_EGENERIC;
  1252.     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1253.         id->p_decoder->p_owner->pp_pics[i] = 0;
  1254.     id->p_decoder->p_owner->p_sys = p_sys;
  1255.     /* id->p_decoder->p_cfg = p_sys->p_video_cfg; */
  1256.     id->p_decoder->p_module =
  1257.         module_need( id->p_decoder, "decoder", "$codec", false );
  1258.     if( !id->p_decoder->p_module )
  1259.     {
  1260.         msg_Err( p_stream, "cannot find video decoder" );
  1261.         free( id->p_decoder->p_owner );
  1262.         return VLC_EGENERIC;
  1263.     }
  1264.     /*
  1265.      * Open encoder.
  1266.      * Because some info about the decoded input will only be available
  1267.      * once the first frame is decoded, we actually only test the availability
  1268.      * of the encoder here.
  1269.      */
  1270.     /* Initialization of encoder format structures */
  1271.     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
  1272.                     id->p_decoder->fmt_out.i_codec );
  1273.     id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
  1274.     /* The dimensions will be set properly later on.
  1275.      * Just put sensible values so we can test an encoder is available. */
  1276.     id->p_encoder->fmt_in.video.i_width =
  1277.         id->p_encoder->fmt_out.video.i_width
  1278.           ? id->p_encoder->fmt_out.video.i_width
  1279.           : id->p_decoder->fmt_in.video.i_width
  1280.             ? id->p_decoder->fmt_in.video.i_width : 16;
  1281.     id->p_encoder->fmt_in.video.i_height =
  1282.         id->p_encoder->fmt_out.video.i_height
  1283.           ? id->p_encoder->fmt_out.video.i_height
  1284.           : id->p_decoder->fmt_in.video.i_height
  1285.             ? id->p_decoder->fmt_in.video.i_height : 16;
  1286.     id->p_encoder->fmt_in.video.i_frame_rate = ENC_FRAMERATE;
  1287.     id->p_encoder->fmt_in.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
  1288.     id->p_encoder->i_threads = p_sys->i_threads;
  1289.     id->p_encoder->p_cfg = p_sys->p_video_cfg;
  1290.     id->p_encoder->p_module =
  1291.         module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
  1292.     if( !id->p_encoder->p_module )
  1293.     {
  1294.         msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
  1295.                  p_sys->psz_venc ? p_sys->psz_venc : "any",
  1296.                  (char *)&p_sys->i_vcodec );
  1297.         module_unneed( id->p_decoder, id->p_decoder->p_module );
  1298.         id->p_decoder->p_module = 0;
  1299.         free( id->p_decoder->p_owner );
  1300.         return VLC_EGENERIC;
  1301.     }
  1302.     /* Close the encoder.
  1303.      * We'll open it only when we have the first frame. */
  1304.     module_unneed( id->p_encoder, id->p_encoder->p_module );
  1305.     if( id->p_encoder->fmt_out.p_extra )
  1306.     {
  1307.         free( id->p_encoder->fmt_out.p_extra );
  1308.         id->p_encoder->fmt_out.p_extra = NULL;
  1309.         id->p_encoder->fmt_out.i_extra = 0;
  1310.     }
  1311.     id->p_encoder->p_module = NULL;
  1312.     if( p_sys->i_threads >= 1 )
  1313.     {
  1314.         int i_priority = p_sys->b_high_priority ? VLC_THREAD_PRIORITY_OUTPUT :
  1315.                            VLC_THREAD_PRIORITY_VIDEO;
  1316.         p_sys->id_video = id;
  1317.         vlc_mutex_init( &p_sys->lock_out );
  1318.         vlc_cond_init( &p_sys->cond );
  1319.         memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
  1320.         p_sys->i_first_pic = 0;
  1321.         p_sys->i_last_pic = 0;
  1322.         p_sys->p_buffers = NULL;
  1323.         p_sys->b_die = p_sys->b_error = 0;
  1324.         if( vlc_thread_create( p_sys, "encoder", EncoderThread, i_priority ) )
  1325.         {
  1326.             msg_Err( p_stream, "cannot spawn encoder thread" );
  1327.             module_unneed( id->p_decoder, id->p_decoder->p_module );
  1328.             id->p_decoder->p_module = 0;
  1329.             free( id->p_decoder->p_owner );
  1330.             return VLC_EGENERIC;
  1331.         }
  1332.     }
  1333.     return VLC_SUCCESS;
  1334. }
  1335. static void transcode_video_encoder_init( sout_stream_t *p_stream,
  1336.                                           sout_stream_id_t *id )
  1337. {
  1338.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1339.     /* Calculate scaling
  1340.      * width/height of source */
  1341.     int i_src_width = id->p_decoder->fmt_out.video.i_width;
  1342.     int i_src_height = id->p_decoder->fmt_out.video.i_height;
  1343.     /* with/height scaling */
  1344.     float f_scale_width = 1;
  1345.     float f_scale_height = 1;
  1346.     /* width/height of output stream */
  1347.     int i_dst_width;
  1348.     int i_dst_height;
  1349.     /* aspect ratio */
  1350.     float f_aspect = (float)id->p_decoder->fmt_out.video.i_aspect /
  1351.                             VOUT_ASPECT_FACTOR;
  1352.     msg_Dbg( p_stream, "decoder aspect is %i:%i",
  1353.                  id->p_decoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
  1354.     /* Change f_aspect from source frame to source pixel */
  1355.     f_aspect = f_aspect * i_src_height / i_src_width;
  1356.     msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
  1357.     /* Calculate scaling factor for specified parameters */
  1358.     if( id->p_encoder->fmt_out.video.i_width <= 0 &&
  1359.         id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
  1360.     {
  1361.         /* Global scaling. Make sure width will remain a factor of 16 */
  1362.         float f_real_scale;
  1363.         int  i_new_height;
  1364.         int i_new_width = i_src_width * p_sys->f_scale;
  1365.         if( i_new_width % 16 <= 7 && i_new_width >= 16 )
  1366.             i_new_width -= i_new_width % 16;
  1367.         else
  1368.             i_new_width += 16 - i_new_width % 16;
  1369.         f_real_scale = (float)( i_new_width ) / (float) i_src_width;
  1370.         i_new_height = __MAX( 16, i_src_height * (float)f_real_scale );
  1371.         f_scale_width = f_real_scale;
  1372.         f_scale_height = (float) i_new_height / (float) i_src_height;
  1373.     }
  1374.     else if( id->p_encoder->fmt_out.video.i_width > 0 &&
  1375.              id->p_encoder->fmt_out.video.i_height <= 0 )
  1376.     {
  1377.         /* Only width specified */
  1378.         f_scale_width = (float)id->p_encoder->fmt_out.video.i_width/i_src_width;
  1379.         f_scale_height = f_scale_width;
  1380.     }
  1381.     else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
  1382.              id->p_encoder->fmt_out.video.i_height > 0 )
  1383.     {
  1384.          /* Only height specified */
  1385.          f_scale_height = (float)id->p_encoder->fmt_out.video.i_height/i_src_height;
  1386.          f_scale_width = f_scale_height;
  1387.      }
  1388.      else if( id->p_encoder->fmt_out.video.i_width > 0 &&
  1389.               id->p_encoder->fmt_out.video.i_height > 0 )
  1390.      {
  1391.          /* Width and height specified */
  1392.          f_scale_width = (float)id->p_encoder->fmt_out.video.i_width/i_src_width;
  1393.          f_scale_height = (float)id->p_encoder->fmt_out.video.i_height/i_src_height;
  1394.      }
  1395.      /* check maxwidth and maxheight
  1396.       */
  1397.      if( p_sys->i_maxwidth && f_scale_width > (float)p_sys->i_maxwidth /
  1398.                                                      i_src_width )
  1399.      {
  1400.          f_scale_width = (float)p_sys->i_maxwidth / i_src_width;
  1401.      }
  1402.      if( p_sys->i_maxheight && f_scale_height > (float)p_sys->i_maxheight /
  1403.                                                        i_src_height )
  1404.      {
  1405.          f_scale_height = (float)p_sys->i_maxheight / i_src_height;
  1406.      }
  1407.      /* Change aspect ratio from source pixel to scaled pixel */
  1408.      f_aspect = f_aspect * f_scale_height / f_scale_width;
  1409.      msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
  1410.      /* f_scale_width and f_scale_height are now final */
  1411.      /* Calculate width, height from scaling
  1412.       * Make sure its multiple of 2
  1413.       */
  1414.      i_dst_width =  2 * (int)(f_scale_width*i_src_width/2+0.5);
  1415.      i_dst_height = 2 * (int)(f_scale_height*i_src_height/2+0.5);
  1416.      /* Change aspect ratio from scaled pixel to output frame */
  1417.      f_aspect = f_aspect * i_dst_width / i_dst_height;
  1418.      /* Store calculated values */
  1419.      id->p_encoder->fmt_out.video.i_width =
  1420.      id->p_encoder->fmt_out.video.i_visible_width = i_dst_width;
  1421.      id->p_encoder->fmt_out.video.i_height =
  1422.      id->p_encoder->fmt_out.video.i_visible_height = i_dst_height;
  1423.      id->p_encoder->fmt_in.video.i_width =
  1424.      id->p_encoder->fmt_in.video.i_visible_width = i_dst_width;
  1425.      id->p_encoder->fmt_in.video.i_height =
  1426.      id->p_encoder->fmt_in.video.i_visible_height = i_dst_height;
  1427.      msg_Dbg( p_stream, "source %ix%i, destination %ix%i",
  1428.          i_src_width, i_src_height,
  1429.          i_dst_width, i_dst_height
  1430.      );
  1431.     /* Handle frame rate conversion */
  1432.     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
  1433.         !id->p_encoder->fmt_out.video.i_frame_rate_base )
  1434.     {
  1435.         if( id->p_decoder->fmt_out.video.i_frame_rate &&
  1436.             id->p_decoder->fmt_out.video.i_frame_rate_base )
  1437.         {
  1438.             id->p_encoder->fmt_out.video.i_frame_rate =
  1439.                 id->p_decoder->fmt_out.video.i_frame_rate;
  1440.             id->p_encoder->fmt_out.video.i_frame_rate_base =
  1441.                 id->p_decoder->fmt_out.video.i_frame_rate_base;
  1442.         }
  1443.         else
  1444.         {
  1445.             /* Pick a sensible default value */
  1446.             id->p_encoder->fmt_out.video.i_frame_rate = ENC_FRAMERATE;
  1447.             id->p_encoder->fmt_out.video.i_frame_rate_base = ENC_FRAMERATE_BASE;
  1448.         }
  1449.     }
  1450.     id->p_encoder->fmt_in.video.i_frame_rate =
  1451.         id->p_encoder->fmt_out.video.i_frame_rate;
  1452.     id->p_encoder->fmt_in.video.i_frame_rate_base =
  1453.         id->p_encoder->fmt_out.video.i_frame_rate_base;
  1454.     date_Init( &id->interpolated_pts,
  1455.                id->p_encoder->fmt_out.video.i_frame_rate,
  1456.                id->p_encoder->fmt_out.video.i_frame_rate_base );
  1457.     /* Check whether a particular aspect ratio was requested */
  1458.     if( !id->p_encoder->fmt_out.video.i_aspect )
  1459.     {
  1460.         id->p_encoder->fmt_out.video.i_aspect =
  1461.                 (int)( f_aspect * VOUT_ASPECT_FACTOR + 0.5 );
  1462.     }
  1463.     id->p_encoder->fmt_in.video.i_aspect =
  1464.         id->p_encoder->fmt_out.video.i_aspect;
  1465.     msg_Dbg( p_stream, "encoder aspect is %i:%i",
  1466.              id->p_encoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
  1467.     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
  1468. }
  1469. static int transcode_video_encoder_open( sout_stream_t *p_stream,
  1470.                                          sout_stream_id_t *id )
  1471. {
  1472.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1473.     msg_Dbg( p_stream, "destination (after video filters) %ix%i",
  1474.              id->p_encoder->fmt_in.video.i_width,
  1475.              id->p_encoder->fmt_in.video.i_height );
  1476.     id->p_encoder->p_module =
  1477.         module_need( id->p_encoder, "encoder", p_sys->psz_venc, true );
  1478.     if( !id->p_encoder->p_module )
  1479.     {
  1480.         msg_Err( p_stream, "cannot find video encoder (module:%s fourcc:%4.4s)",
  1481.                  p_sys->psz_venc ? p_sys->psz_venc : "any",
  1482.                  (char *)&p_sys->i_vcodec );
  1483.         return VLC_EGENERIC;
  1484.     }
  1485.     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
  1486.     /* Hack for mp2v/mp1v transcoding support */
  1487.     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
  1488.         id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
  1489.     {
  1490.         id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
  1491.     }
  1492.     id->id = sout_StreamIdAdd( p_stream->p_sys->p_out,
  1493.                                &id->p_encoder->fmt_out );
  1494.     if( !id->id )
  1495.     {
  1496.         msg_Err( p_stream, "cannot add this stream" );
  1497.         return VLC_EGENERIC;
  1498.     }
  1499.     return VLC_SUCCESS;
  1500. }
  1501. static void transcode_video_close( sout_stream_t *p_stream,
  1502.                                    sout_stream_id_t *id )
  1503. {
  1504.     int i;
  1505.     if( p_stream->p_sys->i_threads >= 1 )
  1506.     {
  1507.         vlc_mutex_lock( &p_stream->p_sys->lock_out );
  1508.         vlc_object_kill( p_stream->p_sys );
  1509.         vlc_cond_signal( &p_stream->p_sys->cond );
  1510.         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
  1511.         vlc_thread_join( p_stream->p_sys );
  1512.         vlc_mutex_destroy( &p_stream->p_sys->lock_out );
  1513.         vlc_cond_destroy( &p_stream->p_sys->cond );
  1514.     }
  1515.     video_timer_close( id->p_encoder );
  1516.     /* Close decoder */
  1517.     if( id->p_decoder->p_module )
  1518.         module_unneed( id->p_decoder, id->p_decoder->p_module );
  1519.     if( id->p_decoder->p_description )
  1520.         vlc_meta_Delete( id->p_decoder->p_description );
  1521.     if( id->p_decoder->p_owner )
  1522.     {
  1523.         /* Clean-up pictures ring buffer */
  1524.         for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1525.         {
  1526.             if( id->p_decoder->p_owner->pp_pics[i] )
  1527.                 video_del_buffer( VLC_OBJECT(id->p_decoder),
  1528.                                   id->p_decoder->p_owner->pp_pics[i] );
  1529.         }
  1530.         free( id->p_decoder->p_owner );
  1531.     }
  1532.     /* Close encoder */
  1533.     if( id->p_encoder->p_module )
  1534.         module_unneed( id->p_encoder, id->p_encoder->p_module );
  1535.     /* Close filters */
  1536.     if( id->p_f_chain )
  1537.         filter_chain_Delete( id->p_f_chain );
  1538.     if( id->p_uf_chain )
  1539.         filter_chain_Delete( id->p_uf_chain );
  1540. }
  1541. static int transcode_video_process( sout_stream_t *p_stream,
  1542.                                     sout_stream_id_t *id,
  1543.                                     block_t *in, block_t **out )
  1544. {
  1545.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1546.     int i_duplicate = 1;
  1547.     picture_t *p_pic, *p_pic2 = NULL;
  1548.     *out = NULL;
  1549.     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
  1550.     {
  1551.         subpicture_t *p_subpic = NULL;
  1552.         sout_UpdateStatistic( p_stream->p_sout, SOUT_STATISTIC_DECODED_VIDEO, 1 );
  1553.         if( p_stream->p_sout->i_out_pace_nocontrol && p_sys->b_hurry_up )
  1554.         {
  1555.             mtime_t current_date = mdate();
  1556.             if( current_date + 50000 > p_pic->date )
  1557.             {
  1558.                 msg_Dbg( p_stream, "late picture skipped (%"PRId64")",
  1559.                          current_date + 50000 - p_pic->date );
  1560.                 p_pic->pf_release( p_pic );
  1561.                 continue;
  1562.             }
  1563.         }
  1564.         if( p_sys->b_master_sync )
  1565.         {
  1566.             mtime_t i_video_drift;
  1567.             mtime_t i_master_drift = p_sys->i_master_drift;
  1568.             mtime_t i_pts;
  1569.             i_pts = date_Get( &id->interpolated_pts ) + 1;
  1570.             if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
  1571.                   || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
  1572.             {
  1573.                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
  1574.                 date_Set( &id->interpolated_pts, p_pic->date );
  1575.                 i_pts = p_pic->date + 1;
  1576.             }
  1577.             i_video_drift = p_pic->date - i_pts;
  1578.             i_duplicate = 1;
  1579.             /* Set the pts of the frame being encoded */
  1580.             p_pic->date = i_pts;
  1581.             if( i_video_drift < (i_master_drift - 50000) )
  1582.             {
  1583. #if 0
  1584.                 msg_Dbg( p_stream, "dropping frame (%i)",
  1585.                          (int)(i_video_drift - i_master_drift) );
  1586. #endif
  1587.                 p_pic->pf_release( p_pic );
  1588.                 continue;
  1589.             }
  1590.             else if( i_video_drift > (i_master_drift + 50000) )
  1591.             {
  1592. #if 0
  1593.                 msg_Dbg( p_stream, "adding frame (%i)",
  1594.                          (int)(i_video_drift - i_master_drift) );
  1595. #endif
  1596.                 i_duplicate = 2;
  1597.             }
  1598.         }
  1599.         if( !id->p_encoder->p_module )
  1600.         {
  1601.             transcode_video_encoder_init( p_stream, id );
  1602.             id->p_f_chain = filter_chain_New( p_stream, "video filter2",
  1603.                                               false,
  1604.                                transcode_video_filter_allocation_init,
  1605.                                transcode_video_filter_allocation_clear,
  1606.                                p_stream->p_sys );
  1607.             /* Deinterlace */
  1608.             if( p_stream->p_sys->b_deinterlace )
  1609.             {
  1610.                 filter_chain_AppendFilter( id->p_f_chain,
  1611.                                            p_sys->psz_deinterlace,
  1612.                                            p_sys->p_deinterlace_cfg,
  1613.                                            &id->p_decoder->fmt_out,
  1614.                                            &id->p_decoder->fmt_out );
  1615.             }
  1616.             /* Take care of the scaling and chroma conversions */
  1617.             if( ( id->p_decoder->fmt_out.video.i_chroma !=
  1618.                   id->p_encoder->fmt_in.video.i_chroma ) ||
  1619.                 ( id->p_decoder->fmt_out.video.i_width !=
  1620.                   id->p_encoder->fmt_in.video.i_width ) ||
  1621.                 ( id->p_decoder->fmt_out.video.i_height !=
  1622.                   id->p_encoder->fmt_in.video.i_height ) )
  1623.             {
  1624.                 filter_chain_AppendFilter( id->p_f_chain,
  1625.                                            NULL, NULL,
  1626.                                            &id->p_decoder->fmt_out,
  1627.                                            &id->p_encoder->fmt_in );
  1628.             }
  1629.             if( p_sys->psz_vf2 )
  1630.             {
  1631.                 const es_format_t *p_fmt_out;
  1632.                 id->p_uf_chain = filter_chain_New( p_stream, "video filter2",
  1633.                                                    true,
  1634.                                    transcode_video_filter_allocation_init,
  1635.                                    transcode_video_filter_allocation_clear,
  1636.                                    p_stream->p_sys );
  1637.                 filter_chain_Reset( id->p_uf_chain, &id->p_encoder->fmt_in,
  1638.                                     &id->p_encoder->fmt_in );
  1639.                 filter_chain_AppendFromString( id->p_uf_chain, p_sys->psz_vf2 );
  1640.                 p_fmt_out = filter_chain_GetFmtOut( id->p_uf_chain );
  1641.                 es_format_Copy( &id->p_encoder->fmt_in, p_fmt_out );
  1642.                 id->p_encoder->fmt_out.video.i_width =
  1643.                     id->p_encoder->fmt_in.video.i_width;
  1644.                 id->p_encoder->fmt_out.video.i_height =
  1645.                     id->p_encoder->fmt_in.video.i_height;
  1646.                 id->p_encoder->fmt_out.video.i_aspect =
  1647.                     id->p_encoder->fmt_in.video.i_aspect;
  1648.             }
  1649.             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
  1650.             {
  1651.                 p_pic->pf_release( p_pic );
  1652.                 transcode_video_close( p_stream, id );
  1653.                 id->b_transcode = false;
  1654.                 return VLC_EGENERIC;
  1655.             }
  1656.         }
  1657.         /* Run filter chain */
  1658.         if( id->p_f_chain )
  1659.             p_pic = filter_chain_VideoFilter( id->p_f_chain, p_pic );
  1660.         /*
  1661.          * Encoding
  1662.          */
  1663.         /* Check if we have a subpicture to overlay */
  1664.         if( p_sys->p_spu )
  1665.         {
  1666.             p_subpic = spu_SortSubpicturesNew( p_sys->p_spu, p_pic->date, false );
  1667.             /* TODO: get another pic */
  1668.         }
  1669.         /* Overlay subpicture */
  1670.         if( p_subpic )
  1671.         {
  1672.             video_format_t fmt;
  1673.             if( p_pic->i_refcount && !filter_chain_GetLength( id->p_f_chain ) )
  1674.             {
  1675.                 /* We can't modify the picture, we need to duplicate it */
  1676.                 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
  1677.                 if( p_tmp )
  1678.                 {
  1679.                     picture_Copy( p_tmp, p_pic );
  1680.                     p_pic->pf_release( p_pic );
  1681.                     p_pic = p_tmp;
  1682.                 }
  1683.             }
  1684.             if( filter_chain_GetLength( id->p_f_chain ) > 0 )
  1685.                 fmt = filter_chain_GetFmtOut( id->p_f_chain )->video;
  1686.             else
  1687.                 fmt = id->p_decoder->fmt_out.video;
  1688.             /* FIXME (shouldn't have to be done here) */
  1689.             fmt.i_sar_num = fmt.i_aspect * fmt.i_height / fmt.i_width;
  1690.             fmt.i_sar_den = VOUT_ASPECT_FACTOR;
  1691.             /* FIXME the mdate() seems highly suspicious */
  1692.             spu_RenderSubpicturesNew( p_sys->p_spu, p_pic, &fmt,
  1693.                                    p_subpic, &id->p_decoder->fmt_out.video, mdate() );
  1694.         }
  1695.         /* Run user specified filter chain */
  1696.         if( id->p_uf_chain )
  1697.             p_pic = filter_chain_VideoFilter( id->p_uf_chain, p_pic );
  1698.         if( p_sys->i_threads == 0 )
  1699.         {
  1700.             block_t *p_block;
  1701.             video_timer_start( id->p_encoder );
  1702.             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
  1703.             video_timer_stop( id->p_encoder );
  1704.             block_ChainAppend( out, p_block );
  1705.         }
  1706.         if( p_sys->b_master_sync )
  1707.         {
  1708.             mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
  1709.             if ( p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT
  1710.                   || p_pic->date - i_pts < -MASTER_SYNC_MAX_DRIFT )
  1711.             {
  1712.                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
  1713.                 date_Set( &id->interpolated_pts, p_pic->date );
  1714.                 i_pts = p_pic->date + 1;
  1715.             }
  1716.             date_Increment( &id->interpolated_pts, 1 );
  1717.         }
  1718.         if( p_sys->b_master_sync && i_duplicate > 1 )
  1719.         {
  1720.             mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
  1721.             if( (p_pic->date - i_pts > MASTER_SYNC_MAX_DRIFT)
  1722.                  || ((p_pic->date - i_pts) < -MASTER_SYNC_MAX_DRIFT) )
  1723.             {
  1724.                 msg_Dbg( p_stream, "drift is too high, resetting master sync" );
  1725.                 date_Set( &id->interpolated_pts, p_pic->date );
  1726.                 i_pts = p_pic->date + 1;
  1727.             }
  1728.             date_Increment( &id->interpolated_pts, 1 );
  1729.             if( p_sys->i_threads >= 1 )
  1730.             {
  1731.                 /* We can't modify the picture, we need to duplicate it */
  1732.                 p_pic2 = video_new_buffer_decoder( id->p_decoder );
  1733.                 if( p_pic2 != NULL )
  1734.                 {
  1735.                     picture_Copy( p_pic2, p_pic );
  1736.                     p_pic2->date = i_pts;
  1737.                 }
  1738.             }
  1739.             else
  1740.             {
  1741.                 block_t *p_block;
  1742.                 p_pic->date = i_pts;
  1743.                 video_timer_start( id->p_encoder );
  1744.                 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
  1745.                 video_timer_stop( id->p_encoder );
  1746.                 block_ChainAppend( out, p_block );
  1747.             }
  1748.         }
  1749.         if( p_sys->i_threads == 0 )
  1750.         {
  1751.             p_pic->pf_release( p_pic );
  1752.         }
  1753.         else
  1754.         {
  1755.             vlc_mutex_lock( &p_sys->lock_out );
  1756.             p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
  1757.             p_sys->i_last_pic %= PICTURE_RING_SIZE;
  1758.             *out = p_sys->p_buffers;
  1759.             p_sys->p_buffers = NULL;
  1760.             if( p_pic2 != NULL )
  1761.             {
  1762.                 p_sys->pp_pics[p_sys->i_last_pic++] = p_pic2;
  1763.                 p_sys->i_last_pic %= PICTURE_RING_SIZE;
  1764.             }
  1765.             vlc_cond_signal( &p_sys->cond );
  1766.             vlc_mutex_unlock( &p_sys->lock_out );
  1767.         }
  1768.     }
  1769.     return VLC_SUCCESS;
  1770. }
  1771. static void* EncoderThread( vlc_object_t* p_this )
  1772. {
  1773.     sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_this;
  1774.     sout_stream_id_t *id = p_sys->id_video;
  1775.     picture_t *p_pic;
  1776.     int canc = vlc_savecancel ();
  1777.     while( vlc_object_alive (p_sys) && !p_sys->b_error )
  1778.     {
  1779.         block_t *p_block;
  1780.         vlc_mutex_lock( &p_sys->lock_out );
  1781.         while( p_sys->i_last_pic == p_sys->i_first_pic )
  1782.         {
  1783.             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
  1784.             if( !vlc_object_alive (p_sys) || p_sys->b_error ) break;
  1785.         }
  1786.         if( !vlc_object_alive (p_sys) || p_sys->b_error )
  1787.         {
  1788.             vlc_mutex_unlock( &p_sys->lock_out );
  1789.             break;
  1790.         }
  1791.         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
  1792.         p_sys->i_first_pic %= PICTURE_RING_SIZE;
  1793.         vlc_mutex_unlock( &p_sys->lock_out );
  1794.         video_timer_start( id->p_encoder );
  1795.         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
  1796.         video_timer_stop( id->p_encoder );
  1797.         vlc_mutex_lock( &p_sys->lock_out );
  1798.         block_ChainAppend( &p_sys->p_buffers, p_block );
  1799.         vlc_mutex_unlock( &p_sys->lock_out );
  1800.         p_pic->pf_release( p_pic );
  1801.     }
  1802.     while( p_sys->i_last_pic != p_sys->i_first_pic )
  1803.     {
  1804.         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
  1805.         p_sys->i_first_pic %= PICTURE_RING_SIZE;
  1806.         p_pic->pf_release( p_pic );
  1807.     }
  1808.     block_ChainRelease( p_sys->p_buffers );
  1809.     vlc_restorecancel (canc);
  1810.     return NULL;
  1811. }
  1812. struct picture_sys_t
  1813. {
  1814.     vlc_object_t *p_owner;
  1815. };
  1816. static void video_release_buffer( picture_t *p_pic )
  1817. {
  1818.     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
  1819.     {
  1820.         video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
  1821.     }
  1822.     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
  1823. }
  1824. static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring,
  1825.                                     sout_stream_sys_t *p_sys )
  1826. {
  1827.     decoder_t *p_dec = (decoder_t *)p_this;
  1828.     picture_t *p_pic;
  1829.     int i;
  1830.     /* Find an empty space in the picture ring buffer */
  1831.     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1832.     {
  1833.         if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
  1834.         {
  1835.             pp_ring[i]->i_status = RESERVED_PICTURE;
  1836.             return pp_ring[i];
  1837.         }
  1838.     }
  1839.     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1840.     {
  1841.         if( pp_ring[i] == NULL ) break;
  1842.     }
  1843.     if( i == PICTURE_RING_SIZE && p_sys->i_threads >= 1 )
  1844.     {
  1845.         int i_first_pic = p_sys->i_first_pic;
  1846.         if( p_sys->i_first_pic != p_sys->i_last_pic )
  1847.         {
  1848.             /* Encoder still has stuff to encode, wait to clear-up the list */
  1849.             while( p_sys->i_first_pic == i_first_pic )
  1850.                 msleep( 5000 );
  1851.         }
  1852.         /* Find an empty space in the picture ring buffer */
  1853.         for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1854.         {
  1855.             if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
  1856.             {
  1857.                 pp_ring[i]->i_status = RESERVED_PICTURE;
  1858.                 return pp_ring[i];
  1859.             }
  1860.         }
  1861.         for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1862.         {
  1863.             if( pp_ring[i] == NULL ) break;
  1864.         }
  1865.     }
  1866.     if( i == PICTURE_RING_SIZE )
  1867.     {
  1868.         msg_Err( p_this, "decoder/filter is leaking pictures, "
  1869.                  "resetting its ring buffer" );
  1870.         for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1871.         {
  1872.             pp_ring[i]->pf_release( pp_ring[i] );
  1873.         }
  1874.         i = 0;
  1875.     }
  1876.     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
  1877.     p_pic = picture_New( p_dec->fmt_out.video.i_chroma,
  1878.                          p_dec->fmt_out.video.i_width,
  1879.                          p_dec->fmt_out.video.i_height,
  1880.                          p_dec->fmt_out.video.i_aspect );
  1881.     if( !p_pic ) return NULL;
  1882.     p_pic->p_sys = calloc( 1, sizeof(picture_sys_t) );
  1883.     if( !p_pic->p_sys )
  1884.     {
  1885.         picture_Release( p_pic );
  1886.         return NULL;
  1887.     }
  1888.     p_pic->pf_release = video_release_buffer;
  1889.     p_pic->i_refcount = 0;
  1890.     pp_ring[i] = p_pic;
  1891.     return p_pic;
  1892. }
  1893. static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
  1894. {
  1895.     return video_new_buffer( VLC_OBJECT(p_dec),
  1896.                              p_dec->p_owner->pp_pics, p_dec->p_owner->p_sys );
  1897. }
  1898. static picture_t *video_new_buffer_filter( filter_t *p_filter )
  1899. {
  1900.     return video_new_buffer( VLC_OBJECT(p_filter),
  1901.                              p_filter->p_owner->pp_pics,
  1902.                              p_filter->p_owner->p_sys );
  1903. }
  1904. static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
  1905. {
  1906.     VLC_UNUSED(p_this);
  1907.     if( p_pic )
  1908.     {
  1909.         free( p_pic->p_q );
  1910.         free( p_pic->p_data_orig );
  1911.         free( p_pic->p_sys );
  1912.         free( p_pic );
  1913.     }
  1914. }
  1915. static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
  1916. {
  1917.     VLC_UNUSED(p_decoder);
  1918.     p_pic->i_refcount = 0;
  1919.     p_pic->i_status = DESTROYED_PICTURE;
  1920.     picture_CleanupQuant( p_pic );
  1921. }
  1922. static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
  1923. {
  1924.     VLC_UNUSED(p_filter);
  1925.     p_pic->i_refcount = 0;
  1926.     p_pic->i_status = DESTROYED_PICTURE;
  1927.     picture_CleanupQuant( p_pic );
  1928. }
  1929. static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
  1930. {
  1931.     VLC_UNUSED(p_dec);
  1932.     p_pic->i_refcount++;
  1933. }
  1934. static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
  1935. {
  1936.     VLC_UNUSED(p_dec);
  1937.     video_release_buffer( p_pic );
  1938. }
  1939. /*
  1940.  * SPU
  1941.  */
  1942. static subpicture_t *spu_new_buffer( decoder_t * );
  1943. static void spu_del_buffer( decoder_t *, subpicture_t * );
  1944. static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
  1945. {
  1946.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1947.     /*
  1948.      * Open decoder
  1949.      */
  1950.     /* Initialization of decoder structures */
  1951.     id->p_decoder->pf_decode_sub = NULL;
  1952.     id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
  1953.     id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
  1954.     id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
  1955.     /* id->p_decoder->p_cfg = p_sys->p_spu_cfg; */
  1956.     id->p_decoder->p_module =
  1957.         module_need( id->p_decoder, "decoder", "$codec", false );
  1958.     if( !id->p_decoder->p_module )
  1959.     {
  1960.         msg_Err( p_stream, "cannot find spu decoder" );
  1961.         return VLC_EGENERIC;
  1962.     }
  1963.     if( !p_sys->b_soverlay )
  1964.     {
  1965.         /* Open encoder */
  1966.         /* Initialization of encoder format structures */
  1967.         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
  1968.                         id->p_decoder->fmt_in.i_codec );
  1969.         id->p_encoder->p_cfg = p_sys->p_spu_cfg;
  1970.         id->p_encoder->p_module =
  1971.             module_need( id->p_encoder, "encoder", p_sys->psz_senc, true );
  1972.         if( !id->p_encoder->p_module )
  1973.         {
  1974.             module_unneed( id->p_decoder, id->p_decoder->p_module );
  1975.             msg_Err( p_stream, "cannot find spu encoder (%s)", p_sys->psz_senc );
  1976.             return VLC_EGENERIC;
  1977.         }
  1978.     }
  1979.     if( !p_sys->p_spu )
  1980.     {
  1981.         p_sys->p_spu = spu_Create( p_stream );
  1982.         spu_Init( p_sys->p_spu );
  1983.     }
  1984.     return VLC_SUCCESS;
  1985. }
  1986. static void transcode_spu_close( sout_stream_id_t *id)
  1987. {
  1988.     /* Close decoder */
  1989.     if( id->p_decoder->p_module )
  1990.         module_unneed( id->p_decoder, id->p_decoder->p_module );
  1991.     if( id->p_decoder->p_description )
  1992.         vlc_meta_Delete( id->p_decoder->p_description );
  1993.     /* Close encoder */
  1994.     if( id->p_encoder->p_module )
  1995.         module_unneed( id->p_encoder, id->p_encoder->p_module );
  1996. }
  1997. static int transcode_spu_process( sout_stream_t *p_stream,
  1998.                                   sout_stream_id_t *id,
  1999.                                   block_t *in, block_t **out )
  2000. {
  2001.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  2002.     subpicture_t *p_subpic;
  2003.     *out = NULL;
  2004.     p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
  2005.     if( !p_subpic )
  2006.         return VLC_EGENERIC;
  2007.     sout_UpdateStatistic( p_stream->p_sout, SOUT_STATISTIC_DECODED_SUBTITLE, 1 );
  2008.     if( p_sys->b_master_sync && p_sys->i_master_drift )
  2009.     {
  2010.         p_subpic->i_start -= p_sys->i_master_drift;
  2011.         if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
  2012.     }
  2013.     if( p_sys->b_soverlay )
  2014.     {
  2015.         spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
  2016.     }
  2017.     else
  2018.     {
  2019.         block_t *p_block;
  2020.         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
  2021.         spu_del_buffer( id->p_decoder, p_subpic );
  2022.         if( p_block )
  2023.         {
  2024.             block_ChainAppend( out, p_block );
  2025.             return VLC_SUCCESS;
  2026.         }
  2027.     }
  2028.     return VLC_EGENERIC;
  2029. }
  2030. static subpicture_t *spu_new_buffer( decoder_t *p_dec )
  2031. {
  2032.     VLC_UNUSED( p_dec );
  2033.     return subpicture_New();
  2034. }
  2035. static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
  2036. {
  2037.     VLC_UNUSED( p_dec );
  2038.     subpicture_Delete( p_subpic );
  2039. }
  2040. /*
  2041.  * OSD menu
  2042.  */
  2043. static int transcode_osd_new( sout_stream_t *p_stream, sout_stream_id_t *id )
  2044. {
  2045.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  2046.     id->p_decoder->fmt_in.i_cat = SPU_ES;
  2047.     id->p_encoder->fmt_out.psz_language = strdup( "osd" );
  2048.     if( p_sys->i_osdcodec != 0 || p_sys->psz_osdenc )
  2049.     {
  2050.         msg_Dbg( p_stream, "creating osdmenu transcoding from fcc=`%4.4s' "
  2051.                  "to fcc=`%4.4s'", (char*)&id->p_encoder->fmt_out.i_codec,
  2052.                  (char*)&p_sys->i_osdcodec );
  2053.         /* Complete destination format */
  2054.         id->p_encoder->fmt_out.i_codec = p_sys->i_osdcodec;
  2055.         /* Open encoder */
  2056.         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
  2057.                         VLC_FOURCC('Y','U','V','A') );
  2058.         id->p_encoder->fmt_in.psz_language = strdup( "osd" );
  2059.         id->p_encoder->p_cfg = p_sys->p_osd_cfg;
  2060.         id->p_encoder->p_module =
  2061.             module_need( id->p_encoder, "encoder", p_sys->psz_osdenc, true );
  2062.         if( !id->p_encoder->p_module )
  2063.         {
  2064.             msg_Err( p_stream, "cannot find spu encoder (%s)", p_sys->psz_osdenc );
  2065.             goto error;
  2066.         }
  2067.         /* open output stream */
  2068.         id->id = sout_StreamIdAdd( p_sys->p_out, &id->p_encoder->fmt_out );
  2069.         id->b_transcode = true;
  2070.         if( !id->id ) goto error;
  2071.     }
  2072.     else
  2073.     {
  2074.         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
  2075.                  (char*)&id->p_decoder->fmt_out.i_codec );
  2076.         id->id = sout_StreamIdAdd( p_sys->p_out, &id->p_decoder->fmt_out );
  2077.         id->b_transcode = false;
  2078.         if( !id->id ) goto error;
  2079.     }
  2080.     if( !p_sys->p_spu )
  2081.     {
  2082.         p_sys->p_spu = spu_Create( p_stream );
  2083.         spu_Init( p_sys->p_spu );
  2084.     }
  2085.     return VLC_SUCCESS;
  2086.  error:
  2087.     msg_Err( p_stream, "starting osd encoding thread failed" );
  2088.     if( id->p_encoder->p_module )
  2089.             module_unneed( id->p_encoder, id->p_encoder->p_module );
  2090.     p_sys->b_osd = false;
  2091.     return VLC_EGENERIC;
  2092. }
  2093. static void transcode_osd_close( sout_stream_t *p_stream, sout_stream_id_t *id)
  2094. {
  2095.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  2096.     /* Close encoder */
  2097.     if( id )
  2098.     {
  2099.         if( id->p_encoder->p_module )
  2100.             module_unneed( id->p_encoder, id->p_encoder->p_module );
  2101.     }
  2102.     p_sys->b_osd = false;
  2103. }
  2104. static int transcode_osd_process( sout_stream_t *p_stream,
  2105.                                   sout_stream_id_t *id,
  2106.                                   block_t *in, block_t **out )
  2107. {
  2108.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  2109.     subpicture_t *p_subpic = NULL;
  2110.     /* Check if we have a subpicture to send */
  2111.     if( p_sys->p_spu && in->i_dts > 0)
  2112.     {
  2113.         p_subpic = spu_SortSubpicturesNew( p_sys->p_spu, in->i_dts, false );
  2114.     }
  2115.     else
  2116.     {
  2117.         msg_Warn( p_stream, "spu channel not initialized, doing it now" );
  2118.         if( !p_sys->p_spu )
  2119.         {
  2120.             p_sys->p_spu = spu_Create( p_stream );
  2121.             spu_Init( p_sys->p_spu );
  2122.         }
  2123.     }
  2124.     if( p_subpic )
  2125.     {
  2126.         block_t *p_block = NULL;
  2127.         if( p_sys->b_master_sync && p_sys->i_master_drift )
  2128.         {
  2129.             p_subpic->i_start -= p_sys->i_master_drift;
  2130.             if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
  2131.         }
  2132.         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
  2133.         subpicture_Delete( p_subpic );
  2134.         if( p_block )
  2135.         {
  2136.             p_block->i_dts = p_block->i_pts = in->i_dts;
  2137.             block_ChainAppend( out, p_block );
  2138.             return VLC_SUCCESS;
  2139.         }
  2140.     }
  2141.     return VLC_EGENERIC;
  2142. }