📄 vc1.c
字号:
/***************************************************************************** * vc1.c ***************************************************************************** * Copyright (C) 2001, 2002, 2006 the VideoLAN team * $Id$ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * Gildas Bazin <gbazin@videolan.org> * * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <vlc_common.h>#include <vlc_plugin.h>#include <vlc_codec.h>#include <vlc_block.h>#include "vlc_bits.h"#include "vlc_block_helper.h"/***************************************************************************** * Module descriptor *****************************************************************************/static int Open ( vlc_object_t * );static void Close( vlc_object_t * );vlc_module_begin(); set_category( CAT_SOUT ); set_subcategory( SUBCAT_SOUT_PACKETIZER ); set_description( N_("VC-1 packetizer") ); set_capability( "packetizer", 50 ); set_callbacks( Open, Close );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/struct decoder_sys_t{ /* * Input properties */ block_bytestream_t bytestream; int i_state; size_t i_offset; uint8_t p_startcode[3]; /* Current sequence header */ bool b_sequence_header; struct { block_t *p_sh; bool b_advanced_profile; bool b_interlaced; bool b_frame_interpolation; bool b_range_reduction; bool b_has_bframe; } sh; bool b_entry_point; struct { block_t *p_ep; } ep; /* */ bool b_frame; /* Current frame being built */ block_t *p_frame; block_t **pp_last; mtime_t i_interpolated_dts;};enum{ STATE_NOSYNC, STATE_NEXT_SYNC};typedef enum{ IDU_TYPE_SEQUENCE_HEADER = 0x0f, IDU_TYPE_ENTRY_POINT = 0x0e, IDU_TYPE_FRAME = 0x0D, IDU_TYPE_FIELD = 0x0C, IDU_TYPE_SLICE = 0x0B, IDU_TYPE_END_OF_SEQUENCE = 0x0B, IDU_TYPE_SEQUENCE_LEVEL_USER_DATA = 0x1F, IDU_TYPE_ENTRY_POINT_USER_DATA = 0x1E, IDU_TYPE_FRAME_USER_DATA = 0x1D, IDU_TYPE_FIELD_USER_DATA = 0x1C, IDU_TYPE_SLICE_USER_DATA = 0x1B,} idu_type_t;static block_t *Packetize( decoder_t *p_dec, block_t **pp_block );/***************************************************************************** * Open: probe the packetizer and return score ***************************************************************************** * Tries to launch a decoder and return score so that the interface is able * to choose. *****************************************************************************/static int Open( 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( 'W', 'V', 'C', '1' ) ) return VLC_EGENERIC; p_dec->pf_packetize = Packetize; /* Create the output format */ es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in ); p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) ); p_sys->i_state = STATE_NOSYNC; p_sys->bytestream = block_BytestreamInit(); p_sys->p_startcode[0] = 0x00; p_sys->p_startcode[1] = 0x00; p_sys->p_startcode[2] = 0x01; p_sys->i_offset = 0; p_sys->b_sequence_header = false; p_sys->sh.p_sh = NULL; p_sys->b_entry_point = false; p_sys->ep.p_ep = NULL; p_sys->b_frame = false; p_sys->p_frame = NULL; p_sys->pp_last = &p_sys->p_frame; p_sys->i_interpolated_dts = -1; if( p_dec->fmt_in.i_extra > 0 ) { block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra ); block_t *p_pic; memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ); while( ( p_pic = Packetize( p_dec, &p_init ) ) ) block_Release( p_pic ); /* Should not happen (only sequence header) */ } return VLC_SUCCESS;}/***************************************************************************** * Close: *****************************************************************************/static void Close( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t*)p_this; decoder_sys_t *p_sys = p_dec->p_sys; block_BytestreamRelease( &p_sys->bytestream ); if( p_sys->p_frame ) block_Release( p_sys->p_frame ); free( p_sys );}/***************************************************************************** * Packetize: packetize an access unit *****************************************************************************/static block_t *ParseIDU( decoder_t *p_dec, block_t *p_frag );static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ){ decoder_sys_t *p_sys = p_dec->p_sys; block_t *p_pic; if( pp_block == NULL || *pp_block == NULL ) return NULL; if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED ) { p_sys->i_state = STATE_NOSYNC; block_BytestreamFlush( &p_sys->bytestream ); if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame ); p_sys->p_frame = NULL; p_sys->pp_last = &p_sys->p_frame; p_sys->b_frame = false; }// p_sys->i_interpolated_dts = 0; block_Release( *pp_block ); return NULL; } block_BytestreamPush( &p_sys->bytestream, *pp_block ); for( ;; ) { switch( p_sys->i_state ) { case STATE_NOSYNC: if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS ) p_sys->i_state = STATE_NEXT_SYNC; if( p_sys->i_offset ) { block_SkipBytes( &p_sys->bytestream, p_sys->i_offset ); p_sys->i_offset = 0; block_BytestreamFlush( &p_sys->bytestream ); } if( p_sys->i_state != STATE_NEXT_SYNC ) return NULL; /* Need more data */ p_sys->i_offset = 4; /* To find next startcode */ case STATE_NEXT_SYNC: /* TODO: If p_block == NULL, flush the buffer without checking the * next sync word */ /* Find the next startcode */ if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS ) return NULL; /* Need more data */ /* Get the new fragment and set the pts/dts */ p_pic = block_New( p_dec, p_sys->i_offset ); block_BytestreamFlush( &p_sys->bytestream ); p_pic->i_pts = p_sys->bytestream.p_block->i_pts; p_pic->i_dts = p_sys->bytestream.p_block->i_dts; p_pic->i_rate = p_sys->bytestream.p_block->i_rate; block_GetBytes( &p_sys->bytestream, p_pic->p_buffer, p_pic->i_buffer ); p_sys->i_offset = 0; /* Parse and return complete frame */ p_pic = ParseIDU( p_dec, p_pic ); /* Don't reuse the same timestamps several times */ if( p_sys->p_frame && p_sys->p_frame->i_dts == p_sys->bytestream.p_block->i_dts && p_sys->p_frame->i_pts == p_sys->bytestream.p_block->i_pts ) { p_sys->bytestream.p_block->i_pts = -1; p_sys->bytestream.p_block->i_dts = -1; } /* */ if( !p_pic ) { p_sys->i_state = STATE_NOSYNC; break; } /* */ if( p_sys->i_interpolated_dts < 0 ) { msg_Dbg( p_dec, "need a starting pts/dts" ); p_sys->i_state = STATE_NOSYNC; block_Release( p_pic ); break; } /* So p_block doesn't get re-added several times */ *pp_block = block_BytestreamPop( &p_sys->bytestream ); p_sys->i_state = STATE_NOSYNC; return p_pic; } }}/* DecodeRIDU: decode the startcode emulation prevention (same than h264) */static void DecodeRIDU( uint8_t *p_ret, int *pi_ret, uint8_t *src, int i_src ){ uint8_t *end = &src[i_src]; uint8_t *dst_end = &p_ret[*pi_ret]; uint8_t *dst = p_ret; while( src < end && dst < dst_end ) { if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 ) { *dst++ = 0x00; *dst++ = 0x00; src += 3; continue; } *dst++ = *src++; } *pi_ret = dst - p_ret;}/* BuildExtraData: gather sequence header and entry point */static void BuildExtraData( decoder_t *p_dec ){ decoder_sys_t *p_sys = p_dec->p_sys; es_format_t *p_es = &p_dec->fmt_out; int i_extra; if( !p_sys->b_sequence_header || !p_sys->b_entry_point ) return; i_extra = p_sys->sh.p_sh->i_buffer + p_sys->ep.p_ep->i_buffer; if( p_es->i_extra != i_extra ) { p_es->i_extra = i_extra; p_es->p_extra = realloc( p_dec->fmt_out.p_extra, p_es->i_extra ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -