📄 real.c
字号:
/***************************************************************************** * real.c: Real demuxer. ***************************************************************************** * Copyright (C) 2004, 2006 the VideoLAN team * $Id: real.c 16987 2006-10-08 12:54:12Z jpsaman $ * * Authors: 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <stdlib.h> /* malloc(), free() */#include <vlc/vlc.h>#include <vlc/input.h>#include "charset.h"/***************************************************************************** * Module descriptor *****************************************************************************/static int Open ( vlc_object_t * );static void Close ( vlc_object_t * );vlc_module_begin(); set_description( _("Real demuxer" ) ); set_capability( "demux2", 15 ); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_DEMUX ); set_callbacks( Open, Close ); add_shortcut( "real" ); add_shortcut( "rm" );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/typedef struct{ int i_id; es_format_t fmt; es_out_id_t *p_es; int i_frame; block_t *p_frame; int i_subpacket_h; int i_subpacket_size; int i_coded_frame_size; int i_frame_size; int i_subpacket; int i_subpackets; block_t **p_subpackets; int i_out_subpacket;} real_track_t;struct demux_sys_t{ int64_t i_data_offset; int64_t i_data_size; uint32_t i_data_packets_count; uint32_t i_data_packets; int64_t i_data_offset_next; int i_our_duration; int i_mux_rate; int i_track; real_track_t **track; uint8_t buffer[65536]; int64_t i_pcr; vlc_meta_t *p_meta;};static int Demux( demux_t *p_demux );static int Control( demux_t *p_demux, int i_query, va_list args );static int HeaderRead( demux_t *p_demux );static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );/***************************************************************************** * Open *****************************************************************************/static int Open( vlc_object_t *p_this ){ demux_t *p_demux = (demux_t*)p_this; demux_sys_t *p_sys; uint8_t *p_peek; if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC; if( strncmp( (char *)p_peek, ".RMF", 4 ) ) return VLC_EGENERIC; /* Fill p_demux field */ p_demux->pf_demux = Demux; p_demux->pf_control = Control; p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); memset( p_sys, 0, sizeof( demux_sys_t ) ); p_sys->i_data_offset = 0; p_sys->i_track = 0; p_sys->track = NULL; p_sys->i_pcr = 1; /* Parse the headers */ if( HeaderRead( p_demux ) ) { int i; msg_Err( p_demux, "invalid header" ); for( i = 0; i < p_sys->i_track; i++ ) { real_track_t *tk = p_sys->track[i]; if( tk->p_es ) { es_out_Del( p_demux->out, tk->p_es ); } free( tk ); } if( p_sys->i_track > 0 ) { free( p_sys->track ); } free( p_sys ); return VLC_EGENERIC; } return VLC_SUCCESS;}/***************************************************************************** * Close *****************************************************************************/static void Close( vlc_object_t *p_this ){ demux_t *p_demux = (demux_t*)p_this; demux_sys_t *p_sys = p_demux->p_sys; int i; for( i = 0; i < p_sys->i_track; i++ ) { real_track_t *tk = p_sys->track[i]; int j = tk->i_subpackets; if( tk->p_frame ) block_Release( tk->p_frame ); es_format_Clean( &tk->fmt ); while( j-- ) { if( tk->p_subpackets[ j ] ) block_Release( tk->p_subpackets[ j ] ); } if( tk->i_subpackets ) free( tk->p_subpackets ); free( tk ); } if( p_sys->i_track > 0 ) free( p_sys->track ); free( p_sys );}/***************************************************************************** * Demux: *****************************************************************************/static int Demux( demux_t *p_demux ){ demux_sys_t *p_sys = p_demux->p_sys; uint8_t header[18]; int i_size, i_id, i_flags, i; int64_t i_pts; real_track_t *tk = NULL; vlc_bool_t b_selected; if( p_sys->i_data_packets >= p_sys->i_data_packets_count && p_sys->i_data_packets_count ) { if( stream_Read( p_demux->s, header, 18 ) < 18 ) { return 0; } if( strncmp( (char *)header, "DATA", 4 ) ) { return 0; } p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18; p_sys->i_data_size = GetDWBE( &header[4] ); p_sys->i_data_packets_count = GetDWBE( &header[10] ); p_sys->i_data_packets = 0; p_sys->i_data_offset_next = GetDWBE( &header[14] ); msg_Dbg( p_demux, "entering new DATA packets=%d next=%u", p_sys->i_data_packets_count, (uint32_t)p_sys->i_data_offset_next ); } if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0; i_size = GetWBE( &header[2] ) - 12; i_id = GetWBE( &header[4] ); i_pts = 1000 * GetDWBE( &header[6] ); i_pts += 1000; /* Avoid 0 pts */ i_flags= header[11]; /* flags 0x02 -> keyframe */#if 0 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u", p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );#endif p_sys->i_data_packets++; stream_Read( p_demux->s, p_sys->buffer, i_size ); for( i = 0; i < p_sys->i_track; i++ ) { if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i]; } if( tk == NULL ) { msg_Warn( p_demux, "unknown track id(0x%x)", i_id ); return 1; } es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected ); if( tk->fmt.i_cat == VIDEO_ES && b_selected ) { uint8_t *p = p_sys->buffer; while( p < &p_sys->buffer[i_size - 2] ) { uint8_t h = *p++; int i_len = 0; int i_copy; int i_subseq = 0; int i_seqnum = 0; int i_offset = 0; if( (h&0xc0) == 0x40 ) { /* Short header */ p++; i_len = &p_sys->buffer[i_size] - p; } else { if( (h&0x40) == 0 ) { i_subseq = (*p++)&0x7f; } i_len = (p[0] << 8)|p[1]; p += 2; if( (i_len&0xc000) == 0 ) { i_len <<= 16; i_len |= (p[0] << 8)|p[1]; p += 2; i_len &= 0x3fffffff; } else { i_len &= 0x3fff; } i_offset = (p[0] << 8)|p[1]; p += 2; if( (i_offset&0xc000) == 0 ) { i_offset <<= 16; i_offset |= (p[0] << 8)|p[1]; p += 2; i_offset &= 0x3fffffff; } else { i_offset &= 0x3fff; } i_seqnum = *p++; } i_copy = i_len - i_offset; if( i_copy > &p_sys->buffer[i_size] - p ) { i_copy = &p_sys->buffer[i_size] - p; } else if( i_copy < 0 ) { break; } msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d", i_len, i_offset, i_copy, i_subseq, i_seqnum ); if( (h&0xc0) == 0x80 ) { /* last fragment -> fixes */ i_copy = i_offset; i_offset = i_len - i_copy; msg_Dbg( p_demux, "last fixing copy=%d offset=%d", i_copy, i_offset ); } if( tk->p_frame && ( tk->p_frame->i_dts != i_pts || tk->i_frame != i_len ) ) { msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer ); if( p_sys->i_pcr < tk->p_frame->i_dts ) { p_sys->i_pcr = tk->p_frame->i_dts; es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr ); } es_out_Send( p_demux->out, tk->p_es, tk->p_frame ); tk->i_frame = 0; tk->p_frame = NULL; } if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame ) { /* no fragment */ i_len = i_copy; i_offset = 0; } if( tk->p_frame == NULL ) { msg_Dbg( p_demux, "new frame size=%d", i_len ); tk->i_frame = i_len; if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) ) { return -1; } memset( &tk->p_frame->p_buffer[8], 0, i_len ); tk->p_frame->i_dts = i_pts; tk->p_frame->i_pts = i_pts; ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */ ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */ } if( i_offset < tk->i_frame) { int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++; msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d", i_ck, i_offset, i_copy );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -