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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * decoder.c: Functions for the management of decoders
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 the VideoLAN team
  5.  * $Id: c0e9fb5744bc21f8acab17d6bbbbbece890b019f $
  6.  *
  7.  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  8.  *          Gildas Bazin <gbazin@videolan.org>
  9.  *          Laurent Aimar <fenrir@via.ecp.fr>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24.  *****************************************************************************/
  25. /*****************************************************************************
  26.  * Preamble
  27.  *****************************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. # include "config.h"
  30. #endif
  31. #include <assert.h>
  32. #include <vlc_common.h>
  33. #include <vlc_block.h>
  34. #include <vlc_vout.h>
  35. #include <vlc_aout.h>
  36. #include <vlc_sout.h>
  37. #include <vlc_codec.h>
  38. #include <vlc_osd.h>
  39. #include <vlc_meta.h>
  40. #include <vlc_dialog.h>
  41. #include "audio_output/aout_internal.h"
  42. #include "stream_output/stream_output.h"
  43. #include "input_internal.h"
  44. #include "clock.h"
  45. #include "decoder.h"
  46. #include "event.h"
  47. #include "resource.h"
  48. #include "../video_output/vout_control.h"
  49. static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
  50. static void       DeleteDecoder( decoder_t * );
  51. static void      *DecoderThread( vlc_object_t * );
  52. static void       DecoderProcess( decoder_t *, block_t * );
  53. static void       DecoderError( decoder_t *p_dec, block_t *p_block );
  54. static void       DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
  55. static void       DecoderFlush( decoder_t * );
  56. static void       DecoderSignalBuffering( decoder_t *, bool );
  57. static void       DecoderFlushBuffering( decoder_t * );
  58. static void       DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
  59. /* Buffers allocation callbacks for the decoders */
  60. static aout_buffer_t *aout_new_buffer( decoder_t *, int );
  61. static void aout_del_buffer( decoder_t *, aout_buffer_t * );
  62. static picture_t *vout_new_buffer( decoder_t * );
  63. static void vout_del_buffer( decoder_t *, picture_t * );
  64. static void vout_link_picture( decoder_t *, picture_t * );
  65. static void vout_unlink_picture( decoder_t *, picture_t * );
  66. static subpicture_t *spu_new_buffer( decoder_t * );
  67. static void spu_del_buffer( decoder_t *, subpicture_t * );
  68. struct decoder_owner_sys_t
  69. {
  70.     int64_t         i_preroll_end;
  71.     input_thread_t  *p_input;
  72.     input_clock_t   *p_clock;
  73.     int             i_last_rate;
  74.     vout_thread_t   *p_spu_vout;
  75.     int              i_spu_channel;
  76.     int64_t          i_spu_order;
  77.     sout_instance_t         *p_sout;
  78.     sout_packetizer_input_t *p_sout_input;
  79.     /* Some decoders require already packetized data (ie. not truncated) */
  80.     decoder_t *p_packetizer;
  81.     /* Current format in use by the output */
  82.     video_format_t video;
  83.     audio_format_t audio;
  84.     es_format_t    sout;
  85.     /* */
  86.     bool           b_fmt_description;
  87.     es_format_t    fmt_description;
  88.     vlc_meta_t     *p_description;
  89.     /* fifo */
  90.     block_fifo_t *p_fifo;
  91.     /* Lock for communication with decoder thread */
  92.     vlc_mutex_t lock;
  93.     vlc_cond_t  wait_request;
  94.     vlc_cond_t  wait_acknowledge;
  95.     /* -- These variables need locking on write(only) -- */
  96.     aout_instance_t *p_aout;
  97.     aout_input_t    *p_aout_input;
  98.     vout_thread_t   *p_vout;
  99.     /* -- Theses variables need locking on read *and* write -- */
  100.     /* */
  101.     /* Pause */
  102.     bool b_paused;
  103.     struct
  104.     {
  105.         mtime_t i_date;
  106.         int     i_ignore;
  107.     } pause;
  108.     /* Buffering */
  109.     bool b_buffering;
  110.     struct
  111.     {
  112.         bool b_first;
  113.         bool b_full;
  114.         int  i_count;
  115.         picture_t     *p_picture;
  116.         picture_t     **pp_picture_next;
  117.         subpicture_t  *p_subpic;
  118.         subpicture_t  **pp_subpic_next;
  119.         aout_buffer_t *p_audio;
  120.         aout_buffer_t **pp_audio_next;
  121.         block_t       *p_block;
  122.         block_t       **pp_block_next;
  123.     } buffer;
  124.     /* Flushing */
  125.     bool b_flushing;
  126.     /* CC */
  127.     struct
  128.     {
  129.         bool b_supported;
  130.         bool pb_present[4];
  131.         decoder_t *pp_decoder[4];
  132.     } cc;
  133.     /* Delay */
  134.     mtime_t i_ts_delay;
  135. };
  136. #define DECODER_MAX_BUFFERING_COUNT (4)
  137. #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
  138. #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
  139. /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
  140.  * a bogus PTS and won't be displayed */
  141. #define DECODER_BOGUS_VIDEO_DELAY                ((mtime_t)(DEFAULT_PTS_DELAY * 30))
  142. /* */
  143. #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
  144. /*****************************************************************************
  145.  * Public functions
  146.  *****************************************************************************/
  147. picture_t *decoder_NewPicture( decoder_t *p_decoder )
  148. {
  149.     picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
  150.     if( !p_picture )
  151.         msg_Warn( p_decoder, "can't get output picture" );
  152.     return p_picture;
  153. }
  154. void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
  155. {
  156.     p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
  157. }
  158. void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
  159. {
  160.     p_decoder->pf_picture_link( p_decoder, p_picture );
  161. }
  162. void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
  163. {
  164.     p_decoder->pf_picture_unlink( p_decoder, p_picture );
  165. }
  166. aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
  167. {
  168.     if( !p_decoder->pf_aout_buffer_new )
  169.         return NULL;
  170.     return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
  171. }
  172. void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
  173. {
  174.     p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
  175. }
  176. subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
  177. {
  178.     subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
  179.     if( !p_subpicture )
  180.         msg_Warn( p_decoder, "can't get output subpicture" );
  181.     return p_subpicture;
  182. }
  183. void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
  184. {
  185.     p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
  186. }
  187. /* decoder_GetInputAttachments:
  188.  */
  189. int decoder_GetInputAttachments( decoder_t *p_dec,
  190.                                  input_attachment_t ***ppp_attachment,
  191.                                  int *pi_attachment )
  192. {
  193.     if( !p_dec->pf_get_attachments )
  194.         return VLC_EGENERIC;
  195.     return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
  196. }
  197. /* decoder_GetDisplayDate:
  198.  */
  199. mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
  200. {
  201.     if( !p_dec->pf_get_display_date )
  202.         return VLC_TS_INVALID;
  203.     return p_dec->pf_get_display_date( p_dec, i_ts );
  204. }
  205. /* decoder_GetDisplayRate:
  206.  */
  207. int decoder_GetDisplayRate( decoder_t *p_dec )
  208. {
  209.     if( !p_dec->pf_get_display_rate )
  210.         return INPUT_RATE_DEFAULT;
  211.     return p_dec->pf_get_display_rate( p_dec );
  212. }
  213. /**
  214.  * Spawns a new decoder thread
  215.  *
  216.  * param p_input the input thread
  217.  * param p_es the es descriptor
  218.  * return the spawned decoder object
  219.  */
  220. decoder_t *input_DecoderNew( input_thread_t *p_input,
  221.                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
  222. {
  223.     decoder_t *p_dec = NULL;
  224.     int i_priority;
  225. #ifdef ENABLE_SOUT
  226.     /* If we are in sout mode, search for packetizer module */
  227.     if( p_sout )
  228.     {
  229.         /* Create the decoder configuration structure */
  230.         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
  231.         if( p_dec == NULL )
  232.         {
  233.             msg_Err( p_input, "could not create packetizer" );
  234.             dialog_Fatal( p_input, _("Streaming / Transcoding failed"), "%s",
  235.                           _("VLC could not open the packetizer module.") );
  236.             return NULL;
  237.         }
  238.     }
  239.     else
  240. #endif
  241.     {
  242.         /* Create the decoder configuration structure */
  243.         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
  244.         if( p_dec == NULL )
  245.         {
  246.             msg_Err( p_input, "could not create decoder" );
  247.             dialog_Fatal( p_input, _("Streaming / Transcoding failed"), "%s",
  248.                           _("VLC could not open the decoder module.") );
  249.             return NULL;
  250.         }
  251.     }
  252.     if( !p_dec->p_module )
  253.     {
  254.         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
  255.         DeleteDecoder( p_dec );
  256.         vlc_object_release( p_dec );
  257.         return NULL;
  258.     }
  259.     p_dec->p_owner->p_clock = p_clock;
  260.     assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
  261.     if( p_dec->fmt_out.i_cat == AUDIO_ES )
  262.         i_priority = VLC_THREAD_PRIORITY_AUDIO;
  263.     else
  264.         i_priority = VLC_THREAD_PRIORITY_VIDEO;
  265.     /* Spawn the decoder thread */
  266.     if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
  267.     {
  268.         msg_Err( p_dec, "cannot spawn decoder thread" );
  269.         module_unneed( p_dec, p_dec->p_module );
  270.         DeleteDecoder( p_dec );
  271.         vlc_object_release( p_dec );
  272.         return NULL;
  273.     }
  274.     return p_dec;
  275. }
  276. /**
  277.  * Kills a decoder thread and waits until it's finished
  278.  *
  279.  * param p_input the input thread
  280.  * param p_es the es descriptor
  281.  * return nothing
  282.  */
  283. void input_DecoderDelete( decoder_t *p_dec )
  284. {
  285.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  286.     vlc_object_kill( p_dec );
  287.     /* Make sure we aren't paused/buffering/waiting anymore */
  288.     vlc_mutex_lock( &p_owner->lock );
  289.     const bool b_was_paused = p_owner->b_paused;
  290.     p_owner->b_paused = false;
  291.     p_owner->b_buffering = false;
  292.     p_owner->b_flushing = true;
  293.     vlc_cond_signal( &p_owner->wait_request );
  294.     vlc_mutex_unlock( &p_owner->lock );
  295.     vlc_thread_join( p_dec );
  296.     p_owner->b_paused = b_was_paused;
  297.     module_unneed( p_dec, p_dec->p_module );
  298.     /* */
  299.     if( p_dec->p_owner->cc.b_supported )
  300.     {
  301.         int i;
  302.         for( i = 0; i < 4; i++ )
  303.             input_DecoderSetCcState( p_dec, false, i );
  304.     }
  305.     /* Delete decoder configuration */
  306.     DeleteDecoder( p_dec );
  307.     /* Delete the decoder */
  308.     vlc_object_release( p_dec );
  309. }
  310. /**
  311.  * Put a block_t in the decoder's fifo.
  312.  * Thread-safe w.r.t. the decoder. May be a cancellation point.
  313.  *
  314.  * param p_dec the decoder object
  315.  * param p_block the data block
  316.  */
  317. void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
  318. {
  319.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  320.     if( b_do_pace )
  321.     {
  322.         /* The fifo is not consummed when buffering and so will
  323.          * deadlock vlc.
  324.          * There is no need to lock as b_buffering is never modify
  325.          * inside decoder thread. */
  326.         if( !p_owner->b_buffering )
  327.             block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
  328.     }
  329. #ifdef __arm__
  330.     else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
  331. #else
  332.     else if( block_FifoSize( p_owner->p_fifo ) > 400000000 /* 400 MB, ie ~ 50mb/s for 60s */ )
  333. #endif
  334.     {
  335.         /* FIXME: ideally we would check the time amount of data
  336.          * in the FIFO instead of its size. */
  337.         msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
  338.                   "consumed quickly enough), resetting fifo!" );
  339.         block_FifoEmpty( p_owner->p_fifo );
  340.     }
  341.     block_FifoPut( p_owner->p_fifo, p_block );
  342. }
  343. bool input_DecoderIsEmpty( decoder_t * p_dec )
  344. {
  345.     assert( !p_dec->p_owner->b_buffering );
  346.     /* FIXME that's not really true */
  347.     return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
  348. }
  349. void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
  350. {
  351.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  352.     int i;
  353.     vlc_mutex_lock( &p_owner->lock );
  354.     for( i = 0; i < 4; i++ )
  355.         pb_present[i] =  p_owner->cc.pb_present[i];
  356.     vlc_mutex_unlock( &p_owner->lock );
  357. }
  358. int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
  359. {
  360.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  361.     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
  362.     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
  363.         return VLC_EGENERIC;
  364.     if( b_decode )
  365.     {
  366.         static const vlc_fourcc_t fcc[4] = {
  367.             VLC_FOURCC('c', 'c', '1', ' '),
  368.             VLC_FOURCC('c', 'c', '2', ' '),
  369.             VLC_FOURCC('c', 'c', '3', ' '),
  370.             VLC_FOURCC('c', 'c', '4', ' '),
  371.         };
  372.         decoder_t *p_cc;
  373.         es_format_t fmt;
  374.         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
  375.         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
  376.         if( !p_cc )
  377.         {
  378.             msg_Err( p_dec, "could not create decoder" );
  379.             dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
  380.                           _("VLC could not open the decoder module.") );
  381.             return VLC_EGENERIC;
  382.         }
  383.         else if( !p_cc->p_module )
  384.         {
  385.             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
  386.             DeleteDecoder( p_cc );
  387.             vlc_object_release( p_cc );
  388.             return VLC_EGENERIC;
  389.         }
  390.         p_cc->p_owner->p_clock = p_owner->p_clock;
  391.         vlc_mutex_lock( &p_owner->lock );
  392.         p_owner->cc.pp_decoder[i_channel] = p_cc;
  393.         vlc_mutex_unlock( &p_owner->lock );
  394.     }
  395.     else
  396.     {
  397.         decoder_t *p_cc;
  398.         vlc_mutex_lock( &p_owner->lock );
  399.         p_cc = p_owner->cc.pp_decoder[i_channel];
  400.         p_owner->cc.pp_decoder[i_channel] = NULL;
  401.         vlc_mutex_unlock( &p_owner->lock );
  402.         if( p_cc )
  403.         {
  404.             vlc_object_kill( p_cc );
  405.             module_unneed( p_cc, p_cc->p_module );
  406.             DeleteDecoder( p_cc );
  407.             vlc_object_release( p_cc );
  408.         }
  409.     }
  410.     return VLC_SUCCESS;
  411. }
  412. int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
  413. {
  414.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  415.     *pb_decode = false;
  416.     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
  417.         return VLC_EGENERIC;
  418.     vlc_mutex_lock( &p_owner->lock );
  419.     *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
  420.     vlc_mutex_unlock( &p_owner->lock );
  421.     return VLC_EGENERIC;
  422. }
  423. void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
  424. {
  425.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  426.     vlc_mutex_lock( &p_owner->lock );
  427.     assert( !p_owner->b_paused || !b_paused );
  428.     p_owner->b_paused = b_paused;
  429.     p_owner->pause.i_date = i_date;
  430.     p_owner->pause.i_ignore = 0;
  431.     vlc_cond_signal( &p_owner->wait_request );
  432.     DecoderOutputChangePause( p_dec, b_paused, i_date );
  433.     vlc_mutex_unlock( &p_owner->lock );
  434. }
  435. void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
  436. {
  437.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  438.     vlc_mutex_lock( &p_owner->lock );
  439.     p_owner->i_ts_delay = i_delay;
  440.     vlc_mutex_unlock( &p_owner->lock );
  441. }
  442. void input_DecoderStartBuffering( decoder_t *p_dec )
  443. {
  444.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  445.     vlc_mutex_lock( &p_owner->lock );
  446.     DecoderFlush( p_dec );
  447.     p_owner->buffer.b_first = true;
  448.     p_owner->buffer.b_full = false;
  449.     p_owner->buffer.i_count = 0;
  450.     assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
  451.             !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
  452.     p_owner->buffer.p_picture = NULL;
  453.     p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
  454.     p_owner->buffer.p_subpic = NULL;
  455.     p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
  456.     p_owner->buffer.p_audio = NULL;
  457.     p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
  458.     p_owner->buffer.p_block = NULL;
  459.     p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
  460.     p_owner->b_buffering = true;
  461.     vlc_cond_signal( &p_owner->wait_request );
  462.     vlc_mutex_unlock( &p_owner->lock );
  463. }
  464. void input_DecoderStopBuffering( decoder_t *p_dec )
  465. {
  466.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  467.     vlc_mutex_lock( &p_owner->lock );
  468.     p_owner->b_buffering = false;
  469.     vlc_cond_signal( &p_owner->wait_request );
  470.     vlc_mutex_unlock( &p_owner->lock );
  471. }
  472. void input_DecoderWaitBuffering( decoder_t *p_dec )
  473. {
  474.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  475.     vlc_mutex_lock( &p_owner->lock );
  476.     while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
  477.     {
  478.         block_FifoWake( p_owner->p_fifo );
  479.         vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
  480.     }
  481.     vlc_mutex_unlock( &p_owner->lock );
  482. }
  483. void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
  484. {
  485.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  486.     *pi_duration = 0;
  487.     vlc_mutex_lock( &p_owner->lock );
  488.     if( p_dec->fmt_out.i_cat == VIDEO_ES )
  489.     {
  490.         if( p_owner->b_paused && p_owner->p_vout )
  491.         {
  492.             vout_NextPicture( p_owner->p_vout, pi_duration );
  493.             p_owner->pause.i_ignore++;
  494.             vlc_cond_signal( &p_owner->wait_request );
  495.         }
  496.     }
  497.     else
  498.     {
  499.         /* TODO subtitle should not be flushed */
  500.         DecoderFlush( p_dec );
  501.     }
  502.     vlc_mutex_unlock( &p_owner->lock );
  503. }
  504. bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
  505. {
  506.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  507.     bool b_changed;
  508.     vlc_mutex_lock( &p_owner->lock );
  509.     b_changed = p_owner->b_fmt_description;
  510.     if( b_changed )
  511.     {
  512.         if( p_fmt )
  513.             es_format_Copy( p_fmt, &p_owner->fmt_description );
  514.         if( pp_meta )
  515.         {
  516.             *pp_meta = NULL;
  517.             if( p_owner->p_description )
  518.             {
  519.                 *pp_meta = vlc_meta_New();
  520.                 if( *pp_meta )
  521.                     vlc_meta_Merge( *pp_meta, p_owner->p_description );
  522.             }
  523.         }
  524.         p_owner->b_fmt_description = false;
  525.     }
  526.     vlc_mutex_unlock( &p_owner->lock );
  527.     return b_changed;
  528. }
  529. size_t input_DecoderGetFifoSize( decoder_t *p_dec )
  530. {
  531.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  532.     return block_FifoSize( p_owner->p_fifo );
  533. }
  534. /*****************************************************************************
  535.  * Internal functions
  536.  *****************************************************************************/
  537. static int DecoderGetInputAttachments( decoder_t *p_dec,
  538.                                        input_attachment_t ***ppp_attachment,
  539.                                        int *pi_attachment )
  540. {
  541.     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
  542.                           ppp_attachment, pi_attachment );
  543. }
  544. static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
  545. {
  546.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  547.     vlc_mutex_lock( &p_owner->lock );
  548.     if( p_owner->b_buffering || p_owner->b_paused )
  549.         i_ts = VLC_TS_INVALID;
  550.     vlc_mutex_unlock( &p_owner->lock );
  551.     if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
  552.         return i_ts;
  553.     if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
  554.         return VLC_TS_INVALID;
  555.     return i_ts;
  556. }
  557. static int DecoderGetDisplayRate( decoder_t *p_dec )
  558. {
  559.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  560.     if( !p_owner->p_clock )
  561.         return INPUT_RATE_DEFAULT;
  562.     return input_clock_GetRate( p_owner->p_clock );
  563. }
  564. /* */
  565. static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
  566. {
  567.     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.n"
  568.              "VLC probably does not support this sound or video format.",
  569.              (char*)&codec );
  570.     dialog_Fatal( p_dec, _("No suitable decoder module"),
  571.                  _("VLC does not support the audio or video format "%4.4s". "
  572.                   "Unfortunately there is no way for you to fix this."),
  573.                   (char*)&codec );
  574. }
  575. /**
  576.  * Create a decoder object
  577.  *
  578.  * param p_input the input thread
  579.  * param p_es the es descriptor
  580.  * param i_object_type Object type as define in include/vlc_objects.h
  581.  * return the decoder object
  582.  */
  583. static decoder_t * CreateDecoder( input_thread_t *p_input,
  584.                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
  585. {
  586.     decoder_t *p_dec;
  587.     decoder_owner_sys_t *p_owner;
  588.     es_format_t null_es_format;
  589.     int i;
  590.     p_dec = vlc_object_create( p_input, i_object_type );
  591.     if( p_dec == NULL )
  592.         return NULL;
  593.     p_dec->pf_decode_audio = NULL;
  594.     p_dec->pf_decode_video = NULL;
  595.     p_dec->pf_decode_sub = NULL;
  596.     p_dec->pf_get_cc = NULL;
  597.     p_dec->pf_packetize = NULL;
  598.     /* Initialize the decoder */
  599.     p_dec->p_module = NULL;
  600.     memset( &null_es_format, 0, sizeof(es_format_t) );
  601.     es_format_Copy( &p_dec->fmt_in, fmt );
  602.     es_format_Copy( &p_dec->fmt_out, &null_es_format );
  603.     p_dec->p_description = NULL;
  604.     /* Allocate our private structure for the decoder */
  605.     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
  606.     if( p_dec->p_owner == NULL )
  607.     {
  608.         vlc_object_release( p_dec );
  609.         return NULL;
  610.     }
  611.     p_dec->p_owner->i_preroll_end = VLC_TS_INVALID;
  612.     p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
  613.     p_dec->p_owner->p_input = p_input;
  614.     p_dec->p_owner->p_aout = NULL;
  615.     p_dec->p_owner->p_aout_input = NULL;
  616.     p_dec->p_owner->p_vout = NULL;
  617.     p_dec->p_owner->p_spu_vout = NULL;
  618.     p_dec->p_owner->i_spu_channel = 0;
  619.     p_dec->p_owner->i_spu_order = 0;
  620.     p_dec->p_owner->p_sout = p_sout;
  621.     p_dec->p_owner->p_sout_input = NULL;
  622.     p_dec->p_owner->p_packetizer = NULL;
  623.     /* decoder fifo */
  624.     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
  625.     {
  626.         free( p_dec->p_owner );
  627.         vlc_object_release( p_dec );
  628.         return NULL;
  629.     }
  630.     /* Set buffers allocation callbacks for the decoders */
  631.     p_dec->pf_aout_buffer_new = aout_new_buffer;
  632.     p_dec->pf_aout_buffer_del = aout_del_buffer;
  633.     p_dec->pf_vout_buffer_new = vout_new_buffer;
  634.     p_dec->pf_vout_buffer_del = vout_del_buffer;
  635.     p_dec->pf_picture_link    = vout_link_picture;
  636.     p_dec->pf_picture_unlink  = vout_unlink_picture;
  637.     p_dec->pf_spu_buffer_new  = spu_new_buffer;
  638.     p_dec->pf_spu_buffer_del  = spu_del_buffer;
  639.     /* */
  640.     p_dec->pf_get_attachments  = DecoderGetInputAttachments;
  641.     p_dec->pf_get_display_date = DecoderGetDisplayDate;
  642.     p_dec->pf_get_display_rate = DecoderGetDisplayRate;
  643.     vlc_object_attach( p_dec, p_input );
  644.     /* Find a suitable decoder/packetizer module */
  645.     if( i_object_type == VLC_OBJECT_DECODER )
  646.         p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
  647.     else
  648.         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
  649.     /* Check if decoder requires already packetized data */
  650.     if( i_object_type == VLC_OBJECT_DECODER &&
  651.         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
  652.     {
  653.         p_dec->p_owner->p_packetizer =
  654.             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
  655.         if( p_dec->p_owner->p_packetizer )
  656.         {
  657.             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
  658.                             &p_dec->fmt_in );
  659.             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
  660.                             &null_es_format );
  661.             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
  662.             p_dec->p_owner->p_packetizer->p_module =
  663.                 module_need( p_dec->p_owner->p_packetizer,
  664.                              "packetizer", "$packetizer", false );
  665.             if( !p_dec->p_owner->p_packetizer->p_module )
  666.             {
  667.                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
  668.                 vlc_object_detach( p_dec->p_owner->p_packetizer );
  669.                 vlc_object_release( p_dec->p_owner->p_packetizer );
  670.             }
  671.         }
  672.     }
  673.     /* Copy ourself the input replay gain */
  674.     if( fmt->i_cat == AUDIO_ES )
  675.     {
  676.         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
  677.         {
  678.             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
  679.             {
  680.                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
  681.                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
  682.             }
  683.             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
  684.             {
  685.                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
  686.                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
  687.             }
  688.         }
  689.     }
  690.     /* */
  691.     vlc_mutex_init( &p_owner->lock );
  692.     vlc_cond_init( &p_owner->wait_request );
  693.     vlc_cond_init( &p_owner->wait_acknowledge );
  694.     p_owner->b_fmt_description = false;
  695.     es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
  696.     p_owner->p_description = NULL;
  697.     p_owner->b_paused = false;
  698.     p_owner->pause.i_date = VLC_TS_INVALID;
  699.     p_owner->pause.i_ignore = 0;
  700.     p_owner->b_buffering = false;
  701.     p_owner->buffer.b_first = true;
  702.     p_owner->buffer.b_full = false;
  703.     p_owner->buffer.i_count = 0;
  704.     p_owner->buffer.p_picture = NULL;
  705.     p_owner->buffer.p_subpic = NULL;
  706.     p_owner->buffer.p_audio = NULL;
  707.     p_owner->buffer.p_block = NULL;
  708.     p_owner->b_flushing = false;
  709.     /* */
  710.     p_owner->cc.b_supported = false;
  711.     if( i_object_type == VLC_OBJECT_DECODER )
  712.     {
  713.         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
  714.             p_owner->cc.b_supported = true;
  715.         if( p_dec->pf_get_cc )
  716.             p_owner->cc.b_supported = true;
  717.     }
  718.     for( i = 0; i < 4; i++ )
  719.     {
  720.         p_owner->cc.pb_present[i] = false;
  721.         p_owner->cc.pp_decoder[i] = NULL;
  722.     }
  723.     p_owner->i_ts_delay = 0;
  724.     return p_dec;
  725. }
  726. /**
  727.  * The decoding main loop
  728.  *
  729.  * param p_dec the decoder
  730.  */
  731. static void *DecoderThread( vlc_object_t *p_this )
  732. {
  733.     decoder_t *p_dec = (decoder_t *)p_this;
  734.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  735.     /* The decoder's main loop */
  736.     for( ;; )
  737.     {
  738.         block_t *p_block = block_FifoGet( p_owner->p_fifo );
  739.         /* Make sure there is no cancellation point other than this one^^.
  740.          * If you need one, be sure to push cleanup of p_block. */
  741.         DecoderSignalBuffering( p_dec, p_block == NULL );
  742.         if( p_block )
  743.         {
  744.             int canc = vlc_savecancel();
  745.             if( p_dec->b_error )
  746.                 DecoderError( p_dec, p_block );
  747.             else
  748.                 DecoderProcess( p_dec, p_block );
  749.             vlc_restorecancel( canc );
  750.         }
  751.     }
  752.     return NULL;
  753. }
  754. static block_t *DecoderBlockFlushNew()
  755. {
  756.     block_t *p_null = block_Alloc( 128 );
  757.     if( !p_null )
  758.         return NULL;
  759.     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
  760.                        BLOCK_FLAG_CORRUPTED |
  761.                        BLOCK_FLAG_CORE_FLUSH;
  762.     memset( p_null->p_buffer, 0, p_null->i_buffer );
  763.     return p_null;
  764. }
  765. static void DecoderFlush( decoder_t *p_dec )
  766. {
  767.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  768.     vlc_assert_locked( &p_owner->lock );
  769.     /* Empty the fifo */
  770.     block_FifoEmpty( p_owner->p_fifo );
  771.     /* Monitor for flush end */
  772.     p_owner->b_flushing = true;
  773.     vlc_cond_signal( &p_owner->wait_request );
  774.     /* Send a special block */
  775.     block_t *p_null = DecoderBlockFlushNew();
  776.     if( !p_null )
  777.         return;
  778.     input_DecoderDecode( p_dec, p_null, false );
  779.     /* */
  780.     while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
  781.         vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
  782. }
  783. static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
  784. {
  785.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  786.     vlc_mutex_lock( &p_owner->lock );
  787.     if( p_owner->b_buffering )
  788.     {
  789.         if( b_full )
  790.             p_owner->buffer.b_full = true;
  791.         vlc_cond_signal( &p_owner->wait_acknowledge );
  792.     }
  793.     vlc_mutex_unlock( &p_owner->lock );
  794. }
  795. static bool DecoderIsFlushing( decoder_t *p_dec )
  796. {
  797.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  798.     bool b_flushing;
  799.     vlc_mutex_lock( &p_owner->lock );
  800.     b_flushing = p_owner->b_flushing;
  801.     vlc_mutex_unlock( &p_owner->lock );
  802.     return b_flushing;
  803. }
  804. static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
  805. {
  806.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  807.     vlc_assert_locked( &p_owner->lock );
  808.     while( !p_owner->b_flushing )
  809.     {
  810.         if( p_owner->b_paused )
  811.         {
  812.             if( p_owner->b_buffering && !p_owner->buffer.b_full )
  813.                 break;
  814.             if( p_owner->pause.i_ignore > 0 )
  815.             {
  816.                 p_owner->pause.i_ignore--;
  817.                 break;
  818.             }
  819.         }
  820.         else
  821.         {
  822.             if( !p_owner->b_buffering || !p_owner->buffer.b_full )
  823.                 break;
  824.         }
  825.         vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
  826.     }
  827.     if( pb_reject )
  828.         *pb_reject = p_owner->b_flushing;
  829. }
  830. static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
  831. {
  832.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  833.     vlc_assert_locked( &p_owner->lock );
  834.     /* XXX only audio and video output have to be paused.
  835.      * - for sout it is useless
  836.      * - for subs, it is done by the vout
  837.      */
  838.     if( p_dec->fmt_out.i_cat == AUDIO_ES )
  839.     {
  840.         if( p_owner->p_aout && p_owner->p_aout_input )
  841.             aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
  842.                                  b_paused, i_date );
  843.     }
  844.     else if( p_dec->fmt_out.i_cat == VIDEO_ES )
  845.     {
  846.         if( p_owner->p_vout )
  847.             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
  848.     }
  849. }
  850. static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
  851. {
  852.     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
  853.         *pi_preroll = INT64_MAX;
  854.     else if( p->i_dts > VLC_TS_INVALID )
  855.         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
  856.     else if( p->i_pts > VLC_TS_INVALID )
  857.         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
  858. }
  859. static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
  860. {
  861.     mtime_t current_date = mdate();
  862.     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
  863.     if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
  864.     {
  865.         /* ETSI EN 300 472 Annex A : do not take into account the PTS
  866.          * for teletext streams. */
  867.         return current_date + 400000;
  868.     }
  869.     return i_ts;
  870. }
  871. static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
  872.                           mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
  873. {
  874.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  875.     input_clock_t   *p_clock = p_owner->p_clock;
  876.     vlc_assert_locked( &p_owner->lock );
  877.     const mtime_t i_es_delay = p_owner->i_ts_delay;
  878.     if( p_clock )
  879.     {
  880.         const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
  881.         int i_rate;
  882.         if( *pi_ts0 > VLC_TS_INVALID )
  883.         {
  884.             *pi_ts0 += i_es_delay;
  885.             if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
  886.                 *pi_ts1 += i_es_delay;
  887.             if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
  888.                 *pi_ts0 = VLC_TS_INVALID;
  889.         }
  890.         else
  891.         {
  892.             i_rate = input_clock_GetRate( p_clock );
  893.         }
  894.         /* Do not create ephemere data because of rounding errors */
  895.         if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
  896.             *pi_ts1 += 1;
  897.         if( pi_duration )
  898.             *pi_duration = ( *pi_duration * i_rate +
  899.                                     INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
  900.         if( pi_rate )
  901.             *pi_rate = i_rate;
  902.         if( b_telx )
  903.         {
  904.             *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
  905.             if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
  906.                 *pi_ts1 = *pi_ts0;
  907.         }
  908.     }
  909. }
  910. /**
  911.  * If *pb_reject, it does nothing, otherwise it waits for the given
  912.  * deadline or a flush request (in which case it set *pi_reject to true.
  913.  */
  914. static void DecoderWaitDate( decoder_t *p_dec,
  915.                              bool *pb_reject, mtime_t i_deadline )
  916. {
  917.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  918.     if( *pb_reject || i_deadline < 0 )
  919.         return;
  920.     for( ;; )
  921.     {
  922.         vlc_mutex_lock( &p_owner->lock );
  923.         if( p_owner->b_flushing || p_dec->b_die )
  924.         {
  925.             *pb_reject = true;
  926.             vlc_mutex_unlock( &p_owner->lock );
  927.             break;
  928.         }
  929.         int i_ret = vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
  930.                                         i_deadline );
  931.         vlc_mutex_unlock( &p_owner->lock );
  932.         if( i_ret )
  933.             break;
  934.     }
  935. }
  936. static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
  937.                               int *pi_played_sum, int *pi_lost_sum )
  938. {
  939.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  940.     aout_instance_t *p_aout = p_owner->p_aout;
  941.     aout_input_t    *p_aout_input = p_owner->p_aout_input;
  942.     /* */
  943.     if( p_audio->start_date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
  944.     {
  945.         msg_Warn( p_dec, "non-dated audio buffer received" );
  946.         *pi_lost_sum += 1;
  947.         aout_BufferFree( p_audio );
  948.         return;
  949.     }
  950.     /* */
  951.     vlc_mutex_lock( &p_owner->lock );
  952.     if( p_owner->b_buffering || p_owner->buffer.p_audio )
  953.     {
  954.         p_audio->p_next = NULL;
  955.         *p_owner->buffer.pp_audio_next = p_audio;
  956.         p_owner->buffer.pp_audio_next = &p_audio->p_next;
  957.         p_owner->buffer.i_count++;
  958.         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
  959.             p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
  960.         {
  961.             p_owner->buffer.b_full = true;
  962.             vlc_cond_signal( &p_owner->wait_acknowledge );
  963.         }
  964.     }
  965.     for( ;; )
  966.     {
  967.         bool b_has_more = false;
  968.         bool b_reject;
  969.         DecoderWaitUnblock( p_dec, &b_reject );
  970.         if( p_owner->b_buffering )
  971.         {
  972.             vlc_mutex_unlock( &p_owner->lock );
  973.             return;
  974.         }
  975.         /* */
  976.         if( p_owner->buffer.p_audio )
  977.         {
  978.             p_audio = p_owner->buffer.p_audio;
  979.             p_owner->buffer.p_audio = p_audio->p_next;
  980.             p_owner->buffer.i_count--;
  981.             b_has_more = p_owner->buffer.p_audio != NULL;
  982.             if( !b_has_more )
  983.                 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
  984.         }
  985.         /* */
  986.         const bool b_dated = p_audio->start_date > VLC_TS_INVALID;
  987.         int i_rate = INPUT_RATE_DEFAULT;
  988.         DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
  989.                       &i_rate, AOUT_MAX_ADVANCE_TIME, false );
  990.         vlc_mutex_unlock( &p_owner->lock );
  991.         if( !p_aout || !p_aout_input ||
  992.             p_audio->start_date <= VLC_TS_INVALID ||
  993.             i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
  994.             i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
  995.             b_reject = true;
  996.         DecoderWaitDate( p_dec, &b_reject,
  997.                          p_audio->start_date - AOUT_MAX_PREPARE_TIME );
  998.         if( !b_reject )
  999.         {
  1000.             if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
  1001.                 *pi_played_sum += 1;
  1002.             *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
  1003.         }
  1004.         else
  1005.         {
  1006.             if( b_dated )
  1007.                 msg_Warn( p_aout, "received buffer in the future" );
  1008.             else
  1009.                 msg_Warn( p_dec, "non-dated audio buffer received" );
  1010.             *pi_lost_sum += 1;
  1011.             aout_BufferFree( p_audio );
  1012.         }
  1013.         if( !b_has_more )
  1014.             break;
  1015.         vlc_mutex_lock( &p_owner->lock );
  1016.         if( !p_owner->buffer.p_audio )
  1017.         {
  1018.             vlc_mutex_unlock( &p_owner->lock );
  1019.             break;
  1020.         }
  1021.     }
  1022. }
  1023. static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
  1024. {
  1025.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1026.     input_thread_t  *p_input = p_owner->p_input;
  1027.     aout_buffer_t   *p_aout_buf;
  1028.     int i_decoded = 0;
  1029.     int i_lost = 0;
  1030.     int i_played = 0;
  1031.     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
  1032.     {
  1033.         aout_instance_t *p_aout = p_owner->p_aout;
  1034.         aout_input_t    *p_aout_input = p_owner->p_aout_input;
  1035.         if( p_dec->b_die )
  1036.         {
  1037.             /* It prevent freezing VLC in case of broken decoder */
  1038.             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
  1039.             if( p_block )
  1040.                 block_Release( p_block );
  1041.             break;
  1042.         }
  1043.         i_decoded++;
  1044.         if( p_owner->i_preroll_end > VLC_TS_INVALID &&
  1045.             p_aout_buf->start_date < p_owner->i_preroll_end )
  1046.         {
  1047.             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
  1048.             continue;
  1049.         }
  1050.         if( p_owner->i_preroll_end > VLC_TS_INVALID )
  1051.         {
  1052.             msg_Dbg( p_dec, "End of audio preroll" );
  1053.             if( p_owner->p_aout && p_owner->p_aout_input )
  1054.                 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
  1055.             /* */
  1056.             p_owner->i_preroll_end = VLC_TS_INVALID;
  1057.         }
  1058.         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
  1059.     }
  1060.     /* Update ugly stat */
  1061.     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
  1062.     {
  1063.         vlc_mutex_lock( &p_input->p->counters.counters_lock);
  1064.         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
  1065.                              i_lost, NULL );
  1066.         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
  1067.                              i_played, NULL );
  1068.         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
  1069.                              i_decoded, NULL );
  1070.         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
  1071.     }
  1072. }
  1073. static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
  1074. {
  1075.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1076.     block_t *p_cc;
  1077.     bool pb_present[4];
  1078.     bool b_processed = false;
  1079.     int i;
  1080.     int i_cc_decoder;
  1081.     assert( p_dec_cc->pf_get_cc != NULL );
  1082.     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
  1083.     if( !p_owner->cc.b_supported )
  1084.         return;
  1085.     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
  1086.     if( !p_cc )
  1087.         return;
  1088.     vlc_mutex_lock( &p_owner->lock );
  1089.     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
  1090.     {
  1091.         p_owner->cc.pb_present[i] |= pb_present[i];
  1092.         if( p_owner->cc.pp_decoder[i] )
  1093.             i_cc_decoder++;
  1094.     }
  1095.     for( i = 0; i < 4; i++ )
  1096.     {
  1097.         if( !p_owner->cc.pp_decoder[i] )
  1098.             continue;
  1099.         if( i_cc_decoder > 1 )
  1100.             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
  1101.         else
  1102.             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
  1103.         i_cc_decoder--;
  1104.         b_processed = true;
  1105.     }
  1106.     vlc_mutex_unlock( &p_owner->lock );
  1107.     if( !b_processed )
  1108.         block_Release( p_cc );
  1109. }
  1110. static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
  1111.                               int *pi_played_sum, int *pi_lost_sum )
  1112. {
  1113.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1114.     vout_thread_t  *p_vout = p_owner->p_vout;
  1115.     bool b_first_buffered;
  1116.     if( p_picture->date <= VLC_TS_INVALID )
  1117.     {
  1118.         msg_Warn( p_vout, "non-dated video buffer received" );
  1119.         *pi_lost_sum += 1;
  1120.         vout_DropPicture( p_vout, p_picture );
  1121.         return;
  1122.     }
  1123.     /* */
  1124.     vlc_mutex_lock( &p_owner->lock );
  1125.     if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
  1126.     {
  1127.         p_picture->p_next = NULL;
  1128.         *p_owner->buffer.pp_picture_next = p_picture;
  1129.         p_owner->buffer.pp_picture_next = &p_picture->p_next;
  1130.         p_owner->buffer.i_count++;
  1131.         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
  1132.             p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
  1133.         {
  1134.             p_owner->buffer.b_full = true;
  1135.             vlc_cond_signal( &p_owner->wait_acknowledge );
  1136.         }
  1137.     }
  1138.     b_first_buffered = p_owner->buffer.p_picture != NULL;
  1139.     for( ;; b_first_buffered = false )
  1140.     {
  1141.         bool b_has_more = false;
  1142.         bool b_reject;
  1143.         DecoderWaitUnblock( p_dec, &b_reject );
  1144.         if( p_owner->b_buffering && !p_owner->buffer.b_first )
  1145.         {
  1146.             vlc_mutex_unlock( &p_owner->lock );
  1147.             return;
  1148.         }
  1149.         bool b_buffering_first = p_owner->b_buffering;
  1150.         /* */
  1151.         if( p_owner->buffer.p_picture )
  1152.         {
  1153.             p_picture = p_owner->buffer.p_picture;
  1154.             p_owner->buffer.p_picture = p_picture->p_next;
  1155.             p_owner->buffer.i_count--;
  1156.             b_has_more = p_owner->buffer.p_picture != NULL;
  1157.             if( !b_has_more )
  1158.                 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
  1159.         }
  1160.         /* */
  1161.         if( b_buffering_first )
  1162.         {
  1163.             assert( p_owner->buffer.b_first );
  1164.             assert( !p_owner->buffer.i_count );
  1165.             msg_Dbg( p_dec, "Received first picture" );
  1166.             p_owner->buffer.b_first = false;
  1167.             p_picture->b_force = true;
  1168.         }
  1169.         const bool b_dated = p_picture->date > VLC_TS_INVALID;
  1170.         int i_rate = INPUT_RATE_DEFAULT;
  1171.         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
  1172.                       &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
  1173.         vlc_mutex_unlock( &p_owner->lock );
  1174.         /* */
  1175.         if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
  1176.             b_reject = true;
  1177.         if( !b_reject )
  1178.         {
  1179.             if( i_rate != p_owner->i_last_rate || b_first_buffered )
  1180.             {
  1181.                 /* Be sure to not display old picture after our own */
  1182.                 vout_Flush( p_vout, p_picture->date );
  1183.                 p_owner->i_last_rate = i_rate;
  1184.             }
  1185.             vout_DisplayPicture( p_vout, p_picture );
  1186.         }
  1187.         else
  1188.         {
  1189.             if( b_dated )
  1190.                 msg_Warn( p_vout, "early picture skipped" );
  1191.             else
  1192.                 msg_Warn( p_vout, "non-dated video buffer received" );
  1193.             *pi_lost_sum += 1;
  1194.             vout_DropPicture( p_vout, p_picture );
  1195.         }
  1196.         int i_tmp_display;
  1197.         int i_tmp_lost;
  1198.         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
  1199.         *pi_played_sum += i_tmp_display;
  1200.         *pi_lost_sum += i_tmp_lost;
  1201.         if( !b_has_more || b_buffering_first )
  1202.             break;
  1203.         vlc_mutex_lock( &p_owner->lock );
  1204.         if( !p_owner->buffer.p_picture )
  1205.         {
  1206.             vlc_mutex_unlock( &p_owner->lock );
  1207.             break;
  1208.         }
  1209.     }
  1210. }
  1211. static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
  1212. {
  1213.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1214.     input_thread_t *p_input = p_owner->p_input;
  1215.     picture_t      *p_pic;
  1216.     int i_lost = 0;
  1217.     int i_decoded = 0;
  1218.     int i_displayed = 0;
  1219.     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
  1220.     {
  1221.         vout_thread_t  *p_vout = p_owner->p_vout;
  1222.         if( p_dec->b_die )
  1223.         {
  1224.             /* It prevent freezing VLC in case of broken decoder */
  1225.             vout_DropPicture( p_vout, p_pic );
  1226.             if( p_block )
  1227.                 block_Release( p_block );
  1228.             break;
  1229.         }
  1230.         i_decoded++;
  1231.         if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
  1232.         {
  1233.             vout_DropPicture( p_vout, p_pic );
  1234.             continue;
  1235.         }
  1236.         if( p_owner->i_preroll_end > VLC_TS_INVALID )
  1237.         {
  1238.             msg_Dbg( p_dec, "End of video preroll" );
  1239.             if( p_vout )
  1240.                 vout_Flush( p_vout, VLC_TS_INVALID+1 );
  1241.             /* */
  1242.             p_owner->i_preroll_end = VLC_TS_INVALID;
  1243.         }
  1244.         if( p_dec->pf_get_cc &&
  1245.             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
  1246.             DecoderGetCc( p_dec, p_dec );
  1247.         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
  1248.     }
  1249.     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
  1250.     {
  1251.         vlc_mutex_lock( &p_input->p->counters.counters_lock );
  1252.         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
  1253.                              i_decoded, NULL );
  1254.         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
  1255.                              i_lost , NULL);
  1256.         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
  1257.                              i_displayed, NULL);
  1258.         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
  1259.     }
  1260. }
  1261. static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
  1262.                             bool b_telx )
  1263. {
  1264.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1265.     vout_thread_t *p_vout = p_owner->p_spu_vout;
  1266.     /* */
  1267.     if( p_subpic->i_start <= VLC_TS_INVALID )
  1268.     {
  1269.         msg_Warn( p_dec, "non-dated spu buffer received" );
  1270.         subpicture_Delete( p_subpic );
  1271.         return;
  1272.     }
  1273.     /* */
  1274.     vlc_mutex_lock( &p_owner->lock );
  1275.     if( p_owner->b_buffering || p_owner->buffer.p_subpic )
  1276.     {
  1277.         p_subpic->p_next = NULL;
  1278.         *p_owner->buffer.pp_subpic_next = p_subpic;
  1279.         p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
  1280.         p_owner->buffer.i_count++;
  1281.         /* XXX it is important to be full after the first one */
  1282.         if( p_owner->buffer.i_count > 0 )
  1283.         {
  1284.             p_owner->buffer.b_full = true;
  1285.             vlc_cond_signal( &p_owner->wait_acknowledge );
  1286.         }
  1287.     }
  1288.     for( ;; )
  1289.     {
  1290.         bool b_has_more = false;
  1291.         bool b_reject;
  1292.         DecoderWaitUnblock( p_dec, &b_reject );
  1293.         if( p_owner->b_buffering )
  1294.         {
  1295.             vlc_mutex_unlock( &p_owner->lock );
  1296.             return;
  1297.         }
  1298.         /* */
  1299.         if( p_owner->buffer.p_subpic )
  1300.         {
  1301.             p_subpic = p_owner->buffer.p_subpic;
  1302.             p_owner->buffer.p_subpic = p_subpic->p_next;
  1303.             p_owner->buffer.i_count--;
  1304.             b_has_more = p_owner->buffer.p_subpic != NULL;
  1305.             if( !b_has_more )
  1306.                 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
  1307.         }
  1308.         /* */
  1309.         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
  1310.                       NULL, INT64_MAX, b_telx );
  1311.         vlc_mutex_unlock( &p_owner->lock );
  1312.         if( p_subpic->i_start <= VLC_TS_INVALID )
  1313.             b_reject = true;
  1314.         DecoderWaitDate( p_dec, &b_reject,
  1315.                          p_subpic->i_start - SPU_MAX_PREPARE_TIME );
  1316.         if( !b_reject )
  1317.             spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
  1318.         else
  1319.             subpicture_Delete( p_subpic );
  1320.         if( !b_has_more )
  1321.             break;
  1322.         vlc_mutex_lock( &p_owner->lock );
  1323.         if( !p_owner->buffer.p_subpic )
  1324.         {
  1325.             vlc_mutex_unlock( &p_owner->lock );
  1326.             break;
  1327.         }
  1328.     }
  1329. }
  1330. static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
  1331.                              bool b_telx )
  1332. {
  1333.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1334.     assert( p_owner->p_clock );
  1335.     assert( !p_sout_block->p_next );
  1336.     vlc_mutex_lock( &p_owner->lock );
  1337.     if( p_owner->b_buffering || p_owner->buffer.p_block )
  1338.     {
  1339.         block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
  1340.         p_owner->buffer.i_count++;
  1341.         /* XXX it is important to be full after the first one */
  1342.         if( p_owner->buffer.i_count > 0 )
  1343.         {
  1344.             p_owner->buffer.b_full = true;
  1345.             vlc_cond_signal( &p_owner->wait_acknowledge );
  1346.         }
  1347.     }
  1348.     for( ;; )
  1349.     {
  1350.         bool b_has_more = false;
  1351.         bool b_reject;
  1352.         DecoderWaitUnblock( p_dec, &b_reject );
  1353.         if( p_owner->b_buffering )
  1354.         {
  1355.             vlc_mutex_unlock( &p_owner->lock );
  1356.             return;
  1357.         }
  1358.         /* */
  1359.         if( p_owner->buffer.p_block )
  1360.         {
  1361.             p_sout_block = p_owner->buffer.p_block;
  1362.             p_owner->buffer.p_block = p_sout_block->p_next;
  1363.             p_owner->buffer.i_count--;
  1364.             b_has_more = p_owner->buffer.p_block != NULL;
  1365.             if( !b_has_more )
  1366.                 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
  1367.         }
  1368.         p_sout_block->p_next = NULL;
  1369.         DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
  1370.                       &p_sout_block->i_length,
  1371.                       &p_sout_block->i_rate, INT64_MAX, b_telx );
  1372.         vlc_mutex_unlock( &p_owner->lock );
  1373.         if( !b_reject )
  1374.             sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
  1375.         else
  1376.             block_Release( p_sout_block );
  1377.         if( !b_has_more )
  1378.             break;
  1379.         vlc_mutex_lock( &p_owner->lock );
  1380.         if( !p_owner->buffer.p_block )
  1381.         {
  1382.             vlc_mutex_unlock( &p_owner->lock );
  1383.             break;
  1384.         }
  1385.     }
  1386. }
  1387. /* */
  1388. static void DecoderFlushBuffering( decoder_t *p_dec )
  1389. {
  1390.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1391.     vlc_assert_locked( &p_owner->lock );
  1392.     while( p_owner->buffer.p_picture )
  1393.     {
  1394.         picture_t *p_picture = p_owner->buffer.p_picture;
  1395.         p_owner->buffer.p_picture = p_picture->p_next;
  1396.         p_owner->buffer.i_count--;
  1397.         if( p_owner->p_vout )
  1398.             vout_DropPicture( p_owner->p_vout, p_picture );
  1399.         if( !p_owner->buffer.p_picture )
  1400.             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
  1401.     }
  1402.     while( p_owner->buffer.p_audio )
  1403.     {
  1404.         aout_buffer_t *p_audio = p_owner->buffer.p_audio;
  1405.         p_owner->buffer.p_audio = p_audio->p_next;
  1406.         p_owner->buffer.i_count--;
  1407.         aout_BufferFree( p_audio );
  1408.         if( !p_owner->buffer.p_audio )
  1409.             p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
  1410.     }
  1411.     while( p_owner->buffer.p_subpic )
  1412.     {
  1413.         subpicture_t *p_subpic = p_owner->buffer.p_subpic;
  1414.         p_owner->buffer.p_subpic = p_subpic->p_next;
  1415.         p_owner->buffer.i_count--;
  1416.         subpicture_Delete( p_subpic );
  1417.         if( !p_owner->buffer.p_subpic )
  1418.             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
  1419.     }
  1420.     if( p_owner->buffer.p_block )
  1421.     {
  1422.         block_ChainRelease( p_owner->buffer.p_block );
  1423.         p_owner->buffer.i_count = 0;
  1424.         p_owner->buffer.p_block = NULL;
  1425.         p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
  1426.     }
  1427. }
  1428. /* This function process a block for sout
  1429.  */
  1430. static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
  1431. {
  1432.     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
  1433.     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
  1434.     block_t *p_sout_block;
  1435.     while( ( p_sout_block =
  1436.                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
  1437.     {
  1438.         if( !p_owner->p_sout_input )
  1439.         {
  1440.             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
  1441.             p_owner->sout.i_group = p_dec->fmt_in.i_group;
  1442.             p_owner->sout.i_id = p_dec->fmt_in.i_id;
  1443.             if( p_dec->fmt_in.psz_language )
  1444.             {
  1445.                 if( p_owner->sout.psz_language )
  1446.                     free( p_owner->sout.psz_language );
  1447.                 p_owner->sout.psz_language =
  1448.                     strdup( p_dec->fmt_in.psz_language );
  1449.             }
  1450.             p_owner->p_sout_input =
  1451.                 sout_InputNew( p_owner->p_sout,
  1452.                                &p_owner->sout );
  1453.             if( p_owner->p_sout_input == NULL )
  1454.             {
  1455.                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
  1456.                          (char *)&p_owner->sout.i_codec );
  1457.                 p_dec->b_error = true;
  1458.                 while( p_sout_block )
  1459.                 {
  1460.                     block_t *p_next = p_sout_block->p_next;
  1461.                     block_Release( p_sout_block );
  1462.                     p_sout_block = p_next;
  1463.                 }
  1464.                 break;
  1465.             }
  1466.         }
  1467.         while( p_sout_block )
  1468.         {
  1469.             block_t *p_next = p_sout_block->p_next;
  1470.             p_sout_block->p_next = NULL;
  1471.             DecoderPlaySout( p_dec, p_sout_block, b_telx );
  1472.             p_sout_block = p_next;
  1473.         }
  1474.     }
  1475. }
  1476. /* This function process a video block
  1477.  */
  1478. static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
  1479. {
  1480.     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
  1481.     if( p_owner->p_packetizer )
  1482.     {
  1483.         block_t *p_packetized_block;
  1484.         decoder_t *p_packetizer = p_owner->p_packetizer;
  1485.         while( (p_packetized_block =
  1486.                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
  1487.         {
  1488.             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
  1489.             {
  1490.                 es_format_Clean( &p_dec->fmt_in );
  1491.                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
  1492.             }
  1493.             if( p_packetizer->pf_get_cc )
  1494.                 DecoderGetCc( p_dec, p_packetizer );
  1495.             while( p_packetized_block )
  1496.             {
  1497.                 block_t *p_next = p_packetized_block->p_next;
  1498.                 p_packetized_block->p_next = NULL;
  1499.                 DecoderDecodeVideo( p_dec, p_packetized_block );
  1500.                 p_packetized_block = p_next;
  1501.             }
  1502.         }
  1503.         /* The packetizer does not output a block that tell the decoder to flush
  1504.          * do it ourself */
  1505.         if( b_flush )
  1506.         {
  1507.             block_t *p_null = DecoderBlockFlushNew();
  1508.             if( p_null )
  1509.                 DecoderDecodeVideo( p_dec, p_null );
  1510.         }
  1511.     }
  1512.     else if( p_block )
  1513.     {
  1514.         DecoderDecodeVideo( p_dec, p_block );
  1515.     }
  1516.     if( b_flush && p_owner->p_vout )
  1517.         vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
  1518. }
  1519. /* This function process a audio block
  1520.  */
  1521. static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
  1522. {
  1523.     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
  1524.     if( p_owner->p_packetizer )
  1525.     {
  1526.         block_t *p_packetized_block;
  1527.         decoder_t *p_packetizer = p_owner->p_packetizer;
  1528.         while( (p_packetized_block =
  1529.                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
  1530.         {
  1531.             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
  1532.             {
  1533.                 es_format_Clean( &p_dec->fmt_in );
  1534.                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
  1535.             }
  1536.             while( p_packetized_block )
  1537.             {
  1538.                 block_t *p_next = p_packetized_block->p_next;
  1539.                 p_packetized_block->p_next = NULL;
  1540.                 DecoderDecodeAudio( p_dec, p_packetized_block );
  1541.                 p_packetized_block = p_next;
  1542.             }
  1543.         }
  1544.         /* The packetizer does not output a block that tell the decoder to flush
  1545.          * do it ourself */
  1546.         if( b_flush )
  1547.         {
  1548.             block_t *p_null = DecoderBlockFlushNew();
  1549.             if( p_null )
  1550.                 DecoderDecodeAudio( p_dec, p_null );
  1551.         }
  1552.     }
  1553.     else if( p_block )
  1554.     {
  1555.         DecoderDecodeAudio( p_dec, p_block );
  1556.     }
  1557.     if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
  1558.         aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
  1559. }
  1560. /* This function process a subtitle block
  1561.  */
  1562. static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
  1563. {
  1564.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1565.     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
  1566.     input_thread_t *p_input = p_owner->p_input;
  1567.     vout_thread_t *p_vout;
  1568.     subpicture_t *p_spu;
  1569.     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
  1570.     {
  1571.         vlc_mutex_lock( &p_input->p->counters.counters_lock );
  1572.         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
  1573.         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
  1574.         p_vout = input_resource_HoldVout( p_input->p->p_resource );
  1575.         if( p_vout && p_owner->p_spu_vout == p_vout )
  1576.         {
  1577.             /* Preroll does not work very well with subtitle */
  1578.             if( p_spu->i_start > VLC_TS_INVALID &&
  1579.                 p_spu->i_start < p_owner->i_preroll_end &&
  1580.                 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
  1581.             {
  1582.                 subpicture_Delete( p_spu );
  1583.             }
  1584.             else
  1585.             {
  1586.                 DecoderPlaySpu( p_dec, p_spu, b_telx );
  1587.             }
  1588.         }
  1589.         else
  1590.         {
  1591.             subpicture_Delete( p_spu );
  1592.         }
  1593.         if( p_vout )
  1594.             vlc_object_release( p_vout );
  1595.     }
  1596.     if( b_flush && p_owner->p_spu_vout )
  1597.     {
  1598.         p_vout = input_resource_HoldVout( p_input->p->p_resource );
  1599.         if( p_vout && p_owner->p_spu_vout == p_vout )
  1600.             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
  1601.                          p_owner->i_spu_channel );
  1602.         if( p_vout )
  1603.             vlc_object_release( p_vout );
  1604.     }
  1605. }
  1606. /* */
  1607. static void DecoderProcessOnFlush( decoder_t *p_dec )
  1608. {
  1609.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1610.     vlc_mutex_lock( &p_owner->lock );
  1611.     DecoderFlushBuffering( p_dec );
  1612.     if( p_owner->b_flushing )
  1613.     {
  1614.         p_owner->b_flushing = false;
  1615.         vlc_cond_signal( &p_owner->wait_acknowledge );
  1616.     }
  1617.     vlc_mutex_unlock( &p_owner->lock );
  1618. }
  1619. /**
  1620.  * Decode a block
  1621.  *
  1622.  * param p_dec the decoder object
  1623.  * param p_block the block to decode
  1624.  * return VLC_SUCCESS or an error code
  1625.  */
  1626. static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
  1627. {
  1628.     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
  1629.     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
  1630.     if( p_block && p_block->i_buffer <= 0 )
  1631.     {
  1632.         assert( !b_flush_request );
  1633.         block_Release( p_block );
  1634.         return;
  1635.     }
  1636. #ifdef ENABLE_SOUT
  1637.     if( vlc_internals( p_dec )->i_object_type == VLC_OBJECT_PACKETIZER )
  1638.     {
  1639.         if( p_block )
  1640.             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
  1641.         DecoderProcessSout( p_dec, p_block );
  1642.     }
  1643.     else
  1644. #endif
  1645.     {
  1646.         bool b_flush = false;
  1647.         if( p_block )
  1648.         {
  1649.             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
  1650.             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
  1651.             b_flush = !b_flushing && b_flush_request;
  1652.             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
  1653.         }
  1654.         if( p_dec->fmt_out.i_cat == AUDIO_ES )
  1655.         {
  1656.             DecoderProcessAudio( p_dec, p_block, b_flush );
  1657.         }
  1658.         else if( p_dec->fmt_out.i_cat == VIDEO_ES )
  1659.         {
  1660.             DecoderProcessVideo( p_dec, p_block, b_flush );
  1661.         }
  1662.         else if( p_dec->fmt_out.i_cat == SPU_ES )
  1663.         {
  1664.             DecoderProcessSpu( p_dec, p_block, b_flush );
  1665.         }
  1666.         else
  1667.         {
  1668.             msg_Err( p_dec, "unknown ES format" );
  1669.             p_dec->b_error = true;
  1670.         }
  1671.     }
  1672.     /* */
  1673.     if( b_flush_request )
  1674.         DecoderProcessOnFlush( p_dec );
  1675. }
  1676. static void DecoderError( decoder_t *p_dec, block_t *p_block )
  1677. {
  1678.     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
  1679.     /* */
  1680.     if( p_block )
  1681.         block_Release( p_block );
  1682.     if( b_flush_request )
  1683.         DecoderProcessOnFlush( p_dec );
  1684. }
  1685. /**
  1686.  * Destroys a decoder object
  1687.  *
  1688.  * param p_dec the decoder object
  1689.  * return nothing
  1690.  */
  1691. static void DeleteDecoder( decoder_t * p_dec )
  1692. {
  1693.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1694.     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
  1695.              (char*)&p_dec->fmt_in.i_codec,
  1696.              (unsigned)block_FifoCount( p_owner->p_fifo ) );
  1697.     /* Free all packets still in the decoder fifo. */
  1698.     block_FifoEmpty( p_owner->p_fifo );
  1699.     block_FifoRelease( p_owner->p_fifo );
  1700.     /* */
  1701.     vlc_mutex_lock( &p_owner->lock );
  1702.     DecoderFlushBuffering( p_dec );
  1703.     vlc_mutex_unlock( &p_owner->lock );
  1704.     /* Cleanup */
  1705.     if( p_owner->p_aout_input )
  1706.         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
  1707.     if( p_owner->p_aout )
  1708.     {
  1709.         input_resource_RequestAout( p_owner->p_input->p->p_resource,
  1710.                                      p_owner->p_aout );
  1711.         input_SendEventAout( p_owner->p_input );
  1712.         p_owner->p_aout = NULL;
  1713.     }
  1714.     if( p_owner->p_vout )
  1715.     {
  1716.         /* Hack to make sure all the the pictures are freed by the decoder
  1717.          * and that the vout is not paused anymore */
  1718.         vout_FixLeaks( p_owner->p_vout, true );
  1719.         if( p_owner->b_paused )
  1720.             vout_ChangePause( p_owner->p_vout, false, mdate() );
  1721.         /* */
  1722.         input_resource_RequestVout( p_owner->p_input->p->p_resource, p_owner->p_vout, NULL, true );
  1723.         input_SendEventVout( p_owner->p_input );
  1724.     }
  1725. #ifdef ENABLE_SOUT
  1726.     if( p_owner->p_sout_input )
  1727.     {
  1728.         sout_InputDelete( p_owner->p_sout_input );
  1729.         es_format_Clean( &p_owner->sout );
  1730.     }
  1731. #endif
  1732.     if( p_dec->fmt_out.i_cat == SPU_ES )
  1733.     {
  1734.         vout_thread_t *p_vout;
  1735.         p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
  1736.         if( p_vout )
  1737.         {
  1738.             if( p_owner->p_spu_vout == p_vout )
  1739.                 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
  1740.             vlc_object_release( p_vout );
  1741.         }
  1742.     }
  1743.     es_format_Clean( &p_dec->fmt_in );
  1744.     es_format_Clean( &p_dec->fmt_out );
  1745.     if( p_dec->p_description )
  1746.         vlc_meta_Delete( p_dec->p_description );
  1747.     es_format_Clean( &p_owner->fmt_description );
  1748.     if( p_owner->p_description )
  1749.         vlc_meta_Delete( p_owner->p_description );
  1750.     if( p_owner->p_packetizer )
  1751.     {
  1752.         module_unneed( p_owner->p_packetizer,
  1753.                        p_owner->p_packetizer->p_module );
  1754.         es_format_Clean( &p_owner->p_packetizer->fmt_in );
  1755.         es_format_Clean( &p_owner->p_packetizer->fmt_out );
  1756.         if( p_owner->p_packetizer->p_description )
  1757.             vlc_meta_Delete( p_owner->p_packetizer->p_description );
  1758.         vlc_object_detach( p_owner->p_packetizer );
  1759.         vlc_object_release( p_owner->p_packetizer );
  1760.     }
  1761.     vlc_cond_destroy( &p_owner->wait_acknowledge );
  1762.     vlc_cond_destroy( &p_owner->wait_request );
  1763.     vlc_mutex_destroy( &p_owner->lock );
  1764.     vlc_object_detach( p_dec );
  1765.     free( p_owner );
  1766. }
  1767. /*****************************************************************************
  1768.  * Buffers allocation callbacks for the decoders
  1769.  *****************************************************************************/
  1770. static void DecoderUpdateFormatLocked( decoder_t *p_dec )
  1771. {
  1772.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1773.     vlc_assert_locked( &p_owner->lock );
  1774.     p_owner->b_fmt_description = true;
  1775.     /* Copy es_format */
  1776.     es_format_Clean( &p_owner->fmt_description );
  1777.     es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
  1778.     /* Move p_description */
  1779.     if( p_owner->p_description && p_dec->p_description )
  1780.         vlc_meta_Delete( p_owner->p_description );
  1781.     p_owner->p_description = p_dec->p_description;
  1782.     p_dec->p_description = NULL;
  1783. }
  1784. static vout_thread_t *aout_request_vout( void *p_private,
  1785.                                          vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
  1786. {
  1787.     decoder_t *p_dec = p_private;
  1788.     input_thread_t *p_input = p_dec->p_owner->p_input;
  1789.     p_vout = input_resource_RequestVout( p_input->p->p_resource, p_vout, p_fmt, b_recyle );
  1790.     input_SendEventVout( p_input );
  1791.     return p_vout;
  1792. }
  1793. static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
  1794. {
  1795.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1796.     aout_buffer_t *p_buffer;
  1797.     if( p_owner->p_aout_input != NULL &&
  1798.         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
  1799.           p_dec->fmt_out.audio.i_original_channels !=
  1800.               p_owner->audio.i_original_channels ||
  1801.           p_dec->fmt_out.audio.i_bytes_per_frame !=
  1802.               p_owner->audio.i_bytes_per_frame ) )
  1803.     {
  1804.         aout_input_t *p_aout_input = p_owner->p_aout_input;
  1805.         /* Parameters changed, restart the aout */
  1806.         vlc_mutex_lock( &p_owner->lock );
  1807.         DecoderFlushBuffering( p_dec );
  1808.         p_owner->p_aout_input = NULL;
  1809.         aout_DecDelete( p_owner->p_aout, p_aout_input );
  1810.         vlc_mutex_unlock( &p_owner->lock );
  1811.     }
  1812.     if( p_owner->p_aout_input == NULL )
  1813.     {
  1814.         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
  1815.         audio_sample_format_t format;
  1816.         aout_input_t *p_aout_input;
  1817.         aout_instance_t *p_aout;
  1818.         aout_request_vout_t request_vout;
  1819.         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
  1820.         p_owner->audio = p_dec->fmt_out.audio;
  1821.         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
  1822.         if( i_force_dolby &&
  1823.             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
  1824.                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
  1825.         {
  1826.             if( i_force_dolby == 1 )
  1827.             {
  1828.                 format.i_original_channels = format.i_original_channels |
  1829.                                              AOUT_CHAN_DOLBYSTEREO;
  1830.             }
  1831.             else /* i_force_dolby == 2 */
  1832.             {
  1833.                 format.i_original_channels = format.i_original_channels &
  1834.                                              ~AOUT_CHAN_DOLBYSTEREO;
  1835.             }
  1836.         }
  1837.         request_vout.pf_request_vout = aout_request_vout;
  1838.         request_vout.p_private = p_dec;
  1839.         p_aout = p_owner->p_aout;
  1840.         if( !p_aout )
  1841.             p_aout = input_resource_RequestAout( p_owner->p_input->p->p_resource, NULL );
  1842.         p_aout_input = aout_DecNew( p_dec, &p_aout,
  1843.                                     &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
  1844.         vlc_mutex_lock( &p_owner->lock );
  1845.         p_owner->p_aout = p_aout;
  1846.         p_owner->p_aout_input = p_aout_input;
  1847.         DecoderUpdateFormatLocked( p_dec );
  1848.         vlc_mutex_unlock( &p_owner->lock );
  1849.         input_SendEventAout( p_owner->p_input );
  1850.         if( p_owner->p_aout_input == NULL )
  1851.         {
  1852.             msg_Err( p_dec, "failed to create audio output" );
  1853.             p_dec->b_error = true;
  1854.             return NULL;
  1855.         }
  1856.         p_dec->fmt_out.audio.i_bytes_per_frame =
  1857.             p_owner->audio.i_bytes_per_frame;
  1858.     }
  1859.     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
  1860.     return p_buffer;
  1861. }
  1862. static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
  1863. {
  1864.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1865.     aout_DecDeleteBuffer( p_owner->p_aout,
  1866.                           p_owner->p_aout_input, p_buffer );
  1867. }
  1868. int vout_CountPictureAvailable( vout_thread_t *p_vout );
  1869. static picture_t *vout_new_buffer( decoder_t *p_dec )
  1870. {
  1871.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1872.     if( p_owner->p_vout == NULL ||
  1873.         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
  1874.         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
  1875.         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
  1876.         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
  1877.     {
  1878.         vout_thread_t *p_vout;
  1879.         if( !p_dec->fmt_out.video.i_width ||
  1880.             !p_dec->fmt_out.video.i_height )
  1881.         {
  1882.             /* Can't create a new vout without display size */
  1883.             return NULL;
  1884.         }
  1885.         if( !p_dec->fmt_out.video.i_visible_width ||
  1886.             !p_dec->fmt_out.video.i_visible_height )
  1887.         {
  1888.             if( p_dec->fmt_in.video.i_visible_width &&
  1889.                 p_dec->fmt_in.video.i_visible_height )
  1890.             {
  1891.                 p_dec->fmt_out.video.i_visible_width =
  1892.                     p_dec->fmt_in.video.i_visible_width;
  1893.                 p_dec->fmt_out.video.i_visible_height =
  1894.                     p_dec->fmt_in.video.i_visible_height;
  1895.             }
  1896.             else
  1897.             {
  1898.                 p_dec->fmt_out.video.i_visible_width =
  1899.                     p_dec->fmt_out.video.i_width;
  1900.                 p_dec->fmt_out.video.i_visible_height =
  1901.                     p_dec->fmt_out.video.i_height;
  1902.             }
  1903.         }
  1904.         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
  1905.             var_CreateGetBool( p_dec, "hdtv-fix" ) )
  1906.         {
  1907.             p_dec->fmt_out.video.i_visible_height = 1080;
  1908.             if( !(p_dec->fmt_out.video.i_sar_num % 136))
  1909.             {
  1910.                 p_dec->fmt_out.video.i_sar_num *= 135;
  1911.                 p_dec->fmt_out.video.i_sar_den *= 136;
  1912.             }
  1913.             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
  1914.         }
  1915.         if( !p_dec->fmt_out.video.i_sar_num ||
  1916.             !p_dec->fmt_out.video.i_sar_den )
  1917.         {
  1918.             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
  1919.               p_dec->fmt_out.video.i_visible_height;
  1920.             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
  1921.               p_dec->fmt_out.video.i_visible_width;
  1922.         }
  1923.         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
  1924.                      &p_dec->fmt_out.video.i_sar_den,
  1925.                      p_dec->fmt_out.video.i_sar_num,
  1926.                      p_dec->fmt_out.video.i_sar_den, 50000 );
  1927.         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
  1928.         p_owner->video = p_dec->fmt_out.video;
  1929.         vlc_mutex_lock( &p_owner->lock );
  1930.         DecoderFlushBuffering( p_dec );
  1931.         p_vout = p_owner->p_vout;
  1932.         p_owner->p_vout = NULL;
  1933.         vlc_mutex_unlock( &p_owner->lock );
  1934.         p_vout = input_resource_RequestVout( p_owner->p_input->p->p_resource,
  1935.                                               p_vout, &p_dec->fmt_out.video, true );
  1936.         vlc_mutex_lock( &p_owner->lock );
  1937.         p_owner->p_vout = p_vout;
  1938.         DecoderUpdateFormatLocked( p_dec );
  1939.         vlc_mutex_unlock( &p_owner->lock );
  1940.         input_SendEventVout( p_owner->p_input );
  1941.         if( p_vout == NULL )
  1942.         {
  1943.             msg_Err( p_dec, "failed to create video output" );
  1944.             p_dec->b_error = true;
  1945.             return NULL;
  1946.         }
  1947.         if( p_owner->video.i_rmask )
  1948.             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
  1949.         if( p_owner->video.i_gmask )
  1950.             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
  1951.         if( p_owner->video.i_bmask )
  1952.             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
  1953.     }
  1954.     /* Get a new picture
  1955.      */
  1956.     for( ;; )
  1957.     {
  1958.         picture_t *p_picture;
  1959.         if( p_dec->b_die || p_dec->b_error )
  1960.             return NULL;
  1961.         /* The video filter chain required that there is always 1 free buffer
  1962.          * that it will use as temporary one. It will release the temporary
  1963.          * buffer once its work is done, so this check is safe even if we don't
  1964.          * lock around both count() and create().
  1965.          */
  1966.         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
  1967.         {
  1968.             p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
  1969.             if( p_picture )
  1970.                 return p_picture;
  1971.         }
  1972.         if( DecoderIsFlushing( p_dec ) )
  1973.             return NULL;
  1974.         /* */
  1975.         DecoderSignalBuffering( p_dec, true );
  1976.         /* Check the decoder doesn't leak pictures */
  1977.         vout_FixLeaks( p_owner->p_vout, false );
  1978.         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
  1979.         msleep( VOUT_OUTMEM_SLEEP );
  1980.     }
  1981. }
  1982. static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
  1983. {
  1984.     vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
  1985. }
  1986. static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
  1987. {
  1988.     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
  1989. }
  1990. static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
  1991. {
  1992.     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
  1993. }
  1994. static subpicture_t *spu_new_buffer( decoder_t *p_dec )
  1995. {
  1996.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  1997.     vout_thread_t *p_vout = NULL;
  1998.     subpicture_t *p_subpic;
  1999.     int i_attempts = 30;
  2000.     while( i_attempts-- )
  2001.     {
  2002.         if( p_dec->b_die || p_dec->b_error )
  2003.             break;
  2004.         p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
  2005.         if( p_vout )
  2006.             break;
  2007.         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
  2008.     }
  2009.     if( !p_vout )
  2010.     {
  2011.         msg_Warn( p_dec, "no vout found, dropping subpicture" );
  2012.         return NULL;
  2013.     }
  2014.     if( p_owner->p_spu_vout != p_vout )
  2015.     {
  2016.         vlc_mutex_lock( &p_owner->lock );
  2017.         DecoderFlushBuffering( p_dec );
  2018.         vlc_mutex_unlock( &p_owner->lock );
  2019.         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
  2020.                      &p_owner->i_spu_channel );
  2021.         p_owner->i_spu_order = 0;
  2022.         p_owner->p_spu_vout = p_vout;
  2023.     }
  2024.     p_subpic = subpicture_New();
  2025.     if( p_subpic )
  2026.     {
  2027.         p_subpic->i_channel = p_owner->i_spu_channel;
  2028.         p_subpic->i_order = p_owner->i_spu_order++;
  2029.         p_subpic->b_subtitle = true;
  2030.     }
  2031.     vlc_object_release( p_vout );
  2032.     return p_subpic;
  2033. }
  2034. static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
  2035. {
  2036.     decoder_owner_sys_t *p_owner = p_dec->p_owner;
  2037.     vout_thread_t *p_vout = NULL;
  2038.     p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
  2039.     if( !p_vout || p_owner->p_spu_vout != p_vout )
  2040.     {
  2041.         if( p_vout )
  2042.             vlc_object_release( p_vout );
  2043.         msg_Warn( p_dec, "no vout found, leaking subpicture" );
  2044.         return;
  2045.     }
  2046.     subpicture_Delete( p_subpic );
  2047.     vlc_object_release( p_vout );
  2048. }