📄 cvdsub.c
字号:
/***************************************************************************** * cvd.c : CVD Subtitle decoder ***************************************************************************** * Copyright (C) 2003, 2004 VideoLAN * $Id: cvdsub.c 10101 2005-03-02 16:47:31Z robux4 $ * * Authors: Rocky Bernstein * Gildas Bazin <gbazin@videolan.org> * Julio Sanchez Fernandez (http://subhandler.sourceforge.net) * Laurent Aimar <fenrir@via.ecp.fr> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <vlc/vlc.h>#include <vlc/vout.h>#include <vlc/decoder.h>#include "vlc_bits.h"#define DEBUG_CVDSUB 1/***************************************************************************** * Module descriptor. *****************************************************************************/static int DecoderOpen ( vlc_object_t * );static int PacketizerOpen( vlc_object_t * );static void DecoderClose ( vlc_object_t * );vlc_module_begin(); set_description( _("CVD subtitle decoder") ); set_capability( "decoder", 50 ); set_callbacks( DecoderOpen, DecoderClose ); add_submodule(); set_description( _("Chaoji VCD subtitle packetizer") ); set_capability( "packetizer", 50 ); set_callbacks( PacketizerOpen, DecoderClose );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/static subpicture_t *Decode( decoder_t *, block_t ** );static block_t *Packetize ( decoder_t *, block_t ** );static block_t *Reassemble ( decoder_t *, block_t * );static void ParseMetaInfo ( decoder_t *, block_t * );static void ParseHeader ( decoder_t *, block_t * );static subpicture_t *DecodePacket( decoder_t *, block_t * );static void RenderImage( decoder_t *, block_t *, subpicture_region_t * );#define SUBTITLE_BLOCK_EMPTY 0#define SUBTITLE_BLOCK_PARTIAL 1#define SUBTITLE_BLOCK_COMPLETE 2struct decoder_sys_t{ int b_packetizer; int i_state; /* data-gathering state for this subtitle */ block_t *p_spu; /* Bytes of the packet. */ int i_spu_size; /* goal for subtitle_data_pos while gathering, size of used subtitle_data later */ uint16_t i_image_offset; /* offset from subtitle_data to compressed image data */ int i_image_length; /* size of the compressed image data */ int first_field_offset; /* offset of even raster lines */ int second_field_offset; /* offset of odd raster lines */ int metadata_offset; /* offset to data describing the image */ int metadata_length; /* length of metadata */ mtime_t i_duration; /* how long to display the image, 0 stands for "until next subtitle" */ uint16_t i_x_start, i_y_start; /* position of top leftmost pixel of image when displayed */ uint16_t i_width, i_height; /* dimensions in pixels of image */ uint8_t p_palette[4][4]; /* Palette of colors used in subtitle */ uint8_t p_palette_highlight[4][4];};/***************************************************************************** * DecoderOpen: open/initialize the cvdsub decoder. *****************************************************************************/static int DecoderOpen( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t*)p_this; decoder_sys_t *p_sys; if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'c','v','d',' ' ) ) { return VLC_EGENERIC; } p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) ); p_sys->b_packetizer = VLC_FALSE; p_sys->i_state = SUBTITLE_BLOCK_EMPTY; p_sys->p_spu = NULL; es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'c','v','d',' ' ) ); p_dec->pf_decode_sub = Decode; p_dec->pf_packetize = Packetize; return VLC_SUCCESS;}/***************************************************************************** * PacketizerOpen: open/initialize the cvdsub packetizer. *****************************************************************************/static int PacketizerOpen( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t*)p_this; if( DecoderOpen( p_this ) != VLC_SUCCESS ) return VLC_EGENERIC; p_dec->p_sys->b_packetizer = VLC_TRUE; return VLC_SUCCESS;}/***************************************************************************** * DecoderClose: closes the cvdsub decoder/packetizer. *****************************************************************************/void DecoderClose( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t*)p_this; decoder_sys_t *p_sys = p_dec->p_sys; if( p_sys->p_spu ) block_ChainRelease( p_sys->p_spu ); free( p_sys );}/***************************************************************************** * Decode: *****************************************************************************/static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block ){ block_t *p_block, *p_spu; if( pp_block == NULL || *pp_block == NULL ) return NULL; p_block = *pp_block; *pp_block = NULL; if( !(p_spu = Reassemble( p_dec, p_block )) ) return NULL; /* Parse and decode */ return DecodePacket( p_dec, p_spu );}/***************************************************************************** * Packetize: *****************************************************************************/static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ){ block_t *p_block, *p_spu; if( pp_block == NULL || *pp_block == NULL ) return NULL; p_block = *pp_block; *pp_block = NULL; if( !(p_spu = Reassemble( p_dec, p_block )) ) return NULL; p_spu->i_dts = p_spu->i_pts; p_spu->i_length = 0; return p_spu;}/***************************************************************************** Reassemble: Data for single screen subtitle may come in several non-contiguous packets of a stream. This routine is called when the next packet in the stream comes in. The job of this routine is to parse the header, if this is the beginning, and combine the packets into one complete subtitle unit. If everything is complete, we will return a block. Otherwise return NULL. *****************************************************************************/#define SPU_HEADER_LEN 1static block_t *Reassemble( decoder_t *p_dec, block_t *p_block ){ decoder_sys_t *p_sys = p_dec->p_sys; uint8_t *p_buffer; if( p_block->i_buffer < SPU_HEADER_LEN ) { msg_Dbg( p_dec, "invalid packet header (size %d < %d)" , p_block->i_buffer, SPU_HEADER_LEN ); block_Release( p_block ); return NULL; } p_buffer = p_block->p_buffer; /* From the scant data on the format, there is only only way known * to detect the first packet in a subtitle. The first packet * seems to have a valid PTS while later packets for the same * image don't. */ if( p_sys->i_state == SUBTITLE_BLOCK_EMPTY && p_block->i_pts == 0 ) { msg_Warn( p_dec, "first packet expected but no PTS present"); return NULL; } p_block->p_buffer += SPU_HEADER_LEN; p_block->i_buffer -= SPU_HEADER_LEN; /* First packet in the subtitle block */ if( p_sys->i_state == SUBTITLE_BLOCK_EMPTY ) ParseHeader( p_dec, p_block ); block_ChainAppend( &p_sys->p_spu, p_block ); p_sys->p_spu = block_ChainGather( p_sys->p_spu ); if( p_sys->p_spu->i_buffer >= p_sys->i_spu_size ) { block_t *p_spu = p_sys->p_spu; if( p_spu->i_buffer != p_sys->i_spu_size ) { msg_Warn( p_dec, "SPU packets size=%d should be %d", p_spu->i_buffer, p_sys->i_spu_size ); } msg_Dbg( p_dec, "subtitle packet complete, size=%d", p_spu->i_buffer); ParseMetaInfo( p_dec, p_spu ); p_sys->i_state = SUBTITLE_BLOCK_EMPTY; p_sys->p_spu = 0; return p_spu; } else { /* Not last block in subtitle, so wait for another. */ p_sys->i_state = SUBTITLE_BLOCK_PARTIAL; } return NULL;}/* We do not have information on the subtitle format used on CVD's except the submux sample code and a couple of samples of dubious origin. Thus, this is the result of reading some code whose correctness is not known and some experimentation. CVD subtitles are different in several ways from SVCD OGT subtitles. Image comes first and metadata is at the end. So that the metadata can be found easily, the subtitle packet starts with two bytes (everything is big-endian again) that give the total size of the subtitle data and the offset to the metadata - i.e. size of the image data plus the four bytes at the beginning. Image data comes interlaced is run-length encoded. Each field is a four-bit nibble. Each nibble contains a two-bit repeat count and a two-bit color number so that up to three pixels can be described in four bits. The function of a 0 repeat count is unknown; it might be used for RLE extension. However when the full nibble is zero, the rest of the line is filled with the color value in the next nibble. It is unknown what happens if the color value is greater than three. The rest seems to use a 4-entries palette. It is not impossible that the fill-line complete case above is not as described and the zero repeat count means fill line. The sample code never produces this, so it may be untested.*/static void ParseHeader( decoder_t *p_dec, block_t *p_block ){ decoder_sys_t *p_sys = p_dec->p_sys; uint8_t *p = p_block->p_buffer; p_sys->i_spu_size = (p[0] << 8) + p[1] + 4; p += 2; /* FIXME: check data sanity */ p_sys->metadata_offset = (p[0] << 8) + p[1]; p +=2; p_sys->metadata_length = p_sys->i_spu_size - p_sys->metadata_offset;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -