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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * transcode.c: transcoding stream output module
  3.  *****************************************************************************
  4.  * Copyright (C) 2003-2004 VideoLAN
  5.  * $Id: transcode.c 9268 2004-11-10 13:01:48Z gbazin $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *          Gildas Bazin <gbazin@videolan.org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <vlc/vlc.h>
  30. #include <vlc/input.h>
  31. #include <vlc/sout.h>
  32. #include <vlc/vout.h>
  33. #include <vlc/decoder.h>
  34. #include "vlc_filter.h"
  35. #include "osd.h"
  36. /*****************************************************************************
  37.  * Module descriptor
  38.  *****************************************************************************/
  39. #define VENC_TEXT N_("Video encoder")
  40. #define VENC_LONGTEXT N_( 
  41.     "Allows you to specify the video encoder to use and its associated " 
  42.     "options." )
  43. #define VCODEC_TEXT N_("Destination video codec")
  44. #define VCODEC_LONGTEXT N_( 
  45.     "Allows you to specify the destination video codec used for the " 
  46.     "streaming output." )
  47. #define VB_TEXT N_("Video bitrate")
  48. #define VB_LONGTEXT N_( 
  49.     "Allows you to specify the video bitrate used for the streaming " 
  50.     "output." )
  51. #define SCALE_TEXT N_("Video scaling")
  52. #define SCALE_LONGTEXT N_( 
  53.     "Allows you to scale the video before encoding." )
  54. #define FPS_TEXT N_("Video frame-rate")
  55. #define FPS_LONGTEXT N_( 
  56.     "Allows you to specify an output frame rate for the video." )
  57. #define DEINTERLACE_TEXT N_("Deinterlace video")
  58. #define DEINTERLACE_LONGTEXT N_( 
  59.     "Allows you to deinterlace the video before encoding." )
  60. #define WIDTH_TEXT N_("Video width")
  61. #define WIDTH_LONGTEXT N_( 
  62.     "Allows you to specify the output video width." )
  63. #define HEIGHT_TEXT N_("Video height")
  64. #define HEIGHT_LONGTEXT N_( 
  65.     "Allows you to specify the output video height." )
  66. #define CROPTOP_TEXT N_("Video crop top")
  67. #define CROPTOP_LONGTEXT N_( 
  68.     "Allows you to specify the top coordinate for the video cropping." )
  69. #define CROPLEFT_TEXT N_("Video crop left")
  70. #define CROPLEFT_LONGTEXT N_( 
  71.     "Allows you to specify the left coordinate for the video cropping." )
  72. #define CROPBOTTOM_TEXT N_("Video crop bottom")
  73. #define CROPBOTTOM_LONGTEXT N_( 
  74.     "Allows you to specify the bottom coordinate for the video cropping." )
  75. #define CROPRIGHT_TEXT N_("Video crop right")
  76. #define CROPRIGHT_LONGTEXT N_( 
  77.     "Allows you to specify the right coordinate for the video cropping." )
  78. #define AENC_TEXT N_("Audio encoder")
  79. #define AENC_LONGTEXT N_( 
  80.     "Allows you to specify the audio encoder to use and its associated " 
  81.     "options." )
  82. #define ACODEC_TEXT N_("Destination audio codec")
  83. #define ACODEC_LONGTEXT N_( 
  84.     "Allows you to specify the destination audio codec used for the " 
  85.     "streaming output." )
  86. #define AB_TEXT N_("Audio bitrate")
  87. #define AB_LONGTEXT N_( 
  88.     "Allows you to specify the audio bitrate used for the streaming " 
  89.     "output." )
  90. #define ARATE_TEXT N_("Audio sample rate")
  91. #define ARATE_LONGTEXT N_( 
  92.     "Allows you to specify the audio sample rate used for the streaming " 
  93.     "output." )
  94. #define ACHANS_TEXT N_("Audio channels")
  95. #define ACHANS_LONGTEXT N_( 
  96.     "Allows you to specify the number of audio channels used for the " 
  97.     "streaming output." )
  98. #define SENC_TEXT N_("Subtitles encoder")
  99. #define SENC_LONGTEXT N_( 
  100.     "Allows you to specify the subtitles encoder to use and its associated " 
  101.     "options." )
  102. #define SCODEC_TEXT N_("Destination subtitles codec")
  103. #define SCODEC_LONGTEXT N_( 
  104.     "Allows you to specify the destination subtitles codec used for the " 
  105.     "streaming output." )
  106. #define SFILTER_TEXT N_("Subpictures filter")
  107. #define SFILTER_LONGTEXT N_( 
  108.     "Allows you to specify subpictures filters used during the video " 
  109.     "transcoding. The subpictures produced by the filters will be overlayed " 
  110.     "directly onto the video." )
  111. #define THREADS_TEXT N_("Number of threads")
  112. #define THREADS_LONGTEXT N_( 
  113.     "Allows you to specify the number of threads used for the transcoding." )
  114. #define ASYNC_TEXT N_("Synchronise on audio track")
  115. #define ASYNC_LONGTEXT N_( 
  116.     "This option will drop/duplicate video frames to synchronise the video " 
  117.     "track on the audio track." )
  118. static int  Open ( vlc_object_t * );
  119. static void Close( vlc_object_t * );
  120. #define SOUT_CFG_PREFIX "sout-transcode-"
  121. vlc_module_begin();
  122.     set_description( _("Transcode stream output") );
  123.     set_capability( "sout stream", 50 );
  124.     add_shortcut( "transcode" );
  125.     set_callbacks( Open, Close );
  126.     add_string( SOUT_CFG_PREFIX "venc", NULL, NULL, VENC_TEXT,
  127.                 VENC_LONGTEXT, VLC_FALSE );
  128.     add_string( SOUT_CFG_PREFIX "vcodec", NULL, NULL, VCODEC_TEXT,
  129.                 VCODEC_LONGTEXT, VLC_FALSE );
  130.     add_integer( SOUT_CFG_PREFIX "vb", 800 * 1000, NULL, VB_TEXT,
  131.                  VB_LONGTEXT, VLC_FALSE );
  132.     add_float( SOUT_CFG_PREFIX "scale", 1, NULL, SCALE_TEXT,
  133.                SCALE_LONGTEXT, VLC_FALSE );
  134.     add_float( SOUT_CFG_PREFIX "fps", 0, NULL, FPS_TEXT,
  135.                FPS_LONGTEXT, VLC_FALSE );
  136.     add_bool( SOUT_CFG_PREFIX "deinterlace", 0, NULL, DEINTERLACE_TEXT,
  137.               DEINTERLACE_LONGTEXT, VLC_FALSE );
  138.     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
  139.                  WIDTH_LONGTEXT, VLC_TRUE );
  140.     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
  141.                  HEIGHT_LONGTEXT, VLC_TRUE );
  142.     add_integer( SOUT_CFG_PREFIX "croptop", 0, NULL, CROPTOP_TEXT,
  143.                  CROPTOP_LONGTEXT, VLC_TRUE );
  144.     add_integer( SOUT_CFG_PREFIX "cropleft", 0, NULL, CROPLEFT_TEXT,
  145.                  CROPLEFT_LONGTEXT, VLC_TRUE );
  146.     add_integer( SOUT_CFG_PREFIX "cropbottom", 0, NULL, CROPBOTTOM_TEXT,
  147.                  CROPBOTTOM_LONGTEXT, VLC_TRUE );
  148.     add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
  149.                  CROPRIGHT_LONGTEXT, VLC_TRUE );
  150.     add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
  151.                 AENC_LONGTEXT, VLC_FALSE );
  152.     add_string( SOUT_CFG_PREFIX "acodec", NULL, NULL, ACODEC_TEXT,
  153.                 ACODEC_LONGTEXT, VLC_FALSE );
  154.     add_integer( SOUT_CFG_PREFIX "ab", 64000, NULL, AB_TEXT,
  155.                  AB_LONGTEXT, VLC_FALSE );
  156.     add_integer( SOUT_CFG_PREFIX "channels", 0, NULL, ACHANS_TEXT,
  157.                  ACHANS_LONGTEXT, VLC_FALSE );
  158.     add_integer( SOUT_CFG_PREFIX "samplerate", 0, NULL, ARATE_TEXT,
  159.                  ARATE_LONGTEXT, VLC_TRUE );
  160.     add_string( SOUT_CFG_PREFIX "senc", NULL, NULL, SENC_TEXT,
  161.                 SENC_LONGTEXT, VLC_FALSE );
  162.     add_string( SOUT_CFG_PREFIX "scodec", NULL, NULL, SCODEC_TEXT,
  163.                 SCODEC_LONGTEXT, VLC_FALSE );
  164.     add_bool( SOUT_CFG_PREFIX "soverlay", 0, NULL, SCODEC_TEXT,
  165.                SCODEC_LONGTEXT, VLC_FALSE );
  166.     add_string( SOUT_CFG_PREFIX "sfilter", NULL, NULL, SFILTER_TEXT,
  167.                 SFILTER_LONGTEXT, VLC_FALSE );
  168.     add_integer( SOUT_CFG_PREFIX "threads", 0, NULL, THREADS_TEXT,
  169.                  THREADS_LONGTEXT, VLC_TRUE );
  170.     add_bool( SOUT_CFG_PREFIX "audio-sync", 0, NULL, ASYNC_TEXT,
  171.               ASYNC_LONGTEXT, VLC_FALSE );
  172. vlc_module_end();
  173. static const char *ppsz_sout_options[] = {
  174.     "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
  175.     "scale", "fps", "width", "height", "deinterlace", "threads",
  176.     "aenc", "acodec", "ab", "samplerate", "channels",
  177.     "senc", "scodec", "soverlay", "sfilter",
  178.     "audio-sync", NULL
  179. };
  180. /*****************************************************************************
  181.  * Exported prototypes
  182.  *****************************************************************************/
  183. static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
  184. static int               Del ( sout_stream_t *, sout_stream_id_t * );
  185. static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
  186. static int  transcode_audio_new    ( sout_stream_t *, sout_stream_id_t * );
  187. static void transcode_audio_close  ( sout_stream_t *, sout_stream_id_t * );
  188. static int  transcode_audio_process( sout_stream_t *, sout_stream_id_t *,
  189.                                      block_t *, block_t ** );
  190. static aout_buffer_t *audio_new_buffer( decoder_t *, int );
  191. static void audio_del_buffer( decoder_t *, aout_buffer_t * );
  192. static int  transcode_video_new    ( sout_stream_t *, sout_stream_id_t * );
  193. static void transcode_video_close  ( sout_stream_t *, sout_stream_id_t * );
  194. static int  transcode_video_encoder_open( sout_stream_t *, sout_stream_id_t *);
  195. static int  transcode_video_process( sout_stream_t *, sout_stream_id_t *,
  196.                                      block_t *, block_t ** );
  197. static void video_del_buffer( vlc_object_t *, picture_t * );
  198. static picture_t *video_new_buffer_decoder( decoder_t * );
  199. static void video_del_buffer_decoder( decoder_t *, picture_t * );
  200. static void video_link_picture_decoder( decoder_t *, picture_t * );
  201. static void video_unlink_picture_decoder( decoder_t *, picture_t * );
  202. static picture_t *video_new_buffer_filter( filter_t * );
  203. static void video_del_buffer_filter( filter_t *, picture_t * );
  204. static int  transcode_spu_new    ( sout_stream_t *, sout_stream_id_t * );
  205. static void transcode_spu_close  ( sout_stream_t *, sout_stream_id_t * );
  206. static int  transcode_spu_process( sout_stream_t *, sout_stream_id_t *,
  207.                                    block_t *, block_t ** );
  208. static int  EncoderThread( struct sout_stream_sys_t * p_sys );
  209. static int pi_channels_maps[6] =
  210. {
  211.     0,
  212.     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
  213.     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
  214.     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
  215.      | AOUT_CHAN_REARRIGHT,
  216.     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
  217.      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  218. };
  219. #define PICTURE_RING_SIZE 64
  220. #define SUBPICTURE_RING_SIZE 20
  221. struct sout_stream_sys_t
  222. {
  223.     VLC_COMMON_MEMBERS
  224.     sout_stream_t   *p_out;
  225.     sout_stream_id_t *id_video;
  226.     block_t         *p_buffers;
  227.     vlc_mutex_t     lock_out;
  228.     vlc_cond_t      cond;
  229.     picture_t *     pp_pics[PICTURE_RING_SIZE];
  230.     int             i_first_pic, i_last_pic;
  231.     /* Audio */
  232.     vlc_fourcc_t    i_acodec;   /* codec audio (0 if not transcode) */
  233.     char            *psz_aenc;
  234.     sout_cfg_t      *p_audio_cfg;
  235.     int             i_sample_rate;
  236.     int             i_channels;
  237.     int             i_abitrate;
  238.     /* Video */
  239.     vlc_fourcc_t    i_vcodec;   /* codec video (0 if not transcode) */
  240.     char            *psz_venc;
  241.     sout_cfg_t      *p_video_cfg;
  242.     int             i_vbitrate;
  243.     double          f_scale;
  244.     double          f_fps;
  245.     int             i_width;
  246.     int             i_height;
  247.     vlc_bool_t      b_deinterlace;
  248.     int             i_threads;
  249.     int             i_crop_top;
  250.     int             i_crop_bottom;
  251.     int             i_crop_right;
  252.     int             i_crop_left;
  253.     /* SPU */
  254.     vlc_fourcc_t    i_scodec;   /* codec spu (0 if not transcode) */
  255.     char            *psz_senc;
  256.     vlc_bool_t      b_soverlay;
  257.     sout_cfg_t      *p_spu_cfg;
  258.     spu_t           *p_spu;
  259.     /* Sync */
  260.     vlc_bool_t      b_master_sync;
  261.     mtime_t         i_master_drift;
  262. };
  263. struct decoder_owner_sys_t
  264. {
  265.     picture_t *pp_pics[PICTURE_RING_SIZE];
  266. };
  267. struct filter_owner_sys_t
  268. {
  269.     picture_t *pp_pics[PICTURE_RING_SIZE];
  270. };
  271. /*****************************************************************************
  272.  * Open:
  273.  *****************************************************************************/
  274. static int Open( vlc_object_t *p_this )
  275. {
  276.     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
  277.     sout_stream_sys_t *p_sys;
  278.     vlc_value_t       val;
  279.     p_sys = vlc_object_create( p_this, sizeof( sout_stream_sys_t ) );
  280.     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
  281.     if( !p_sys->p_out )
  282.     {
  283.         msg_Err( p_stream, "cannot create chain" );
  284.         free( p_sys );
  285.         return VLC_EGENERIC;
  286.     }
  287.     p_sys->i_master_drift = 0;
  288.     sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
  289.                    p_stream->p_cfg );
  290.     /* Audio transcoding parameters */
  291.     var_Get( p_stream, SOUT_CFG_PREFIX "aenc", &val );
  292.     p_sys->psz_aenc = NULL;
  293.     p_sys->p_audio_cfg = NULL;
  294.     if( val.psz_string && *val.psz_string )
  295.     {
  296.         char *psz_next;
  297.         psz_next = sout_CfgCreate( &p_sys->psz_aenc, &p_sys->p_audio_cfg,
  298.                                    val.psz_string );
  299.         if( psz_next ) free( psz_next );
  300.     }
  301.     if( val.psz_string ) free( val.psz_string );
  302.     var_Get( p_stream, SOUT_CFG_PREFIX "acodec", &val );
  303.     p_sys->i_acodec = 0;
  304.     if( val.psz_string && *val.psz_string )
  305.     {
  306.         char fcc[4] = "    ";
  307.         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
  308.         p_sys->i_acodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
  309.     }
  310.     if( val.psz_string ) free( val.psz_string );
  311.     var_Get( p_stream, SOUT_CFG_PREFIX "ab", &val );
  312.     p_sys->i_abitrate = val.i_int;
  313.     if( p_sys->i_abitrate < 4000 ) p_sys->i_abitrate *= 1000;
  314.     var_Get( p_stream, SOUT_CFG_PREFIX "samplerate", &val );
  315.     p_sys->i_sample_rate = val.i_int;
  316.     var_Get( p_stream, SOUT_CFG_PREFIX "channels", &val );
  317.     p_sys->i_channels = val.i_int;
  318.     if( p_sys->i_acodec )
  319.     {
  320.         msg_Dbg( p_stream, "codec audio=%4.4s %dHz %d channels %dKb/s",
  321.                  (char *)&p_sys->i_acodec, p_sys->i_sample_rate,
  322.                  p_sys->i_channels, p_sys->i_abitrate / 1000 );
  323.     }
  324.     /* Video transcoding parameters */
  325.     var_Get( p_stream, SOUT_CFG_PREFIX "venc", &val );
  326.     p_sys->psz_venc = NULL;
  327.     p_sys->p_video_cfg = NULL;
  328.     if( val.psz_string && *val.psz_string )
  329.     {
  330.         char *psz_next;
  331.         psz_next = sout_CfgCreate( &p_sys->psz_venc, &p_sys->p_video_cfg,
  332.                                    val.psz_string );
  333.         if( psz_next ) free( psz_next );
  334.     }
  335.     if( val.psz_string ) free( val.psz_string );
  336.     var_Get( p_stream, SOUT_CFG_PREFIX "vcodec", &val );
  337.     p_sys->i_vcodec = 0;
  338.     if( val.psz_string && *val.psz_string )
  339.     {
  340.         char fcc[4] = "    ";
  341.         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
  342.         p_sys->i_vcodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
  343.     }
  344.     if( val.psz_string ) free( val.psz_string );
  345.     var_Get( p_stream, SOUT_CFG_PREFIX "vb", &val );
  346.     p_sys->i_vbitrate = val.i_int;
  347.     if( p_sys->i_vbitrate < 16000 ) p_sys->i_vbitrate *= 1000;
  348.     var_Get( p_stream, SOUT_CFG_PREFIX "scale", &val );
  349.     p_sys->f_scale = val.f_float;
  350.     var_Get( p_stream, SOUT_CFG_PREFIX "fps", &val );
  351.     p_sys->f_fps = val.f_float;
  352.     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
  353.     p_sys->i_width = val.i_int;
  354.     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
  355.     p_sys->i_height = val.i_int;
  356.     var_Get( p_stream, SOUT_CFG_PREFIX "deinterlace", &val );
  357.     p_sys->b_deinterlace = val.b_bool;
  358.     var_Get( p_stream, SOUT_CFG_PREFIX "croptop", &val );
  359.     p_sys->i_crop_top = val.i_int;
  360.     var_Get( p_stream, SOUT_CFG_PREFIX "cropbottom", &val );
  361.     p_sys->i_crop_bottom = val.i_int;
  362.     var_Get( p_stream, SOUT_CFG_PREFIX "cropleft", &val );
  363.     p_sys->i_crop_left = val.i_int;
  364.     var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
  365.     p_sys->i_crop_right = val.i_int;
  366.     var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
  367.     p_sys->i_threads = val.i_int;
  368.     if( p_sys->i_vcodec )
  369.     {
  370.         msg_Dbg( p_stream, "codec video=%4.4s %dx%d scaling: %f %dkb/s",
  371.                  (char *)&p_sys->i_vcodec, p_sys->i_width, p_sys->i_height,
  372.                  p_sys->f_scale, p_sys->i_vbitrate / 1000 );
  373.     }
  374.     /* Subpictures transcoding parameters */
  375.     p_sys->p_spu = 0;
  376.     p_sys->psz_senc = NULL;
  377.     p_sys->p_spu_cfg = NULL;
  378.     p_sys->i_scodec = 0;
  379.     var_Get( p_stream, SOUT_CFG_PREFIX "senc", &val );
  380.     if( val.psz_string && *val.psz_string )
  381.     {
  382.         char *psz_next;
  383.         psz_next = sout_CfgCreate( &p_sys->psz_senc, &p_sys->p_spu_cfg,
  384.                                    val.psz_string );
  385.         if( psz_next ) free( psz_next );
  386.     }
  387.     if( val.psz_string ) free( val.psz_string );
  388.     var_Get( p_stream, SOUT_CFG_PREFIX "scodec", &val );
  389.     if( val.psz_string && *val.psz_string )
  390.     {
  391.         char fcc[4] = "    ";
  392.         memcpy( fcc, val.psz_string, __MIN( strlen( val.psz_string ), 4 ) );
  393.         p_sys->i_scodec = VLC_FOURCC( fcc[0], fcc[1], fcc[2], fcc[3] );
  394.     }
  395.     if( val.psz_string ) free( val.psz_string );
  396.     if( p_sys->i_scodec )
  397.     {
  398.         msg_Dbg( p_stream, "codec spu=%4.4s", (char *)&p_sys->i_acodec );
  399.     }
  400.     var_Get( p_stream, SOUT_CFG_PREFIX "soverlay", &val );
  401.     p_sys->b_soverlay = val.b_bool;
  402.     var_Get( p_stream, SOUT_CFG_PREFIX "sfilter", &val );
  403.     if( val.psz_string && *val.psz_string )
  404.     {
  405.         p_sys->p_spu = spu_Create( p_stream );
  406.         var_Create( p_sys->p_spu, "sub-filter", VLC_VAR_STRING );
  407.         var_Set( p_sys->p_spu, "sub-filter", val );
  408.         spu_Init( p_sys->p_spu );
  409.     }
  410.     if( val.psz_string ) free( val.psz_string );
  411.     var_Get( p_stream, SOUT_CFG_PREFIX "audio-sync", &val );
  412.     p_sys->b_master_sync = val.b_bool;
  413.     if( p_sys->f_fps > 0 ) p_sys->b_master_sync = VLC_TRUE;
  414.     p_stream->pf_add    = Add;
  415.     p_stream->pf_del    = Del;
  416.     p_stream->pf_send   = Send;
  417.     p_stream->p_sys     = p_sys;
  418.     return VLC_SUCCESS;
  419. }
  420. /*****************************************************************************
  421.  * Close:
  422.  *****************************************************************************/
  423. static void Close( vlc_object_t * p_this )
  424. {
  425.     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
  426.     sout_stream_sys_t   *p_sys = p_stream->p_sys;
  427.     sout_StreamDelete( p_sys->p_out );
  428.     while( p_sys->p_audio_cfg != NULL )
  429.     {
  430.         sout_cfg_t *p_next = p_sys->p_audio_cfg->p_next;
  431.         if( p_sys->p_audio_cfg->psz_name )
  432.             free( p_sys->p_audio_cfg->psz_name );
  433.         if( p_sys->p_audio_cfg->psz_value )
  434.             free( p_sys->p_audio_cfg->psz_value );
  435.         free( p_sys->p_audio_cfg );
  436.         p_sys->p_audio_cfg = p_next;
  437.     }
  438.     if( p_sys->psz_aenc ) free( p_sys->psz_aenc );
  439.     while( p_sys->p_video_cfg != NULL )
  440.     {
  441.         sout_cfg_t *p_next = p_sys->p_video_cfg->p_next;
  442.         if( p_sys->p_video_cfg->psz_name )
  443.             free( p_sys->p_video_cfg->psz_name );
  444.         if( p_sys->p_video_cfg->psz_value )
  445.             free( p_sys->p_video_cfg->psz_value );
  446.         free( p_sys->p_video_cfg );
  447.         p_sys->p_video_cfg = p_next;
  448.     }
  449.     if( p_sys->psz_venc ) free( p_sys->psz_venc );
  450.     while( p_sys->p_spu_cfg != NULL )
  451.     {
  452.         sout_cfg_t *p_next = p_sys->p_spu_cfg->p_next;
  453.         if( p_sys->p_spu_cfg->psz_name )
  454.             free( p_sys->p_spu_cfg->psz_name );
  455.         if( p_sys->p_spu_cfg->psz_value )
  456.             free( p_sys->p_spu_cfg->psz_value );
  457.         free( p_sys->p_spu_cfg );
  458.         p_sys->p_spu_cfg = p_next;
  459.     }
  460.     if( p_sys->psz_senc ) free( p_sys->psz_senc );
  461.     if( p_sys->p_spu ) spu_Destroy( p_sys->p_spu );
  462.     vlc_object_destroy( p_sys );
  463. }
  464. struct sout_stream_id_t
  465. {
  466.     vlc_fourcc_t  b_transcode;
  467.     /* id of the out stream */
  468.     void *id;
  469.     /* Decoder */
  470.     decoder_t       *p_decoder;
  471.     /* Filters */
  472.     filter_t        *pp_filter[10];
  473.     int             i_filter;
  474.     /* Encoder */
  475.     encoder_t       *p_encoder;
  476.     /* Sync */
  477.     date_t          interpolated_pts;
  478. };
  479. static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
  480. {
  481.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  482.     sout_stream_id_t *id;
  483.     id = malloc( sizeof( sout_stream_id_t ) );
  484.     memset( id, 0, sizeof(sout_stream_id_t) );
  485.     id->id = NULL;
  486.     id->p_decoder = NULL;
  487.     id->p_encoder = NULL;
  488.     /* Create decoder object */
  489.     id->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
  490.     if( !id->p_decoder )
  491.     {
  492.         msg_Err( p_stream, "out of memory" );
  493.         goto error;
  494.     }
  495.     vlc_object_attach( id->p_decoder, p_stream );
  496.     id->p_decoder->p_module = NULL;
  497.     id->p_decoder->fmt_in = *p_fmt;
  498.     id->p_decoder->fmt_out = *p_fmt;
  499.     id->p_decoder->fmt_out.i_extra = 0;
  500.     id->p_decoder->fmt_out.p_extra = 0;
  501.     id->p_decoder->b_pace_control = VLC_TRUE;
  502.     /* Create encoder object */
  503.     id->p_encoder = vlc_object_create( p_stream, VLC_OBJECT_ENCODER );
  504.     if( !id->p_encoder )
  505.     {
  506.         msg_Err( p_stream, "out of memory" );
  507.         goto error;
  508.     }
  509.     vlc_object_attach( id->p_encoder, p_stream );
  510.     id->p_encoder->p_module = NULL;
  511.     /* Create destination format */
  512.     es_format_Init( &id->p_encoder->fmt_out, p_fmt->i_cat, 0 );
  513.     id->p_encoder->fmt_out.i_id    = p_fmt->i_id;
  514.     id->p_encoder->fmt_out.i_group = p_fmt->i_group;
  515.     if( p_fmt->psz_language )
  516.         id->p_encoder->fmt_out.psz_language = strdup( p_fmt->psz_language );
  517.     if( p_fmt->i_cat == AUDIO_ES && (p_sys->i_acodec || p_sys->psz_aenc) )
  518.     {
  519.         msg_Dbg( p_stream,
  520.                  "creating audio transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
  521.                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_acodec );
  522.         /* Complete destination format */
  523.         id->p_encoder->fmt_out.i_codec = p_sys->i_acodec;
  524.         id->p_encoder->fmt_out.audio.i_rate = p_sys->i_sample_rate > 0 ?
  525.             p_sys->i_sample_rate : (int)p_fmt->audio.i_rate;
  526.         id->p_encoder->fmt_out.audio.i_channels = p_sys->i_channels > 0 ?
  527.             p_sys->i_channels : p_fmt->audio.i_channels;
  528.         id->p_encoder->fmt_out.i_bitrate = p_sys->i_abitrate;
  529.         id->p_encoder->fmt_out.audio.i_bitspersample =
  530.             p_fmt->audio.i_bitspersample;
  531.         /* Build decoder -> filter -> encoder chain */
  532.         if( transcode_audio_new( p_stream, id ) )
  533.         {
  534.             msg_Err( p_stream, "cannot create audio chain" );
  535.             goto error;
  536.         }
  537.         /* Open output stream */
  538.         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
  539.         id->b_transcode = VLC_TRUE;
  540.         if( !id->id ) goto error;
  541.         date_Init( &id->interpolated_pts, p_fmt->audio.i_rate, 1 );
  542.     }
  543.     else if( p_fmt->i_cat == VIDEO_ES &&
  544.              (p_sys->i_vcodec != 0 || p_sys->psz_venc) )
  545.     {
  546.         msg_Dbg( p_stream,
  547.                  "creating video transcoding from fcc=`%4.4s' to fcc=`%4.4s'",
  548.                  (char*)&p_fmt->i_codec, (char*)&p_sys->i_vcodec );
  549.         /* Complete destination format */
  550.         id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
  551.         id->p_encoder->fmt_out.video.i_width  = p_sys->i_width;
  552.         id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
  553.         id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
  554.         /* Build decoder -> filter -> encoder chain */
  555.         if( transcode_video_new( p_stream, id ) )
  556.         {
  557.             msg_Err( p_stream, "cannot create video chain" );
  558.             goto error;
  559.         }
  560.         /* Stream will be added later on because we don't know
  561.          * all the characteristics of the decoded stream yet */
  562.         id->b_transcode = VLC_TRUE;
  563.         if( p_sys->f_fps > 0 )
  564.         {
  565.             id->p_encoder->fmt_out.video.i_frame_rate = p_sys->f_fps * 1000;
  566.             id->p_encoder->fmt_out.video.i_frame_rate_base = 1000;
  567.         }
  568.     }
  569.     else if( p_fmt->i_cat == SPU_ES && (p_sys->i_scodec || p_sys->psz_senc) )
  570.     {
  571.         msg_Dbg( p_stream, "creating subtitles transcoding from fcc=`%4.4s' "
  572.                  "to fcc=`%4.4s'", (char*)&p_fmt->i_codec,
  573.                  (char*)&p_sys->i_scodec );
  574.         /* Complete destination format */
  575.         id->p_encoder->fmt_out.i_codec = p_sys->i_scodec;
  576.         /* build decoder -> filter -> encoder */
  577.         if( transcode_spu_new( p_stream, id ) )
  578.         {
  579.             msg_Err( p_stream, "cannot create subtitles chain" );
  580.             goto error;
  581.         }
  582.         /* open output stream */
  583.         id->id = p_sys->p_out->pf_add( p_sys->p_out, &id->p_encoder->fmt_out );
  584.         id->b_transcode = VLC_TRUE;
  585.         if( !id->id ) goto error;
  586.     }
  587.     else if( p_fmt->i_cat == SPU_ES && p_sys->b_soverlay )
  588.     {
  589.         msg_Dbg( p_stream, "subtitles (fcc=`%4.4s') overlaying",
  590.                  (char*)&p_fmt->i_codec );
  591.         id->b_transcode = VLC_TRUE;
  592.         /* Build decoder -> filter -> overlaying chain */
  593.         if( transcode_spu_new( p_stream, id ) )
  594.         {
  595.             msg_Err( p_stream, "cannot create subtitles chain" );
  596.             goto error;
  597.         }
  598.     }
  599.     else
  600.     {
  601.         msg_Dbg( p_stream, "not transcoding a stream (fcc=`%4.4s')",
  602.                  (char*)&p_fmt->i_codec );
  603.         id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
  604.         id->b_transcode = VLC_FALSE;
  605.         if( !id->id ) goto error;
  606.     }
  607.     return id;
  608.  error:
  609.     if( id->p_decoder )
  610.     {
  611.         vlc_object_detach( id->p_decoder );
  612.         vlc_object_destroy( id->p_decoder );
  613.     }
  614.     if( id->p_encoder )
  615.     {
  616.         vlc_object_detach( id->p_encoder );
  617.         vlc_object_destroy( id->p_encoder );
  618.     }
  619.     free( id );
  620.     return NULL;
  621. }
  622. static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
  623. {
  624.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  625.     if( id->b_transcode )
  626.     {
  627.         switch( id->p_decoder->fmt_in.i_cat )
  628.         {
  629.         case AUDIO_ES:
  630.             transcode_audio_close( p_stream, id );
  631.             break;
  632.         case VIDEO_ES:
  633.             transcode_video_close( p_stream, id );
  634.             break;
  635.         case SPU_ES:
  636.             transcode_spu_close( p_stream, id );
  637.             break;
  638.         }
  639.     }
  640.     if( id->id ) p_sys->p_out->pf_del( p_sys->p_out, id->id );
  641.     if( id->p_decoder )
  642.     {
  643.         vlc_object_detach( id->p_decoder );
  644.         vlc_object_destroy( id->p_decoder );
  645.     }
  646.     if( id->p_encoder )
  647.     {
  648.         vlc_object_detach( id->p_encoder );
  649.         vlc_object_destroy( id->p_encoder );
  650.     }
  651.     free( id );
  652.     return VLC_SUCCESS;
  653. }
  654. static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
  655.                  block_t *p_buffer )
  656. {
  657.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  658.     block_t *p_out;
  659.     if( !id->b_transcode && id->id )
  660.     {
  661.         if( p_sys->b_master_sync && p_sys->i_master_drift )
  662.         {
  663.             if( p_buffer->i_dts > 0 )
  664.             {
  665.                 p_buffer->i_dts -= p_sys->i_master_drift;
  666.                 if( p_buffer->i_dts < 0 )
  667.                 {
  668.                     block_Release( p_buffer );
  669.                     return VLC_EGENERIC;
  670.                 }
  671.             }
  672.             if( p_buffer->i_pts > 0 )
  673.             {
  674.                 p_buffer->i_pts -= p_sys->i_master_drift;
  675.                 if( p_buffer->i_pts < 0 )
  676.                 {
  677.                     block_Release( p_buffer );
  678.                     return VLC_EGENERIC;
  679.                 }
  680.             }
  681.         }
  682.         return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
  683.     }
  684.     else if( !id->b_transcode )
  685.     {
  686.         block_Release( p_buffer );
  687.         return VLC_EGENERIC;
  688.     }
  689.     switch( id->p_decoder->fmt_in.i_cat )
  690.     {
  691.     case AUDIO_ES:
  692.         transcode_audio_process( p_stream, id, p_buffer, &p_out );
  693.         break;
  694.     case VIDEO_ES:
  695.         if( transcode_video_process( p_stream, id, p_buffer, &p_out )
  696.             != VLC_SUCCESS )
  697.         {
  698.             return VLC_EGENERIC;
  699.         }
  700.         break;
  701.     case SPU_ES:
  702.         if( transcode_spu_process( p_stream, id, p_buffer, &p_out ) !=
  703.             VLC_SUCCESS )
  704.         {
  705.             return VLC_EGENERIC;
  706.         }
  707.         break;
  708.     default:
  709.         block_Release( p_buffer );
  710.         break;
  711.     }
  712.     if( p_out ) return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_out );
  713.     return VLC_SUCCESS;
  714. }
  715. /****************************************************************************
  716.  * decoder reencoder part
  717.  ****************************************************************************/
  718. int audio_BitsPerSample( vlc_fourcc_t i_format )
  719. {
  720.     switch( i_format )
  721.     {
  722.     case VLC_FOURCC('u','8',' ',' '):
  723.     case VLC_FOURCC('s','8',' ',' '):
  724.         return 8;
  725.     case VLC_FOURCC('u','1','6','l'):
  726.     case VLC_FOURCC('s','1','6','l'):
  727.     case VLC_FOURCC('u','1','6','b'):
  728.     case VLC_FOURCC('s','1','6','b'):
  729.         return 16;
  730.     case VLC_FOURCC('u','2','4','l'):
  731.     case VLC_FOURCC('s','2','4','l'):
  732.     case VLC_FOURCC('u','2','4','b'):
  733.     case VLC_FOURCC('s','2','4','b'):
  734.         return 24;
  735.     case VLC_FOURCC('u','3','2','l'):
  736.     case VLC_FOURCC('s','3','2','l'):
  737.     case VLC_FOURCC('u','3','2','b'):
  738.     case VLC_FOURCC('s','3','2','b'):
  739.     case VLC_FOURCC('f','l','3','2'):
  740.     case VLC_FOURCC('f','i','3','2'):
  741.         return 32;
  742.     case VLC_FOURCC('f','l','6','4'):
  743.         return 64;
  744.     }
  745.     return 0;
  746. }
  747. static int transcode_audio_new( sout_stream_t *p_stream,
  748.                                 sout_stream_id_t *id )
  749. {
  750.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  751.     /*
  752.      * Open decoder
  753.      */
  754.     /* Initialization of decoder structures */
  755.     id->p_decoder->pf_decode_audio = 0;
  756.     id->p_decoder->pf_aout_buffer_new = audio_new_buffer;
  757.     id->p_decoder->pf_aout_buffer_del = audio_del_buffer;
  758.     //id->p_decoder->p_cfg = p_sys->p_video_cfg;
  759.     id->p_decoder->p_module =
  760.         module_Need( id->p_decoder, "decoder", "$codec", 0 );
  761.     if( !id->p_decoder->p_module )
  762.     {
  763.         msg_Err( p_stream, "cannot find decoder" );
  764.         return VLC_EGENERIC;
  765.     }
  766.     id->p_decoder->fmt_out.audio.i_bitspersample = 
  767.         audio_BitsPerSample( id->p_decoder->fmt_out.i_codec );
  768.     /*
  769.      * Open encoder
  770.      */
  771.     /* Initialization of encoder format structures */
  772.     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
  773.                     id->p_decoder->fmt_out.i_codec );
  774.     id->p_encoder->fmt_in.audio.i_format = id->p_decoder->fmt_out.i_codec;
  775.     /* Sanity check for audio channels */
  776.     id->p_encoder->fmt_out.audio.i_channels =
  777.         __MIN( id->p_encoder->fmt_out.audio.i_channels,
  778.                id->p_decoder->fmt_out.audio.i_channels );
  779.     if( id->p_decoder->fmt_out.audio.i_channels ==
  780.         id->p_encoder->fmt_out.audio.i_channels )
  781.         id->p_encoder->fmt_out.audio.i_physical_channels =
  782.             id->p_encoder->fmt_out.audio.i_original_channels =
  783.                 id->p_decoder->fmt_out.audio.i_physical_channels;
  784.     else
  785.         id->p_encoder->fmt_out.audio.i_physical_channels =
  786.             id->p_encoder->fmt_out.audio.i_original_channels =
  787.                 pi_channels_maps[id->p_encoder->fmt_out.audio.i_channels];
  788.     /* Initialization of encoder format structures */
  789.     es_format_Init( &id->p_encoder->fmt_in, AUDIO_ES, AOUT_FMT_S16_NE );
  790.     id->p_encoder->fmt_in.audio.i_format = AOUT_FMT_S16_NE;
  791.     id->p_encoder->fmt_in.audio.i_rate = id->p_encoder->fmt_out.audio.i_rate;
  792.     id->p_encoder->fmt_in.audio.i_physical_channels =
  793.         id->p_encoder->fmt_in.audio.i_original_channels =
  794.             id->p_encoder->fmt_out.audio.i_physical_channels;
  795.     id->p_encoder->fmt_in.audio.i_channels =
  796.         id->p_encoder->fmt_out.audio.i_channels;
  797.     id->p_encoder->fmt_in.audio.i_bitspersample =
  798.         audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
  799.     id->p_encoder->p_cfg = p_stream->p_sys->p_audio_cfg;
  800.     id->p_encoder->p_module =
  801.         module_Need( id->p_encoder, "encoder", p_sys->psz_aenc, VLC_TRUE );
  802.     if( !id->p_encoder->p_module )
  803.     {
  804.         msg_Err( p_stream, "cannot find encoder" );
  805.         module_Unneed( id->p_decoder, id->p_decoder->p_module );
  806.         id->p_decoder->p_module = 0;
  807.         return VLC_EGENERIC;
  808.     }
  809.     id->p_encoder->fmt_in.audio.i_format = id->p_encoder->fmt_in.i_codec;
  810.     id->p_encoder->fmt_in.audio.i_bitspersample =
  811.         audio_BitsPerSample( id->p_encoder->fmt_in.i_codec );
  812.     /* Check if we need a filter for chroma conversion or resizing */
  813.     if( id->p_decoder->fmt_out.i_codec !=
  814.         id->p_encoder->fmt_in.i_codec )
  815.     {
  816.         id->pp_filter[0] =
  817.             vlc_object_create( p_stream, VLC_OBJECT_FILTER );
  818.         vlc_object_attach( id->pp_filter[0], p_stream );
  819.         id->pp_filter[0]->pf_audio_buffer_new = __block_New;
  820.         id->pp_filter[0]->fmt_in = id->p_decoder->fmt_out;
  821.         id->pp_filter[0]->fmt_out = id->p_encoder->fmt_in;
  822.         id->pp_filter[0]->p_module =
  823.             module_Need( id->pp_filter[0], "audio filter2", 0, 0 );
  824.         if( id->pp_filter[0]->p_module ) id->i_filter++;
  825.         else
  826.         {
  827.             msg_Dbg( p_stream, "no audio filter found (%4.4s->%4.4s)",
  828.                      (char *)&id->pp_filter[0]->fmt_in,
  829.                      (char *)&id->pp_filter[0]->fmt_out );
  830.             vlc_object_detach( id->pp_filter[0] );
  831.             vlc_object_destroy( id->pp_filter[0] );
  832.             module_Unneed( id->p_decoder, id->p_decoder->p_module );
  833.             id->p_decoder->p_module = 0;
  834.             module_Unneed( id->p_encoder, id->p_encoder->p_module );
  835.             id->p_encoder->p_module = 0;
  836.             return VLC_EGENERIC;
  837.         }
  838.         id->pp_filter[0]->fmt_out.audio.i_bitspersample = 
  839.             audio_BitsPerSample( id->pp_filter[0]->fmt_out.i_codec );
  840.         /* Try a 2 stage conversion */
  841.         if( id->pp_filter[0]->fmt_out.i_codec !=
  842.             id->p_encoder->fmt_in.i_codec )
  843.         {
  844.             id->pp_filter[1] =
  845.                 vlc_object_create( p_stream, VLC_OBJECT_FILTER );
  846.             vlc_object_attach( id->pp_filter[1], p_stream );
  847.             id->pp_filter[1]->pf_audio_buffer_new = __block_New;
  848.             id->pp_filter[1]->fmt_in = id->pp_filter[0]->fmt_out;
  849.             id->pp_filter[1]->fmt_out = id->p_encoder->fmt_in;
  850.             id->pp_filter[1]->p_module =
  851.               module_Need( id->pp_filter[1], "audio filter2", 0, 0 );
  852.             if( !id->pp_filter[1]->p_module ||
  853.                 id->pp_filter[1]->fmt_out.i_codec !=
  854.                   id->p_encoder->fmt_in.i_codec )
  855.             {
  856.                 msg_Dbg( p_stream, "no audio filter found (%4.4s->%4.4s)",
  857.                          (char *)&id->pp_filter[1]->fmt_in,
  858.                          (char *)&id->pp_filter[1]->fmt_out );
  859.                 module_Unneed( id->pp_filter[0], id->pp_filter[0]->p_module );
  860.                 vlc_object_detach( id->pp_filter[0] );
  861.                 vlc_object_destroy( id->pp_filter[0] );
  862.                 if( id->pp_filter[1]->p_module )
  863.                 module_Unneed( id->pp_filter[0], id->pp_filter[0]->p_module );
  864.                 vlc_object_detach( id->pp_filter[1] );
  865.                 vlc_object_destroy( id->pp_filter[1] );
  866.                 module_Unneed( id->p_decoder, id->p_decoder->p_module );
  867.                 id->p_decoder->p_module = 0;
  868.                 module_Unneed( id->p_encoder, id->p_encoder->p_module );
  869.                 id->p_encoder->p_module = 0;
  870.                 return VLC_EGENERIC;
  871.             }
  872.             else id->i_filter++;
  873.         }
  874.     }
  875.     /* FIXME: Hack for mp3 transcoding support */
  876.     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC( 'm','p','3',' ' ) )
  877.         id->p_encoder->fmt_out.i_codec = VLC_FOURCC( 'm','p','g','a' );
  878.     return VLC_SUCCESS;
  879. }
  880. static void transcode_audio_close( sout_stream_t *p_stream,
  881.                                    sout_stream_id_t *id )
  882. {
  883.     int i;
  884.     /* Close decoder */
  885.     if( id->p_decoder->p_module )
  886.         module_Unneed( id->p_decoder, id->p_decoder->p_module );
  887.     /* Close encoder */
  888.     if( id->p_encoder->p_module )
  889.         module_Unneed( id->p_encoder, id->p_encoder->p_module );
  890.     /* Close filters */
  891.     for( i = 0; i < id->i_filter; i++ )
  892.     {
  893.         vlc_object_detach( id->pp_filter[i] );
  894.         if( id->pp_filter[i]->p_module )
  895.             module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
  896.         vlc_object_destroy( id->pp_filter[i] );
  897.     }
  898. }
  899. static int transcode_audio_process( sout_stream_t *p_stream,
  900.                                     sout_stream_id_t *id,
  901.                                     block_t *in, block_t **out )
  902. {
  903.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  904.     aout_buffer_t *p_audio_buf;
  905.     block_t *p_block, *p_audio_block;
  906.     int i;
  907.     *out = NULL;
  908.     while( (p_audio_buf = id->p_decoder->pf_decode_audio( id->p_decoder,
  909.                                                           &in )) )
  910.     {
  911.         if( p_sys->b_master_sync )
  912.         {
  913.             mtime_t i_dts = date_Get( &id->interpolated_pts ) + 1;
  914.             p_sys->i_master_drift = p_audio_buf->start_date - i_dts;
  915.             date_Increment( &id->interpolated_pts, p_audio_buf->i_nb_samples );
  916.             p_audio_buf->start_date -= p_sys->i_master_drift;
  917.             p_audio_buf->end_date -= p_sys->i_master_drift;
  918.         }
  919.         p_audio_block = p_audio_buf->p_sys;
  920.         p_audio_block->i_buffer = p_audio_buf->i_nb_bytes;
  921.         p_audio_block->i_dts = p_audio_block->i_pts =
  922.             p_audio_buf->start_date;
  923.         p_audio_block->i_length = p_audio_buf->end_date -
  924.             p_audio_buf->start_date;
  925.         p_audio_block->i_samples = p_audio_buf->i_nb_samples;
  926.         /* Run filter chain */
  927.         for( i = 0; i < id->i_filter; i++ )
  928.         {
  929.             p_audio_block =
  930.                 id->pp_filter[i]->pf_audio_filter( id->pp_filter[i],
  931.                                                    p_audio_block );
  932.         }
  933.         p_audio_buf->p_buffer = p_audio_block->p_buffer;
  934.         p_audio_buf->i_nb_bytes = p_audio_block->i_buffer;
  935.         p_audio_buf->i_nb_samples = p_audio_block->i_samples;
  936.         p_audio_buf->start_date = p_audio_block->i_dts;
  937.         p_audio_buf->end_date = p_audio_block->i_dts + p_audio_block->i_length;
  938.         p_block = id->p_encoder->pf_encode_audio( id->p_encoder, p_audio_buf );
  939.         block_ChainAppend( out, p_block );
  940.         block_Release( p_audio_block );
  941.         free( p_audio_buf );
  942.     }
  943.     return VLC_SUCCESS;
  944. }
  945. static void audio_release_buffer( aout_buffer_t *p_buffer )
  946. {
  947.     if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
  948.     if( p_buffer ) free( p_buffer );
  949. }
  950. static aout_buffer_t *audio_new_buffer( decoder_t *p_dec, int i_samples )
  951. {
  952.     aout_buffer_t *p_buffer;
  953.     block_t *p_block;
  954.     int i_size;
  955.     if( p_dec->fmt_out.audio.i_bitspersample )
  956.     {
  957.         i_size = i_samples * p_dec->fmt_out.audio.i_bitspersample / 8 *
  958.             p_dec->fmt_out.audio.i_channels;
  959.     }
  960.     else if( p_dec->fmt_out.audio.i_bytes_per_frame &&
  961.              p_dec->fmt_out.audio.i_frame_length )
  962.     {
  963.         i_size = i_samples * p_dec->fmt_out.audio.i_bytes_per_frame /
  964.             p_dec->fmt_out.audio.i_frame_length;
  965.     }
  966.     else
  967.     {
  968.         i_size = i_samples * 4 * p_dec->fmt_out.audio.i_channels;
  969.     }
  970.     p_buffer = malloc( sizeof(aout_buffer_t) );
  971.     p_buffer->pf_release = audio_release_buffer;
  972.     p_buffer->p_sys = p_block = block_New( p_dec, i_size );
  973.     p_buffer->p_buffer = p_block->p_buffer;
  974.     p_buffer->i_size = p_buffer->i_nb_bytes = p_block->i_buffer;
  975.     p_buffer->i_nb_samples = i_samples;
  976.     p_block->i_samples = i_samples;
  977.     return p_buffer;
  978. }
  979. static void audio_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
  980. {
  981.     if( p_buffer && p_buffer->p_sys ) block_Release( p_buffer->p_sys );
  982.     if( p_buffer ) free( p_buffer );
  983. }
  984. /*
  985.  * video
  986.  */
  987. static int transcode_video_new( sout_stream_t *p_stream, sout_stream_id_t *id )
  988. {
  989.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  990.     int i;
  991.     /*
  992.      * Open decoder
  993.      */
  994.     /* Initialization of decoder structures */
  995.     id->p_decoder->pf_decode_video = 0;
  996.     id->p_decoder->pf_vout_buffer_new = video_new_buffer_decoder;
  997.     id->p_decoder->pf_vout_buffer_del = video_del_buffer_decoder;
  998.     id->p_decoder->pf_picture_link    = video_link_picture_decoder;
  999.     id->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
  1000.     id->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
  1001.     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1002.         id->p_decoder->p_owner->pp_pics[i] = 0;
  1003.     //id->p_decoder->p_cfg = p_sys->p_video_cfg;
  1004.     id->p_decoder->p_module =
  1005.         module_Need( id->p_decoder, "decoder", "$codec", 0 );
  1006.     if( !id->p_decoder->p_module )
  1007.     {
  1008.         msg_Err( p_stream, "cannot find decoder" );
  1009.         return VLC_EGENERIC;
  1010.     }
  1011.     /*
  1012.      * Open encoder.
  1013.      * Because some info about the decoded input will only be available
  1014.      * once the first frame is decoded, we actually only test the availability
  1015.      * of the encoder here.
  1016.      */
  1017.     /* Initialization of encoder format structures */
  1018.     es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
  1019.                     id->p_decoder->fmt_out.i_codec );
  1020.     id->p_encoder->fmt_in.video.i_chroma = id->p_decoder->fmt_out.i_codec;
  1021.     /* The dimensions will be set properly later on.
  1022.      * Just put sensible values so we can test an encoder is available. */
  1023.     id->p_encoder->fmt_in.video.i_width =
  1024.         id->p_encoder->fmt_out.video.i_width ?
  1025.         id->p_encoder->fmt_out.video.i_width :
  1026.         id->p_decoder->fmt_in.video.i_width ?
  1027.         id->p_decoder->fmt_in.video.i_width : 16;
  1028.     id->p_encoder->fmt_in.video.i_height =
  1029.         id->p_encoder->fmt_out.video.i_height ?
  1030.         id->p_encoder->fmt_out.video.i_height :
  1031.         id->p_decoder->fmt_in.video.i_height ?
  1032.         id->p_decoder->fmt_in.video.i_height : 16;
  1033.     id->p_encoder->fmt_in.video.i_frame_rate = 25;
  1034.     id->p_encoder->fmt_in.video.i_frame_rate_base = 1;
  1035.     id->p_encoder->i_threads = p_sys->i_threads;
  1036.     id->p_encoder->p_cfg = p_sys->p_video_cfg;
  1037.     id->p_encoder->p_module =
  1038.         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
  1039.     if( !id->p_encoder->p_module )
  1040.     {
  1041.         msg_Err( p_stream, "cannot find encoder" );
  1042.         module_Unneed( id->p_decoder, id->p_decoder->p_module );
  1043.         id->p_decoder->p_module = 0;
  1044.         return VLC_EGENERIC;
  1045.     }
  1046.     /* Close the encoder.
  1047.      * We'll open it only when we have the first frame. */
  1048.     module_Unneed( id->p_encoder, id->p_encoder->p_module );
  1049.     id->p_encoder->p_module = NULL;
  1050.     if( p_sys->i_threads >= 1 )
  1051.     {
  1052.         p_sys->id_video = id;
  1053.         vlc_mutex_init( p_stream, &p_sys->lock_out );
  1054.         vlc_cond_init( p_stream, &p_sys->cond );
  1055.         memset( p_sys->pp_pics, 0, sizeof(p_sys->pp_pics) );
  1056.         p_sys->i_first_pic = 0;
  1057.         p_sys->i_last_pic = 0;
  1058.         p_sys->p_buffers = NULL;
  1059.         p_sys->b_die = p_sys->b_error = 0;
  1060.         if( vlc_thread_create( p_sys, "encoder", EncoderThread,
  1061.                                VLC_THREAD_PRIORITY_VIDEO, VLC_FALSE ) )
  1062.         {
  1063.             msg_Err( p_stream, "cannot spawn encoder thread" );
  1064.             module_Unneed( id->p_decoder, id->p_decoder->p_module );
  1065.             id->p_decoder->p_module = 0;
  1066.             return VLC_EGENERIC;
  1067.         }
  1068.     }
  1069.     date_Set( &id->interpolated_pts, 0 );
  1070.     return VLC_SUCCESS;
  1071. }
  1072. static int transcode_video_encoder_open( sout_stream_t *p_stream,
  1073.                                          sout_stream_id_t *id )
  1074. {
  1075.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1076.     /* Hack because of the copy packetizer which can fail to detect the
  1077.      * proper size (which forces us to wait until the 1st frame
  1078.      * is decoded) */
  1079.     int i_width = id->p_decoder->fmt_out.video.i_width -
  1080.         p_sys->i_crop_left - p_sys->i_crop_right;
  1081.     int i_height = id->p_decoder->fmt_out.video.i_height -
  1082.         p_sys->i_crop_top - p_sys->i_crop_bottom;
  1083.     if( id->p_encoder->fmt_out.video.i_width <= 0 &&
  1084.         id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
  1085.     {
  1086.         /* Apply the scaling */
  1087.         id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
  1088.         id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
  1089.     }
  1090.     else if( id->p_encoder->fmt_out.video.i_width > 0 &&
  1091.              id->p_encoder->fmt_out.video.i_height <= 0 )
  1092.     {
  1093.         id->p_encoder->fmt_out.video.i_height =
  1094.             id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
  1095.     }
  1096.     else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
  1097.              id->p_encoder->fmt_out.video.i_height > 0 )
  1098.     {
  1099.         id->p_encoder->fmt_out.video.i_width =
  1100.             id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
  1101.     }
  1102.     /* Make sure the size is at least a multiple of 2 */
  1103.     id->p_encoder->fmt_out.video.i_width =
  1104.         (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
  1105.     id->p_encoder->fmt_out.video.i_height =
  1106.         (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
  1107.     id->p_encoder->fmt_in.video.i_width =
  1108.         id->p_encoder->fmt_out.video.i_width;
  1109.     id->p_encoder->fmt_in.video.i_height =
  1110.         id->p_encoder->fmt_out.video.i_height;
  1111.     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
  1112.         !id->p_encoder->fmt_out.video.i_frame_rate_base )
  1113.     {
  1114.         if( id->p_decoder->fmt_out.video.i_frame_rate &&
  1115.             id->p_decoder->fmt_out.video.i_frame_rate_base )
  1116.         {
  1117.             id->p_encoder->fmt_out.video.i_frame_rate =
  1118.                 id->p_decoder->fmt_out.video.i_frame_rate;
  1119.             id->p_encoder->fmt_out.video.i_frame_rate_base =
  1120.                 id->p_decoder->fmt_out.video.i_frame_rate_base;
  1121.         }
  1122.         else
  1123.         {
  1124.             /* Pick a sensible default value */
  1125.             id->p_encoder->fmt_out.video.i_frame_rate = 25;
  1126.             id->p_encoder->fmt_out.video.i_frame_rate_base = 1;
  1127.         }
  1128.     }
  1129.     id->p_encoder->fmt_in.video.i_frame_rate =
  1130.         id->p_encoder->fmt_out.video.i_frame_rate;
  1131.     id->p_encoder->fmt_in.video.i_frame_rate_base =
  1132.         id->p_encoder->fmt_out.video.i_frame_rate_base;
  1133.     date_Init( &id->interpolated_pts,
  1134.                id->p_encoder->fmt_out.video.i_frame_rate,
  1135.                id->p_encoder->fmt_out.video.i_frame_rate_base );
  1136.     /* Check whether a particular aspect ratio was requested */
  1137.     if( !id->p_encoder->fmt_out.video.i_aspect )
  1138.     {
  1139.         id->p_encoder->fmt_out.video.i_aspect =
  1140.             id->p_decoder->fmt_out.video.i_aspect;
  1141.     }
  1142.     id->p_encoder->fmt_in.video.i_aspect =
  1143.         id->p_encoder->fmt_out.video.i_aspect;
  1144.     id->p_encoder->p_module =
  1145.         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
  1146.     if( !id->p_encoder->p_module )
  1147.     {
  1148.         msg_Err( p_stream, "cannot find encoder" );
  1149.         return VLC_EGENERIC;
  1150.     }
  1151.     id->p_encoder->fmt_in.video.i_chroma = id->p_encoder->fmt_in.i_codec;
  1152.     /* Hack for mp2v/mp1v transcoding support */
  1153.     if( id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','1','v') ||
  1154.         id->p_encoder->fmt_out.i_codec == VLC_FOURCC('m','p','2','v') )
  1155.     {
  1156.         id->p_encoder->fmt_out.i_codec = VLC_FOURCC('m','p','g','v');
  1157.     }
  1158.     id->id = p_stream->p_sys->p_out->pf_add( p_stream->p_sys->p_out,
  1159.                                              &id->p_encoder->fmt_out );
  1160.     if( !id->id )
  1161.     {
  1162.         msg_Err( p_stream, "cannot add this stream" );
  1163.         return VLC_EGENERIC;
  1164.     }
  1165.     return VLC_SUCCESS;
  1166. }
  1167. static void transcode_video_close( sout_stream_t *p_stream,
  1168.                                    sout_stream_id_t *id )
  1169. {
  1170.     int i, j;
  1171.     if( p_stream->p_sys->i_threads >= 1 )
  1172.     {
  1173.         vlc_mutex_lock( &p_stream->p_sys->lock_out );
  1174.         p_stream->p_sys->b_die = 1;
  1175.         vlc_cond_signal( &p_stream->p_sys->cond );
  1176.         vlc_mutex_unlock( &p_stream->p_sys->lock_out );
  1177.         vlc_thread_join( p_stream->p_sys );
  1178.         vlc_mutex_destroy( &p_stream->p_sys->lock_out );
  1179.         vlc_cond_destroy( &p_stream->p_sys->cond );
  1180.     }
  1181.     /* Close decoder */
  1182.     if( id->p_decoder->p_module )
  1183.         module_Unneed( id->p_decoder, id->p_decoder->p_module );
  1184.     if( id->p_decoder->p_owner )
  1185.     {
  1186.         /* Clean-up pictures ring buffer */
  1187.         for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1188.         {
  1189.             if( id->p_decoder->p_owner->pp_pics[i] )
  1190.                 video_del_buffer( VLC_OBJECT(id->p_decoder),
  1191.                                   id->p_decoder->p_owner->pp_pics[i] );
  1192.         }
  1193.         free( id->p_decoder->p_owner );
  1194.     }
  1195.     /* Close encoder */
  1196.     if( id->p_encoder->p_module )
  1197.         module_Unneed( id->p_encoder, id->p_encoder->p_module );
  1198.     /* Close filters */
  1199.     for( i = 0; i < id->i_filter; i++ )
  1200.     {
  1201.         vlc_object_detach( id->pp_filter[i] );
  1202.         if( id->pp_filter[i]->p_module )
  1203.             module_Unneed( id->pp_filter[i], id->pp_filter[i]->p_module );
  1204.         /* Clean-up pictures ring buffer */
  1205.         for( j = 0; j < PICTURE_RING_SIZE; j++ )
  1206.         {
  1207.             if( id->pp_filter[i]->p_owner->pp_pics[j] )
  1208.                 video_del_buffer( VLC_OBJECT(id->pp_filter[i]),
  1209.                                   id->pp_filter[i]->p_owner->pp_pics[j] );
  1210.         }
  1211.         free( id->pp_filter[i]->p_owner );
  1212.         vlc_object_destroy( id->pp_filter[i] );
  1213.     }
  1214. }
  1215. static int transcode_video_process( sout_stream_t *p_stream,
  1216.                                     sout_stream_id_t *id,
  1217.                                     block_t *in, block_t **out )
  1218. {
  1219.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1220.     int i_duplicate = 1, i;
  1221.     picture_t *p_pic;
  1222.     *out = NULL;
  1223.     while( (p_pic = id->p_decoder->pf_decode_video( id->p_decoder, &in )) )
  1224.     {
  1225.         subpicture_t *p_subpic = 0;
  1226.         if( p_sys->b_master_sync )
  1227.         {
  1228.             mtime_t i_video_drift;
  1229.             mtime_t i_master_drift = p_sys->i_master_drift;
  1230.             mtime_t i_pts;
  1231.             if( !i_master_drift )
  1232.             {
  1233.                 /* No audio track ? */
  1234.                 p_sys->i_master_drift = i_master_drift = p_pic->date;
  1235.             }
  1236.             i_pts = date_Get( &id->interpolated_pts ) + 1;
  1237.             i_video_drift = p_pic->date - i_pts;
  1238.             i_duplicate = 1;
  1239.             /* Set the pts of the frame being encoded */
  1240.             p_pic->date = i_pts;
  1241.             if( i_video_drift < i_master_drift - 50000 )
  1242.             {
  1243. #if 0
  1244.                 msg_Dbg( p_stream, "dropping frame (%i)",
  1245.                          (int)(i_video_drift - i_master_drift) );
  1246. #endif
  1247.                 p_pic->pf_release( p_pic );
  1248.                 return VLC_EGENERIC;
  1249.             }
  1250.             else if( i_video_drift > i_master_drift + 50000 )
  1251.             {
  1252. #if 0
  1253.                 msg_Dbg( p_stream, "adding frame (%i)",
  1254.                          (int)(i_video_drift - i_master_drift) );
  1255. #endif
  1256.                 i_duplicate = 2;
  1257.             }
  1258.         }
  1259.         if( !id->p_encoder->p_module )
  1260.         {
  1261.             if( transcode_video_encoder_open( p_stream, id ) != VLC_SUCCESS )
  1262.             {
  1263.                 transcode_video_close( p_stream, id );
  1264.                 id->b_transcode = VLC_FALSE;
  1265.                 p_pic->pf_release( p_pic );
  1266.                 return VLC_EGENERIC;
  1267.             }
  1268.             /* Deinterlace */
  1269.             if( p_stream->p_sys->b_deinterlace )
  1270.             {
  1271.                 id->pp_filter[id->i_filter] =
  1272.                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
  1273.                 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
  1274.                 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
  1275.                     video_new_buffer_filter;
  1276.                 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
  1277.                     video_del_buffer_filter;
  1278.                 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
  1279.                 id->pp_filter[id->i_filter]->fmt_out = id->p_decoder->fmt_out;
  1280.                 id->pp_filter[id->i_filter]->p_module =
  1281.                     module_Need( id->pp_filter[id->i_filter],
  1282.                                  "video filter2", "deinterlace", 0 );
  1283.                 if( id->pp_filter[id->i_filter]->p_module )
  1284.                 {
  1285.                     id->pp_filter[id->i_filter]->p_owner =
  1286.                         malloc( sizeof(filter_owner_sys_t) );
  1287.                     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1288.                         id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
  1289.                     id->i_filter++;
  1290.                 }
  1291.                 else
  1292.                 {
  1293.                     msg_Dbg( p_stream, "no video filter found" );
  1294.                     vlc_object_detach( id->pp_filter[id->i_filter] );
  1295.                     vlc_object_destroy( id->pp_filter[id->i_filter] );
  1296.                 }
  1297.             }
  1298.             /* Check if we need a filter for chroma conversion or resizing */
  1299.             if( id->p_decoder->fmt_out.video.i_chroma !=
  1300.                 id->p_encoder->fmt_in.video.i_chroma ||
  1301.                 id->p_decoder->fmt_out.video.i_width !=
  1302.                 id->p_encoder->fmt_out.video.i_width ||
  1303.                 id->p_decoder->fmt_out.video.i_height !=
  1304.                 id->p_encoder->fmt_out.video.i_height ||
  1305.                 p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
  1306.                 p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
  1307.             {
  1308.                 id->pp_filter[id->i_filter] =
  1309.                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
  1310.                 vlc_object_attach( id->pp_filter[id->i_filter], p_stream );
  1311.                 id->pp_filter[id->i_filter]->pf_vout_buffer_new =
  1312.                     video_new_buffer_filter;
  1313.                 id->pp_filter[id->i_filter]->pf_vout_buffer_del =
  1314.                     video_del_buffer_filter;
  1315.                 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
  1316.                 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
  1317.                 id->pp_filter[id->i_filter]->p_module =
  1318.                     module_Need( id->pp_filter[id->i_filter],
  1319.                                  "video filter2", 0, 0 );
  1320.                 if( id->pp_filter[id->i_filter]->p_module )
  1321.                 {
  1322.                     id->pp_filter[id->i_filter]->p_owner =
  1323.                         malloc( sizeof(filter_owner_sys_t) );
  1324.                     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1325.                         id->pp_filter[id->i_filter]->p_owner->pp_pics[i] = 0;
  1326.                     id->i_filter++;
  1327.                 }
  1328.                 else
  1329.                 {
  1330.                     msg_Dbg( p_stream, "no video filter found" );
  1331.                     vlc_object_detach( id->pp_filter[id->i_filter] );
  1332.                     vlc_object_destroy( id->pp_filter[id->i_filter] );
  1333.                     transcode_video_close( p_stream, id );
  1334.                     id->b_transcode = VLC_FALSE;
  1335.                     p_pic->pf_release( p_pic );
  1336.                     return VLC_EGENERIC;
  1337.                 }
  1338.             }
  1339.         }
  1340.         /* Run filter chain */
  1341.         for( i = 0; i < id->i_filter; i++ )
  1342.         {
  1343.             p_pic = id->pp_filter[i]->pf_video_filter(id->pp_filter[i], p_pic);
  1344.         }
  1345.         /*
  1346.          * Encoding
  1347.          */
  1348.         /* Check if we have a subpicture to overlay */
  1349.         if( p_sys->p_spu )
  1350.         {
  1351.             p_subpic = spu_SortSubpictures( p_sys->p_spu, p_pic->date );
  1352.             /* TODO: get another pic */
  1353.         }
  1354.         /* Overlay subpicture */
  1355.         if( p_subpic )
  1356.         {
  1357.             int i_scale_width, i_scale_height;
  1358.             video_format_t *p_fmt;
  1359.             i_scale_width = id->p_encoder->fmt_in.video.i_width * 1000 /
  1360.                 id->p_decoder->fmt_out.video.i_width;
  1361.             i_scale_height = id->p_encoder->fmt_in.video.i_height * 1000 /
  1362.                 id->p_decoder->fmt_out.video.i_height;
  1363.             if( p_pic->i_refcount && !id->i_filter )
  1364.             {
  1365.                 /* We can't modify the picture, we need to duplicate it */
  1366.                 picture_t *p_tmp = video_new_buffer_decoder( id->p_decoder );
  1367.                 if( p_tmp )
  1368.                 {
  1369.                     vout_CopyPicture( p_stream, p_tmp, p_pic );
  1370.                     p_pic->pf_release( p_pic );
  1371.                     p_pic = p_tmp;
  1372.                 }
  1373.             }
  1374.             if( id->i_filter )
  1375.                 p_fmt = &id->pp_filter[id->i_filter -1]->fmt_out.video;
  1376.             else
  1377.                 p_fmt = &id->p_decoder->fmt_out.video;
  1378.             /* FIXME (shouldn't have to be done here) */
  1379.             p_fmt->i_sar_num = p_fmt->i_aspect *
  1380.                 p_fmt->i_height / p_fmt->i_width;
  1381.             p_fmt->i_sar_den = VOUT_ASPECT_FACTOR;
  1382.             spu_RenderSubpictures( p_sys->p_spu, p_fmt, p_pic, p_pic, p_subpic,
  1383.                                    i_scale_width, i_scale_height );
  1384.         }
  1385.         if( p_sys->i_threads >= 1 )
  1386.         {
  1387.             vlc_mutex_lock( &p_sys->lock_out );
  1388.             p_sys->pp_pics[p_sys->i_last_pic++] = p_pic;
  1389.             p_sys->i_last_pic %= PICTURE_RING_SIZE;
  1390.             *out = p_sys->p_buffers;
  1391.             p_sys->p_buffers = NULL;
  1392.             vlc_cond_signal( &p_sys->cond );
  1393.             vlc_mutex_unlock( &p_sys->lock_out );
  1394.         }
  1395.         else
  1396.         {
  1397.             block_t *p_block;
  1398.             p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
  1399.             block_ChainAppend( out, p_block );
  1400.             if( p_sys->b_master_sync )
  1401.                 date_Increment( &id->interpolated_pts, 1 );
  1402.             if( p_sys->b_master_sync && i_duplicate > 1 )
  1403.             {
  1404.                 mtime_t i_pts = date_Get( &id->interpolated_pts ) + 1;
  1405.                 date_Increment( &id->interpolated_pts, 1 );
  1406.                 p_pic->date = i_pts;
  1407.                 p_block = id->p_encoder->pf_encode_video(id->p_encoder, p_pic);
  1408.                 block_ChainAppend( out, p_block );
  1409.             }
  1410.             p_pic->pf_release( p_pic );
  1411.         }
  1412.     }
  1413.     return VLC_SUCCESS;
  1414. }
  1415. static int EncoderThread( sout_stream_sys_t *p_sys )
  1416. {
  1417.     sout_stream_id_t *id = p_sys->id_video;
  1418.     picture_t *p_pic;
  1419.     int i_plane;
  1420.     while( !p_sys->b_die && !p_sys->b_error )
  1421.     {
  1422.         block_t *p_block;
  1423.         vlc_mutex_lock( &p_sys->lock_out );
  1424.         while( p_sys->i_last_pic == p_sys->i_first_pic )
  1425.         {
  1426.             vlc_cond_wait( &p_sys->cond, &p_sys->lock_out );
  1427.             if( p_sys->b_die || p_sys->b_error ) break;
  1428.         }
  1429.         if( p_sys->b_die || p_sys->b_error )
  1430.         {
  1431.             vlc_mutex_unlock( &p_sys->lock_out );
  1432.             break;
  1433.         }
  1434.         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
  1435.         p_sys->i_first_pic %= PICTURE_RING_SIZE;
  1436.         vlc_mutex_unlock( &p_sys->lock_out );
  1437.         p_block = id->p_encoder->pf_encode_video( id->p_encoder, p_pic );
  1438.         vlc_mutex_lock( &p_sys->lock_out );
  1439.         block_ChainAppend( &p_sys->p_buffers, p_block );
  1440.         vlc_mutex_unlock( &p_sys->lock_out );
  1441.         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
  1442.         {
  1443.             free( p_pic->p[i_plane].p_pixels );
  1444.         }
  1445.         free( p_pic );
  1446.     }
  1447.     while( p_sys->i_last_pic != p_sys->i_first_pic )
  1448.     {
  1449.         p_pic = p_sys->pp_pics[p_sys->i_first_pic++];
  1450.         p_sys->i_first_pic %= PICTURE_RING_SIZE;
  1451.         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
  1452.         {
  1453.             free( p_pic->p[i_plane].p_pixels );
  1454.         }
  1455.         free( p_pic );
  1456.     }
  1457.     block_ChainRelease( p_sys->p_buffers );
  1458.     return 0;
  1459. }
  1460. struct picture_sys_t
  1461. {
  1462.     vlc_object_t *p_owner;
  1463. };
  1464. static void video_release_buffer( picture_t *p_pic )
  1465. {
  1466.     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
  1467.     {
  1468.         video_del_buffer_decoder( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
  1469.     }
  1470.     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
  1471. }
  1472. static picture_t *video_new_buffer( vlc_object_t *p_this, picture_t **pp_ring )
  1473. {
  1474.     decoder_t *p_dec = (decoder_t *)p_this;
  1475.     picture_t *p_pic;
  1476.     int i;
  1477.     /* Find an empty space in the picture ring buffer */
  1478.     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1479.     {
  1480.         if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
  1481.         {
  1482.             pp_ring[i]->i_status = RESERVED_PICTURE;
  1483.             return pp_ring[i];
  1484.         }
  1485.     }
  1486.     for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1487.     {
  1488.         if( pp_ring[i] == 0 ) break;
  1489.     }
  1490.     if( i == PICTURE_RING_SIZE )
  1491.     {
  1492.         msg_Err( p_this, "decoder/filter is leaking pictures, "
  1493.                  "resetting its ring buffer" );
  1494.         for( i = 0; i < PICTURE_RING_SIZE; i++ )
  1495.         {
  1496.             pp_ring[i]->pf_release( pp_ring[i] );
  1497.         }
  1498.         i = 0;
  1499.     }
  1500.     p_pic = malloc( sizeof(picture_t) );
  1501.     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
  1502.     vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
  1503.                           p_dec->fmt_out.video.i_chroma,
  1504.                           p_dec->fmt_out.video.i_width,
  1505.                           p_dec->fmt_out.video.i_height,
  1506.                           p_dec->fmt_out.video.i_aspect );
  1507.     if( !p_pic->i_planes )
  1508.     {
  1509.         free( p_pic );
  1510.         return 0;
  1511.     }
  1512.     p_pic->pf_release = video_release_buffer;
  1513.     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
  1514.     p_pic->p_sys->p_owner = p_this;
  1515.     p_pic->i_status = RESERVED_PICTURE;
  1516.     pp_ring[i] = p_pic;
  1517.     return p_pic;
  1518. }
  1519. static picture_t *video_new_buffer_decoder( decoder_t *p_dec )
  1520. {
  1521.     return video_new_buffer( VLC_OBJECT(p_dec),
  1522.                              p_dec->p_owner->pp_pics );
  1523. }
  1524. static picture_t *video_new_buffer_filter( filter_t *p_filter )
  1525. {
  1526.     return video_new_buffer( VLC_OBJECT(p_filter),
  1527.                              p_filter->p_owner->pp_pics );
  1528. }
  1529. static void video_del_buffer( vlc_object_t *p_this, picture_t *p_pic )
  1530. {
  1531.     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
  1532.     if( p_pic && p_pic->p_sys ) free( p_pic->p_sys );
  1533.     if( p_pic ) free( p_pic );
  1534. }
  1535. static void video_del_buffer_decoder( decoder_t *p_decoder, picture_t *p_pic )
  1536. {
  1537.     p_pic->i_refcount = 0;
  1538.     p_pic->i_status = DESTROYED_PICTURE;
  1539. }
  1540. static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
  1541. {
  1542.     p_pic->i_refcount = 0;
  1543.     p_pic->i_status = DESTROYED_PICTURE;
  1544. }
  1545. static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
  1546. {
  1547.     p_pic->i_refcount++;
  1548. }
  1549. static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
  1550. {
  1551.     video_release_buffer( p_pic );
  1552. }
  1553. /*
  1554.  * SPU
  1555.  */
  1556. static subpicture_t *spu_new_buffer( decoder_t * );
  1557. static void spu_del_buffer( decoder_t *, subpicture_t * );
  1558. static int transcode_spu_new( sout_stream_t *p_stream, sout_stream_id_t *id )
  1559. {
  1560.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1561.     /*
  1562.      * Open decoder
  1563.      */
  1564.     /* Initialization of decoder structures */
  1565.     id->p_decoder->pf_spu_buffer_new = spu_new_buffer;
  1566.     id->p_decoder->pf_spu_buffer_del = spu_del_buffer;
  1567.     id->p_decoder->p_owner = (decoder_owner_sys_t *)p_stream;
  1568.     //id->p_decoder->p_cfg = p_sys->p_spu_cfg;
  1569.     id->p_decoder->p_module =
  1570.         module_Need( id->p_decoder, "decoder", "$codec", 0 );
  1571.     if( !id->p_decoder->p_module )
  1572.     {
  1573.         msg_Err( p_stream, "cannot find decoder" );
  1574.         return VLC_EGENERIC;
  1575.     }
  1576.     if( !p_sys->b_soverlay )
  1577.     {
  1578.         /*
  1579.          * Open encoder
  1580.          */
  1581.         /* Initialization of encoder format structures */
  1582.         es_format_Init( &id->p_encoder->fmt_in, id->p_decoder->fmt_in.i_cat,
  1583.                         id->p_decoder->fmt_in.i_codec );
  1584.         id->p_encoder->p_cfg = p_sys->p_spu_cfg;
  1585.         id->p_encoder->p_module =
  1586.             module_Need( id->p_encoder, "encoder", p_sys->psz_senc, VLC_TRUE );
  1587.         if( !id->p_encoder->p_module )
  1588.         {
  1589.             module_Unneed( id->p_decoder, id->p_decoder->p_module );
  1590.             msg_Err( p_stream, "cannot find encoder" );
  1591.             return VLC_EGENERIC;
  1592.         }
  1593.     }
  1594.     if( !p_sys->p_spu )
  1595.     {
  1596.         p_sys->p_spu = spu_Create( p_stream );
  1597.         spu_Init( p_sys->p_spu );
  1598.     }
  1599.     return VLC_SUCCESS;
  1600. }
  1601. static void transcode_spu_close( sout_stream_t *p_stream, sout_stream_id_t *id)
  1602. {
  1603.     /* Close decoder */
  1604.     if( id->p_decoder->p_module )
  1605.         module_Unneed( id->p_decoder, id->p_decoder->p_module );
  1606.     /* Close encoder */
  1607.     if( id->p_encoder->p_module )
  1608.         module_Unneed( id->p_encoder, id->p_encoder->p_module );
  1609. }
  1610. static int transcode_spu_process( sout_stream_t *p_stream,
  1611.                                   sout_stream_id_t *id,
  1612.                                   block_t *in, block_t **out )
  1613. {
  1614.     sout_stream_sys_t *p_sys = p_stream->p_sys;
  1615.     subpicture_t *p_subpic;
  1616.     *out = NULL;
  1617.     p_subpic = id->p_decoder->pf_decode_sub( id->p_decoder, &in );
  1618.     if( !p_subpic ) return VLC_EGENERIC;
  1619.     if( p_sys->b_master_sync && p_sys->i_master_drift )
  1620.     {
  1621.         p_subpic->i_start -= p_sys->i_master_drift;
  1622.         if( p_subpic->i_stop ) p_subpic->i_stop -= p_sys->i_master_drift;
  1623.     }
  1624.     if( p_sys->b_soverlay )
  1625.     {
  1626.         spu_DisplaySubpicture( p_sys->p_spu, p_subpic );
  1627.     }
  1628.     else
  1629.     {
  1630.         block_t *p_block;
  1631.         p_block = id->p_encoder->pf_encode_sub( id->p_encoder, p_subpic );
  1632.         spu_del_buffer( id->p_decoder, p_subpic );
  1633.         if( p_block )
  1634.         {
  1635.             block_ChainAppend( out, p_block );
  1636.             return VLC_SUCCESS;
  1637.         }
  1638.     }
  1639.     return VLC_EGENERIC;
  1640. }
  1641. static subpicture_t *spu_new_buffer( decoder_t *p_dec )
  1642. {
  1643.     sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
  1644.     return spu_CreateSubpicture( p_stream->p_sys->p_spu );
  1645. }
  1646. static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
  1647. {
  1648.     sout_stream_t *p_stream = (sout_stream_t *)p_dec->p_owner;
  1649.     spu_DestroySubpicture( p_stream->p_sys->p_spu, p_subpic );
  1650. }