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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * decoder.c: Functions for the management of decoders
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 VideoLAN
  5.  * $Id: decoder.c 8894 2004-10-02 21:01:46Z zorglub $
  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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  24.  *****************************************************************************/
  25. /*****************************************************************************
  26.  * Preamble
  27.  *****************************************************************************/
  28. #include <stdlib.h>
  29. #include <vlc/vlc.h>
  30. #include <vlc/decoder.h>
  31. #include <vlc/vout.h>
  32. #include <vlc/input.h>
  33. #include "stream_output.h"
  34. #include "input_internal.h"
  35. static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
  36. static void        DeleteDecoder( decoder_t * );
  37. static int         DecoderThread( decoder_t * );
  38. static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
  39. /* Buffers allocation callbacks for the decoders */
  40. static aout_buffer_t *aout_new_buffer( decoder_t *, int );
  41. static void aout_del_buffer( decoder_t *, aout_buffer_t * );
  42. static picture_t *vout_new_buffer( decoder_t * );
  43. static void vout_del_buffer( decoder_t *, picture_t * );
  44. static void vout_link_picture( decoder_t *, picture_t * );
  45. static void vout_unlink_picture( decoder_t *, picture_t * );
  46. static subpicture_t *spu_new_buffer( decoder_t * );
  47. static void spu_del_buffer( decoder_t *, subpicture_t * );
  48. static es_format_t null_es_format = {0};
  49. struct decoder_owner_sys_t
  50. {
  51.     vlc_bool_t      b_own_thread;
  52.     input_thread_t  *p_input;
  53.     aout_instance_t *p_aout;
  54.     aout_input_t    *p_aout_input;
  55.     vout_thread_t   *p_vout;
  56.     vout_thread_t   *p_spu_vout;
  57.     int              i_spu_channel;
  58.     sout_instance_t         *p_sout;
  59.     sout_packetizer_input_t *p_sout_input;
  60.     /* Some decoders require already packetized data (ie. not truncated) */
  61.     decoder_t *p_packetizer;
  62.     /* Current format in use by the output */
  63.     video_format_t video;
  64.     audio_format_t audio;
  65.     es_format_t    sout;
  66.     /* fifo */
  67.     block_fifo_t *p_fifo;
  68. };
  69. /**
  70.  * Spawns a new decoder thread
  71.  *
  72.  * param p_input the input thread
  73.  * param p_es the es descriptor
  74.  * return the spawned decoder object
  75.  */
  76. decoder_t *input_DecoderNew( input_thread_t *p_input,
  77.                              es_format_t *fmt, vlc_bool_t b_force_decoder )
  78. {
  79.     decoder_t   *p_dec = NULL;
  80.     vlc_value_t val;
  81.     /* If we are in sout mode, search for packetizer module */
  82.     if( p_input->p_sout && !b_force_decoder )
  83.     {
  84.         /* Create the decoder configuration structure */
  85.         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
  86.         if( p_dec == NULL )
  87.         {
  88.             msg_Err( p_input, "could not create packetizer" );
  89.             return NULL;
  90.         }
  91.     }
  92.     else
  93.     {
  94.         /* Create the decoder configuration structure */
  95.         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
  96.         if( p_dec == NULL )
  97.         {
  98.             msg_Err( p_input, "could not create decoder" );
  99.             return NULL;
  100.         }
  101.     }
  102.     if( !p_dec->p_module )
  103.     {
  104.         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.n"
  105.                  "VLC probably does not support this sound or video format.",
  106.                  (char*)&p_dec->fmt_in.i_codec );
  107.         DeleteDecoder( p_dec );
  108.         vlc_object_destroy( p_dec );
  109.         return NULL;
  110.     }
  111.     if( p_input->p_sout && p_input->input.b_can_pace_control &&
  112.         !b_force_decoder )
  113.     {
  114.         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
  115.         p_dec->p_owner->b_own_thread = VLC_FALSE;
  116.     }
  117.     else
  118.     {
  119.         var_Get( p_input, "minimize-threads", &val );
  120.         p_dec->p_owner->b_own_thread = !val.b_bool;
  121.     }
  122.     if( p_dec->p_owner->b_own_thread )
  123.     {
  124.         int i_priority;
  125.         if( fmt->i_cat == AUDIO_ES )
  126.             i_priority = VLC_THREAD_PRIORITY_AUDIO;
  127.         else
  128.             i_priority = VLC_THREAD_PRIORITY_VIDEO;
  129.         /* Spawn the decoder thread */
  130.         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
  131.                                i_priority, VLC_FALSE ) )
  132.         {
  133.             msg_Err( p_dec, "cannot spawn decoder thread "%s"",
  134.                              p_dec->p_module->psz_object_name );
  135.             module_Unneed( p_dec, p_dec->p_module );
  136.             DeleteDecoder( p_dec );
  137.             vlc_object_destroy( p_dec );
  138.             return NULL;
  139.         }
  140.     }
  141.     return p_dec;
  142. }
  143. /**
  144.  * Kills a decoder thread and waits until it's finished
  145.  *
  146.  * param p_input the input thread
  147.  * param p_es the es descriptor
  148.  * return nothing
  149.  */
  150. void input_DecoderDelete( decoder_t *p_dec )
  151. {
  152.     p_dec->b_die = VLC_TRUE;
  153.     if( p_dec->p_owner->b_own_thread )
  154.     {
  155.         /* Make sure the thread leaves the function by
  156.          * sending it an empty block. */
  157.         block_t *p_block = block_New( p_dec, 0 );
  158.         input_DecoderDecode( p_dec, p_block );
  159.         vlc_thread_join( p_dec );
  160.         /* Don't module_Unneed() here because of the dll loader that wants
  161.          * close() in the same thread than open()/decode() */
  162.     }
  163.     else
  164.     {
  165.         /* Flush */
  166.         input_DecoderDecode( p_dec, NULL );
  167.         module_Unneed( p_dec, p_dec->p_module );
  168.     }
  169.     /* Delete decoder configuration */
  170.     DeleteDecoder( p_dec );
  171.     /* Delete the decoder */
  172.     vlc_object_destroy( p_dec );
  173. }
  174. /**
  175.  * Put a block_t in the decoder's fifo.
  176.  *
  177.  * param p_dec the decoder object
  178.  * param p_block the data block
  179.  */
  180. void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
  181. {
  182.     if( p_dec->p_owner->b_own_thread )
  183.     {
  184.         if( p_dec->p_owner->p_input->b_out_pace_control )
  185.         {
  186.             /* FIXME !!!!! */
  187.             while( !p_dec->b_die && !p_dec->b_error &&
  188.                    p_dec->p_owner->p_fifo->i_depth > 10 )
  189.             {
  190.                 msleep( 1000 );
  191.             }
  192.         }
  193.         else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
  194.         {
  195.             /* FIXME: ideally we would check the time amount of data
  196.              * in the fifo instead of its size. */
  197.             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
  198.                       "consummed quickly enough), resetting fifo!" );
  199.             block_FifoEmpty( p_dec->p_owner->p_fifo );
  200.         }
  201.         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
  202.     }
  203.     else
  204.     {
  205.         if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
  206.         {
  207.             if( p_block ) block_Release( p_block );
  208.         }
  209.         else
  210.         {
  211.             DecoderDecode( p_dec, p_block );
  212.         }
  213.     }
  214. }
  215. void input_DecoderDiscontinuity( decoder_t * p_dec )
  216. {
  217.     block_t *p_null;
  218.     /* Empty the fifo */
  219.     if( p_dec->p_owner->b_own_thread )
  220.     {
  221.         block_FifoEmpty( p_dec->p_owner->p_fifo );
  222.     }
  223.     /* Send a special block */
  224.     p_null = block_New( p_dec, 128 );
  225.     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
  226.     memset( p_null->p_buffer, 0, p_null->i_buffer );
  227.     input_DecoderDecode( p_dec, p_null );
  228. }
  229. vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
  230. {
  231.     if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
  232.     {
  233.         return VLC_FALSE;
  234.     }
  235.     return VLC_TRUE;
  236. }
  237. #if 0
  238. /**
  239.  * Create a NULL packet for padding in case of a data loss
  240.  *
  241.  * param p_input the input thread
  242.  * param p_es es descriptor
  243.  * return nothing
  244.  */
  245. static void input_NullPacket( input_thread_t * p_input,
  246.                               es_descriptor_t * p_es )
  247. {
  248. #if 0
  249.     block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
  250.     if( p_block )
  251.     {
  252.         memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
  253.         p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
  254.         block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
  255.     }
  256. #endif
  257. }
  258. /**
  259.  * Send a NULL packet to the decoders
  260.  *
  261.  * param p_input the input thread
  262.  * return nothing
  263.  */
  264. void input_EscapeDiscontinuity( input_thread_t * p_input )
  265. {
  266. #if 0
  267.     unsigned int i_es, i;
  268.     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
  269.     {
  270.         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
  271.         if( p_es->p_dec != NULL )
  272.         {
  273.             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
  274.             {
  275.                 input_NullPacket( p_input, p_es );
  276.             }
  277.         }
  278.     }
  279. #endif
  280. }
  281. /**
  282.  * Send a NULL packet to the audio decoders
  283.  *
  284.  * param p_input the input thread
  285.  * return nothing
  286.  */
  287. void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
  288. {
  289. #if 0
  290.     unsigned int i_es, i;
  291.     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
  292.     {
  293.         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
  294.         if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
  295.         {
  296.             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
  297.             {
  298.                 input_NullPacket( p_input, p_es );
  299.             }
  300.         }
  301.     }
  302. #endif
  303. }
  304. #endif
  305. /**
  306.  * Create a decoder object
  307.  *
  308.  * param p_input the input thread
  309.  * param p_es the es descriptor
  310.  * param i_object_type Object type as define in include/vlc_objects.h
  311.  * return the decoder object
  312.  */
  313. static decoder_t * CreateDecoder( input_thread_t *p_input,
  314.                                   es_format_t *fmt, int i_object_type )
  315. {
  316.     decoder_t *p_dec;
  317.     p_dec = vlc_object_create( p_input, i_object_type );
  318.     if( p_dec == NULL )
  319.     {
  320.         msg_Err( p_input, "out of memory" );
  321.         return NULL;
  322.     }
  323.     p_dec->pf_decode_audio = 0;
  324.     p_dec->pf_decode_video = 0;
  325.     p_dec->pf_decode_sub = 0;
  326.     p_dec->pf_packetize = 0;
  327.     /* Initialize the decoder fifo */
  328.     p_dec->p_module = NULL;
  329.     es_format_Copy( &p_dec->fmt_in, fmt );
  330.     es_format_Copy( &p_dec->fmt_out, &null_es_format );
  331.     /* Allocate our private structure for the decoder */
  332.     p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
  333.     if( p_dec->p_owner == NULL )
  334.     {
  335.         msg_Err( p_dec, "out of memory" );
  336.         return NULL;
  337.     }
  338.     p_dec->p_owner->b_own_thread = VLC_TRUE;
  339.     p_dec->p_owner->p_input = p_input;
  340.     p_dec->p_owner->p_aout = NULL;
  341.     p_dec->p_owner->p_aout_input = NULL;
  342.     p_dec->p_owner->p_vout = NULL;
  343.     p_dec->p_owner->p_spu_vout = NULL;
  344.     p_dec->p_owner->i_spu_channel = 0;
  345.     p_dec->p_owner->p_sout = p_input->p_sout;
  346.     p_dec->p_owner->p_sout_input = NULL;
  347.     p_dec->p_owner->p_packetizer = NULL;
  348.     /* decoder fifo */
  349.     if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
  350.     {
  351.         msg_Err( p_dec, "out of memory" );
  352.         return NULL;
  353.     }
  354.     /* Set buffers allocation callbacks for the decoders */
  355.     p_dec->pf_aout_buffer_new = aout_new_buffer;
  356.     p_dec->pf_aout_buffer_del = aout_del_buffer;
  357.     p_dec->pf_vout_buffer_new = vout_new_buffer;
  358.     p_dec->pf_vout_buffer_del = vout_del_buffer;
  359.     p_dec->pf_picture_link    = vout_link_picture;
  360.     p_dec->pf_picture_unlink  = vout_unlink_picture;
  361.     p_dec->pf_spu_buffer_new  = spu_new_buffer;
  362.     p_dec->pf_spu_buffer_del  = spu_del_buffer;
  363.     vlc_object_attach( p_dec, p_input );
  364.     /* Find a suitable decoder/packetizer module */
  365.     if( i_object_type == VLC_OBJECT_DECODER )
  366.         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
  367.     else
  368.         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
  369.     /* Check if decoder requires already packetized data */
  370.     if( i_object_type == VLC_OBJECT_DECODER &&
  371.         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
  372.     {
  373.         p_dec->p_owner->p_packetizer =
  374.             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
  375.         if( p_dec->p_owner->p_packetizer )
  376.         {
  377.             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
  378.                             &p_dec->fmt_in );
  379.             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
  380.                             &null_es_format );
  381.             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
  382.             p_dec->p_owner->p_packetizer->p_module =
  383.                 module_Need( p_dec->p_owner->p_packetizer,
  384.                              "packetizer", "$packetizer", 0 );
  385.             if( !p_dec->p_owner->p_packetizer->p_module )
  386.             {
  387.                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
  388.                 vlc_object_detach( p_dec->p_owner->p_packetizer );
  389.                 vlc_object_destroy( p_dec->p_owner->p_packetizer );
  390.             }
  391.         }
  392.     }
  393.     return p_dec;
  394. }
  395. /**
  396.  * The decoding main loop
  397.  *
  398.  * param p_dec the decoder
  399.  * return 0
  400.  */
  401. static int DecoderThread( decoder_t * p_dec )
  402. {
  403.     block_t *p_block;
  404.     /* The decoder's main loop */
  405.     while( !p_dec->b_die && !p_dec->b_error )
  406.     {
  407.         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
  408.         {
  409.             p_dec->b_error = 1;
  410.             break;
  411.         }
  412.         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
  413.         {
  414.             break;
  415.         }
  416.     }
  417.     while( !p_dec->b_die )
  418.     {
  419.         /* Trash all received PES packets */
  420.         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
  421.         if( p_block ) block_Release( p_block );
  422.     }
  423.     /* We do it here because of the dll loader that wants close() in the
  424.      * same thread than open()/decode() */
  425.     module_Unneed( p_dec, p_dec->p_module );
  426.     return 0;
  427. }
  428. /**
  429.  * Decode a block
  430.  *
  431.  * param p_dec the decoder object
  432.  * param p_block the block to decode
  433.  * return VLC_SUCCESS or an error code
  434.  */
  435. static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
  436. {
  437.     int i_rate = p_block ? p_block->i_rate : 1000;
  438.     if( p_block && p_block->i_buffer <= 0 )
  439.     {
  440.         block_Release( p_block );
  441.         return VLC_SUCCESS;
  442.     }
  443.     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
  444.     {
  445.         block_t *p_sout_block;
  446.         while( ( p_sout_block =
  447.                      p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
  448.         {
  449.             if( !p_dec->p_owner->p_sout_input )
  450.             {
  451.                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
  452.                 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
  453.                 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
  454.                 if( p_dec->fmt_in.psz_language )
  455.                 {
  456.                     p_dec->p_owner->sout.psz_language =
  457.                         strdup( p_dec->fmt_in.psz_language );
  458.                 }
  459.                 p_dec->p_owner->p_sout_input =
  460.                     sout_InputNew( p_dec->p_owner->p_sout,
  461.                                    &p_dec->p_owner->sout );
  462.                 if( p_dec->p_owner->p_sout_input == NULL )
  463.                 {
  464.                     msg_Err( p_dec, "cannot create packetizer output" );
  465.                     p_dec->b_error = VLC_TRUE;
  466.                     while( p_sout_block )
  467.                     {
  468.                         block_t *p_next = p_sout_block->p_next;
  469.                         block_Release( p_sout_block );
  470.                         p_sout_block = p_next;
  471.                     }
  472.                     break;
  473.                 }
  474.             }
  475.             while( p_sout_block )
  476.             {
  477.                 block_t *p_next = p_sout_block->p_next;
  478.                 p_sout_block->p_next = NULL;
  479.                 p_sout_block->i_rate = i_rate;
  480.                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
  481.                                       p_sout_block );
  482.                 p_sout_block = p_next;
  483.             }
  484.             /* For now it's enough, as only sout inpact on this flag */
  485.             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
  486.                 p_dec->p_owner->p_input->b_out_pace_control )
  487.             {
  488.                 msg_Dbg( p_dec, "switching to synch mode" );
  489.                 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
  490.             }
  491.             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
  492.                      !p_dec->p_owner->p_input->b_out_pace_control )
  493.             {
  494.                 msg_Dbg( p_dec, "switching to asynch mode" );
  495.                 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
  496.             }
  497.         }
  498.     }
  499.     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
  500.     {
  501.         aout_buffer_t *p_aout_buf;
  502.         if( p_dec->p_owner->p_packetizer )
  503.         {
  504.             block_t *p_packetized_block;
  505.             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
  506.             while( (p_packetized_block =
  507.                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
  508.             {
  509.                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
  510.                 {
  511.                     p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
  512.                     p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
  513.                     memcpy( p_dec->fmt_in.p_extra,
  514.                             p_packetizer->fmt_out.p_extra,
  515.                             p_dec->fmt_in.i_extra );
  516.                 }
  517.                 while( p_packetized_block )
  518.                 {
  519.                     block_t *p_next = p_packetized_block->p_next;
  520.                     p_packetized_block->p_next = NULL;
  521.                     p_packetized_block->i_rate = i_rate;
  522.                     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec,
  523.                                                        &p_packetized_block )) )
  524.                     {
  525.                         aout_DecPlay( p_dec->p_owner->p_aout,
  526.                                       p_dec->p_owner->p_aout_input,
  527.                                       p_aout_buf );
  528.                     }
  529.                     p_packetized_block = p_next;
  530.                 }
  531.             }
  532.         }
  533.         else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
  534.         {
  535.             aout_DecPlay( p_dec->p_owner->p_aout,
  536.                           p_dec->p_owner->p_aout_input, p_aout_buf );
  537.         }
  538.     }
  539.     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
  540.     {
  541.         picture_t *p_pic;
  542.         if( p_dec->p_owner->p_packetizer )
  543.         {
  544.             block_t *p_packetized_block;
  545.             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
  546.             while( (p_packetized_block =
  547.                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
  548.             {
  549.                 while( p_packetized_block )
  550.                 {
  551.                     block_t *p_next = p_packetized_block->p_next;
  552.                     p_packetized_block->p_next = NULL;
  553.                     p_packetized_block->i_rate = i_rate;
  554.                     while( (p_pic = p_dec->pf_decode_video( p_dec,
  555.                                                        &p_packetized_block )) )
  556.                     {
  557.                         vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
  558.                                           p_pic->date );
  559.                         vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
  560.                     }
  561.                     p_packetized_block = p_next;
  562.                 }
  563.             }
  564.         }
  565.         else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
  566.         {
  567.             vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
  568.             vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
  569.         }
  570.     }
  571.     else if( p_dec->fmt_in.i_cat == SPU_ES )
  572.     {
  573.         vout_thread_t *p_vout;
  574.         subpicture_t *p_spu;
  575.         while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
  576.         {
  577.             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
  578.             if( p_vout )
  579.             {
  580.                 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
  581.                 vlc_object_release( p_vout );
  582.             }
  583.         }
  584.     }
  585.     else
  586.     {
  587.         msg_Err( p_dec, "unknown ES format" );
  588.         p_dec->b_error = 1;
  589.     }
  590.     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
  591. }
  592. /**
  593.  * Destroys a decoder object
  594.  *
  595.  * param p_dec the decoder object
  596.  * return nothing
  597.  */
  598. static void DeleteDecoder( decoder_t * p_dec )
  599. {
  600.     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
  601.              (char*)&p_dec->fmt_in.i_codec,
  602.              p_dec->p_owner->p_fifo->i_depth );
  603.     /* Free all packets still in the decoder fifo. */
  604.     block_FifoEmpty( p_dec->p_owner->p_fifo );
  605.     block_FifoRelease( p_dec->p_owner->p_fifo );
  606.     /* Cleanup */
  607.     if( p_dec->p_owner->p_aout_input )
  608.         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
  609.     if( p_dec->p_owner->p_vout )
  610.     {
  611.         int i_pic;
  612. #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
  613.         /* Hack to make sure all the the pictures are freed by the decoder */
  614.         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
  615.              i_pic++ )
  616.         {
  617.             if( p_pic->i_status == RESERVED_PICTURE )
  618.                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
  619.             if( p_pic->i_refcount > 0 )
  620.                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
  621.         }
  622. #undef p_pic
  623.         /* We are about to die. Reattach video output to p_vlc. */
  624.         vout_Request( p_dec, p_dec->p_owner->p_vout, 0, 0, 0, 0 );
  625.     }
  626.     if( p_dec->p_owner->p_sout_input )
  627.     {
  628.         sout_InputDelete( p_dec->p_owner->p_sout_input );
  629.         es_format_Clean( &p_dec->p_owner->sout );
  630.     }
  631.     if( p_dec->fmt_in.i_cat == SPU_ES )
  632.     {
  633.         vout_thread_t *p_vout;
  634.         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
  635.         if( p_vout )
  636.         {
  637.             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
  638.                          p_dec->p_owner->i_spu_channel );
  639.             vlc_object_release( p_vout );
  640.         }
  641.     }
  642.     es_format_Clean( &p_dec->fmt_in );
  643.     es_format_Clean( &p_dec->fmt_out );
  644.     if( p_dec->p_owner->p_packetizer )
  645.     {
  646.         module_Unneed( p_dec->p_owner->p_packetizer,
  647.                        p_dec->p_owner->p_packetizer->p_module );
  648.         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
  649.         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
  650.         vlc_object_detach( p_dec->p_owner->p_packetizer );
  651.         vlc_object_destroy( p_dec->p_owner->p_packetizer );
  652.     }
  653.     vlc_object_detach( p_dec );
  654.     free( p_dec->p_owner );
  655. }
  656. /*****************************************************************************
  657.  * Buffers allocation callbacks for the decoders
  658.  *****************************************************************************/
  659. static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
  660. {
  661.     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
  662.     aout_buffer_t *p_buffer;
  663.     if( p_sys->p_aout_input != NULL &&
  664.         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
  665.           p_dec->fmt_out.audio.i_original_channels !=
  666.               p_sys->audio.i_original_channels ||
  667.           p_dec->fmt_out.audio.i_bytes_per_frame !=
  668.               p_sys->audio.i_bytes_per_frame ) )
  669.     {
  670.         /* Parameters changed, restart the aout */
  671.         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
  672.         p_sys->p_aout_input = NULL;
  673.     }
  674.     if( p_sys->p_aout_input == NULL )
  675.     {
  676.         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
  677.         p_sys->audio = p_dec->fmt_out.audio;
  678.         p_sys->p_aout_input =
  679.             aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
  680.         if( p_sys->p_aout_input == NULL )
  681.         {
  682.             msg_Err( p_dec, "failed to create audio output" );
  683.             p_dec->b_error = VLC_TRUE;
  684.             return NULL;
  685.         }
  686.         p_dec->fmt_out.audio.i_bytes_per_frame =
  687.             p_sys->audio.i_bytes_per_frame;
  688.     }
  689.     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
  690.                                   i_samples );
  691.     return p_buffer;
  692. }
  693. static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
  694. {
  695.     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
  696.                           p_dec->p_owner->p_aout_input, p_buffer );
  697. }
  698. static picture_t *vout_new_buffer( decoder_t *p_dec )
  699. {
  700.     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
  701.     picture_t *p_pic;
  702.     if( p_sys->p_vout == NULL ||
  703.         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
  704.         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
  705.         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
  706.         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
  707.     {
  708.         if( !p_dec->fmt_out.video.i_width ||
  709.             !p_dec->fmt_out.video.i_height )
  710.         {
  711.             /* Can't create a new vout without display size */
  712.             return NULL;
  713.         }
  714.         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
  715.         p_sys->video = p_dec->fmt_out.video;
  716.         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
  717.                                       p_sys->video.i_width,
  718.                                       p_sys->video.i_height,
  719.                                       p_sys->video.i_chroma,
  720.                                       p_sys->video.i_aspect );
  721.         if( p_sys->p_vout == NULL )
  722.         {
  723.             msg_Err( p_dec, "failed to create video output" );
  724.             p_dec->b_error = VLC_TRUE;
  725.             return NULL;
  726.         }
  727.         if( p_sys->video.i_rmask )
  728.             p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
  729.         if( p_sys->video.i_gmask )
  730.             p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
  731.         if( p_sys->video.i_bmask )
  732.             p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
  733.     }
  734.     /* Get a new picture */
  735.     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
  736.     {
  737.         int i_pic, i_ready_pic = 0;
  738.         if( p_dec->b_die || p_dec->b_error )
  739.         {
  740.             return NULL;
  741.         }
  742. #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
  743.         /* Check the decoder doesn't leak pictures */
  744.         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
  745.              i_pic++ )
  746.         {
  747.             if( p_pic->i_status == READY_PICTURE )
  748.             {
  749.                 if( i_ready_pic++ > 0 ) break;
  750.                 else continue;
  751.             }
  752.             if( p_pic->i_status != DISPLAYED_PICTURE &&
  753.                 p_pic->i_status != RESERVED_PICTURE &&
  754.                 p_pic->i_status != READY_PICTURE ) break;
  755.             if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
  756.                 break;
  757.         }
  758.         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
  759.         {
  760.             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
  761.             /* Just free all the pictures */
  762.             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
  763.                  i_pic++ )
  764.             {
  765.                 if( p_pic->i_status == RESERVED_PICTURE )
  766.                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
  767.                 if( p_pic->i_refcount > 0 )
  768.                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
  769.             }
  770.         }
  771. #undef p_pic
  772.         msleep( VOUT_OUTMEM_SLEEP );
  773.     }
  774.     return p_pic;
  775. }
  776. static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
  777. {
  778.     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
  779. }
  780. static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
  781. {
  782.     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
  783. }
  784. static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
  785. {
  786.     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
  787. }
  788. static subpicture_t *spu_new_buffer( decoder_t *p_dec )
  789. {
  790.     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
  791.     vout_thread_t *p_vout = NULL;
  792.     subpicture_t *p_subpic;
  793.     int i_attempts = 30;
  794.     while( i_attempts-- )
  795.     {
  796.         if( p_dec->b_die || p_dec->b_error ) break;
  797.         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
  798.         if( p_vout ) break;
  799.         msleep( VOUT_DISPLAY_DELAY );
  800.     }
  801.     if( !p_vout )
  802.     {
  803.         msg_Warn( p_dec, "no vout found, dropping subpicture" );
  804.         return NULL;
  805.     }
  806.     if( p_sys->p_spu_vout != p_vout )
  807.     {
  808.         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
  809.                      &p_sys->i_spu_channel );
  810.         p_sys->p_spu_vout = p_vout;
  811.     }
  812.     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
  813.     if( p_subpic )
  814.     {
  815.         p_subpic->i_channel = p_sys->i_spu_channel;
  816.     }
  817.     vlc_object_release( p_vout );
  818.     return p_subpic;
  819. }
  820. static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
  821. {
  822.     spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_subpic );
  823. }