Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

spudec.c

00001 /*****************************************************************************
00002  * spudec.c : SPU decoder thread
00003  *****************************************************************************
00004  * Copyright (C) 2000-2001 the VideoLAN team
00005  * $Id: spudec.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Samuel Hocevar <[email protected]>
00008  *          Laurent Aimar <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <vlc/vlc.h>
00029 #include <vlc/decoder.h>
00030 
00031 #include "spudec.h"
00032 
00033 /*****************************************************************************
00034  * Module descriptor.
00035  *****************************************************************************/
00036 static int  DecoderOpen   ( vlc_object_t * );
00037 static int  PacketizerOpen( vlc_object_t * );
00038 static void Close         ( vlc_object_t * );
00039 
00040 vlc_module_begin();
00041     set_description( _("DVD subtitles decoder") );
00042     set_capability( "decoder", 50 );
00043     set_category( CAT_INPUT );
00044     set_subcategory( SUBCAT_INPUT_SCODEC );
00045     set_callbacks( DecoderOpen, Close );
00046 
00047     add_submodule();
00048     set_description( _("DVD subtitles packetizer") );
00049     set_capability( "packetizer", 50 );
00050     set_callbacks( PacketizerOpen, Close );
00051 vlc_module_end();
00052 
00053 /*****************************************************************************
00054  * Local prototypes
00055  *****************************************************************************/
00056 static block_t *      Reassemble( decoder_t *, block_t ** );
00057 static subpicture_t * Decode    ( decoder_t *, block_t ** );
00058 static block_t *      Packetize ( decoder_t *, block_t ** );
00059 
00060 /*****************************************************************************
00061  * DecoderOpen
00062  *****************************************************************************
00063  * Tries to launch a decoder and return score so that the interface is able
00064  * to chose.
00065  *****************************************************************************/
00066 static int DecoderOpen( vlc_object_t *p_this )
00067 {
00068     decoder_t     *p_dec = (decoder_t*)p_this;
00069     decoder_sys_t *p_sys;
00070 
00071     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 's','p','u',' ' ) &&
00072         p_dec->fmt_in.i_codec != VLC_FOURCC( 's','p','u','b' ) )
00073     {
00074         return VLC_EGENERIC;
00075     }
00076 
00077     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
00078 
00079     p_sys->b_packetizer = VLC_FALSE;
00080     p_sys->i_spu_size = 0;
00081     p_sys->i_spu      = 0;
00082     p_sys->p_block    = NULL;
00083 
00084     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
00085 
00086     p_dec->pf_decode_sub = Decode;
00087     p_dec->pf_packetize  = NULL;
00088 
00089     return VLC_SUCCESS;
00090 }
00091 
00092 /*****************************************************************************
00093  * PacketizerOpen
00094  *****************************************************************************
00095  * Tries to launch a decoder and return score so that the interface is able
00096  * to chose.
00097  *****************************************************************************/
00098 static int PacketizerOpen( vlc_object_t *p_this )
00099 {
00100     decoder_t *p_dec = (decoder_t*)p_this;
00101 
00102     if( DecoderOpen( p_this ) )
00103     {
00104         return VLC_EGENERIC;
00105     }
00106     p_dec->pf_packetize  = Packetize;
00107     p_dec->p_sys->b_packetizer = VLC_TRUE;
00108     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
00109     p_dec->fmt_out.i_codec = VLC_FOURCC( 's','p','u',' ' );
00110 
00111     return VLC_SUCCESS;
00112 }
00113 
00114 /*****************************************************************************
00115  * Close:
00116  *****************************************************************************/
00117 static void Close( vlc_object_t *p_this )
00118 {
00119     decoder_t     *p_dec = (decoder_t*)p_this;
00120     decoder_sys_t *p_sys = p_dec->p_sys;
00121 
00122     if( p_sys->p_block ) block_ChainRelease( p_sys->p_block );
00123     free( p_sys );
00124 }
00125 
00126 /*****************************************************************************
00127  * Decode:
00128  *****************************************************************************/
00129 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
00130 {
00131     decoder_sys_t *p_sys = p_dec->p_sys;
00132     block_t       *p_spu_block;
00133 
00134     if( (p_spu_block = Reassemble( p_dec, pp_block )) )
00135     {
00136         subpicture_t *p_spu;
00137 
00138         p_sys->i_spu = block_ChainExtract( p_spu_block, p_sys->buffer, 65536 );
00139         p_sys->i_pts = p_spu_block->i_pts;
00140         block_ChainRelease( p_spu_block );
00141 
00142         /* Parse and decode */
00143         p_spu = E_(ParsePacket)( p_dec );
00144 
00145         /* reinit context */
00146         p_sys->i_spu_size = 0;
00147         p_sys->i_rle_size = 0;
00148         p_sys->i_spu      = 0;
00149         p_sys->p_block    = NULL;
00150 
00151         return p_spu;
00152     }
00153 
00154     return NULL;
00155 }
00156 
00157 /*****************************************************************************
00158  * Packetize:
00159  *****************************************************************************/
00160 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
00161 {
00162     decoder_sys_t *p_sys = p_dec->p_sys;
00163     block_t       *p_spu = Reassemble( p_dec, pp_block );
00164 
00165     if( p_spu )
00166     {
00167         p_spu->i_dts = p_spu->i_pts;
00168         p_spu->i_length = 0;
00169 
00170         /* reinit context */
00171         p_sys->i_spu_size = 0;
00172         p_sys->i_rle_size = 0;
00173         p_sys->i_spu      = 0;
00174         p_sys->p_block    = NULL;
00175 
00176         return block_ChainGather( p_spu );
00177     }
00178 
00179     return NULL;
00180 }
00181 
00182 /*****************************************************************************
00183  * Reassemble:
00184  *****************************************************************************/
00185 static block_t *Reassemble( decoder_t *p_dec, block_t **pp_block )
00186 {
00187     decoder_sys_t *p_sys = p_dec->p_sys;
00188     block_t *p_block;
00189 
00190     if( pp_block == NULL || *pp_block == NULL ) return NULL;
00191     p_block = *pp_block;
00192     *pp_block = NULL;
00193 
00194     if( p_sys->i_spu_size <= 0 &&
00195         ( p_block->i_pts <= 0 || p_block->i_buffer < 4 ) )
00196     {
00197         msg_Dbg( p_dec, "invalid starting packet (size < 4 or pts <=0)" );
00198         msg_Dbg( p_dec, "spu size: %d, i_pts: "I64Fd" i_buffer: %d",
00199                  p_sys->i_spu_size, p_block->i_pts, p_block->i_buffer );
00200         block_Release( p_block );
00201         return NULL;
00202     }
00203 
00204     block_ChainAppend( &p_sys->p_block, p_block );
00205     p_sys->i_spu += p_block->i_buffer;
00206 
00207     if( p_sys->i_spu_size <= 0 )
00208     {
00209         p_sys->i_spu_size = ( p_block->p_buffer[0] << 8 )|
00210             p_block->p_buffer[1];
00211         p_sys->i_rle_size = ( ( p_block->p_buffer[2] << 8 )|
00212             p_block->p_buffer[3] ) - 4;
00213 
00214         /* msg_Dbg( p_dec, "i_spu_size=%d i_rle=%d",
00215                     p_sys->i_spu_size, p_sys->i_rle_size ); */
00216 
00217         if( p_sys->i_spu_size <= 0 || p_sys->i_rle_size >= p_sys->i_spu_size )
00218         {
00219             p_sys->i_spu_size = 0;
00220             p_sys->i_rle_size = 0;
00221             p_sys->i_spu      = 0;
00222             p_sys->p_block    = NULL;
00223 
00224             block_Release( p_block );
00225             return NULL;
00226         }
00227     }
00228 
00229     if( p_sys->i_spu >= p_sys->i_spu_size )
00230     {
00231         /* We have a complete sub */
00232         msg_Dbg( p_dec, "SPU packets size=%d should be %d",
00233                  p_sys->i_spu, p_sys->i_spu_size );
00234 
00235         return p_sys->p_block;
00236     }
00237     return NULL;
00238 }

Generated on Tue Dec 20 10:14:30 2005 for vlc-0.8.4a by  doxygen 1.4.2