📄 ts.c
字号:
pid->es->p_pes = NULL; pid->es->i_pes_size= 0; pid->es->i_pes_gathered= 0; pid->es->pp_last = &pid->es->p_pes; /* FIXME find real max size */ i_max = block_ChainExtract( p_pes, header, 30 ); if( header[0] != 0 || header[1] != 0 || header[2] != 1 ) { if( !p_demux->p_sys->b_silent ) msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)", header[0], header[1],header[2],header[3], pid->i_pid ); block_ChainRelease( p_pes ); return; } /* TODO check size */ switch( header[3] ) { case 0xBC: /* Program stream map */ case 0xBE: /* Padding */ case 0xBF: /* Private stream 2 */ case 0xF0: /* ECM */ case 0xF1: /* EMM */ case 0xFF: /* Program stream directory */ case 0xF2: /* DSMCC stream */ case 0xF8: /* ITU-T H.222.1 type E stream */ i_skip = 6; break; default: if( ( header[6]&0xC0 ) == 0x80 ) { /* mpeg2 PES */ i_skip = header[8] + 9; if( header[7]&0x80 ) /* has pts */ { i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)| (mtime_t)(header[10] << 22)| ((mtime_t)(header[11]&0xfe) << 14)| (mtime_t)(header[12] << 7)| (mtime_t)(header[13] >> 1); if( header[7]&0x40 ) /* has dts */ { i_dts = ((mtime_t)(header[14]&0x0e ) << 29)| (mtime_t)(header[15] << 22)| ((mtime_t)(header[16]&0xfe) << 14)| (mtime_t)(header[17] << 7)| (mtime_t)(header[18] >> 1); } } } else { i_skip = 6; while( i_skip < 23 && header[i_skip] == 0xff ) { i_skip++; } if( i_skip == 23 ) { msg_Err( p_demux, "too much MPEG-1 stuffing" ); block_ChainRelease( p_pes ); return; } if( ( header[i_skip] & 0xC0 ) == 0x40 ) { i_skip += 2; } if( header[i_skip]&0x20 ) { i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)| (mtime_t)(header[i_skip+1] << 22)| ((mtime_t)(header[i_skip+2]&0xfe) << 14)| (mtime_t)(header[i_skip+3] << 7)| (mtime_t)(header[i_skip+4] >> 1); if( header[i_skip]&0x10 ) /* has dts */ { i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)| (mtime_t)(header[i_skip+6] << 22)| ((mtime_t)(header[i_skip+7]&0xfe) << 14)| (mtime_t)(header[i_skip+8] << 7)| (mtime_t)(header[i_skip+9] >> 1); i_skip += 10; } else { i_skip += 5; } } else { i_skip += 1; } } break; } if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) || pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) ) { i_skip += 4; } else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) || pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) || pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) ) { i_skip += 1; } else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) && pid->es->p_mpeg4desc ) { decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr; if( dcd->i_decoder_specific_info_len > 2 && dcd->p_decoder_specific_info[0] == 0x10 && ( dcd->p_decoder_specific_info[1]&0x10 ) ) { /* display length */ if( p_pes->i_buffer + 2 <= i_skip ) { i_length = GetWBE( &p_pes->p_buffer[i_skip] ); } i_skip += 2; } if( p_pes->i_buffer + 2 <= i_skip ) { i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] ); } /* */ i_skip += 2; }#ifdef ZVBI_COMPILED else if( pid->es->fmt.i_codec == VLC_FOURCC( 't', 'e', 'l', 'x' ) ) i_skip = 0; /*hack for zvbi support */#endif /* skip header */ while( p_pes && i_skip > 0 ) { if( p_pes->i_buffer <= i_skip ) { block_t *p_next = p_pes->p_next; i_skip -= p_pes->i_buffer; block_Release( p_pes ); p_pes = p_next; } else { p_pes->i_buffer -= i_skip; p_pes->p_buffer += i_skip; break; } } /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */ if( i_pts >= 0 && i_dts < 0 ) i_dts = i_pts; if( p_pes ) { block_t *p_block; int i; if( i_dts >= 0 ) { p_pes->i_dts = i_dts * 100 / 9; } if( i_pts >= 0 ) { p_pes->i_pts = i_pts * 100 / 9; } p_pes->i_length = i_length * 100 / 9; p_block = block_ChainGather( p_pes ); if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) ) { if( i_pes_size > 0 && p_block->i_buffer > i_pes_size ) { p_block->i_buffer = i_pes_size; } /* Append a \0 */ p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 ); p_block->p_buffer[p_block->i_buffer -1] = '\0'; } for( i = 0; i < pid->i_extra_es; i++ ) { es_out_Send( p_demux->out, pid->extra_es[i]->id, block_Duplicate( p_block ) ); } es_out_Send( p_demux->out, pid->es->id, p_block ); } else { msg_Warn( p_demux, "empty pes" ); }}static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk ){ demux_sys_t *p_sys = p_demux->p_sys; const uint8_t *p = p_bk->p_buffer; if( ( p[3]&0x20 ) && /* adaptation */ ( p[5]&0x10 ) && ( p[4] >= 7 ) ) { int i; mtime_t i_pcr; /* 33 bits */ i_pcr = ( (mtime_t)p[6] << 25 ) | ( (mtime_t)p[7] << 17 ) | ( (mtime_t)p[8] << 9 ) | ( (mtime_t)p[9] << 1 ) | ( (mtime_t)p[10] >> 7 ); /* Search program and set the PCR */ for( i = 0; i < p_sys->i_pmt; i++ ) { int i_prg; for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ ) { if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr ) { es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR, (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number, (int64_t)(i_pcr * 100 / 9) ); } } } }}static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk ){ const uint8_t *p = p_bk->p_buffer; const bool b_unit_start = p[1]&0x40; const bool b_adaptation = p[3]&0x20; const bool b_payload = p[3]&0x10; const int i_cc = p[3]&0x0f; /* continuity counter */ bool b_discontinuity = false;/* discontinuity */ /* transport_scrambling_control is ignored */ int i_skip = 0; bool i_ret = false; int i_diff;#if 0 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d " "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation, b_payload, i_cc );#endif /* For now, ignore additional error correction * TODO: handle Reed-Solomon 204,188 error correction */ p_bk->i_buffer = TS_PACKET_SIZE_188; if( p[1]&0x80 ) { msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)", pid->i_pid ); if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES ) pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED; } if( p_demux->p_sys->csa ) { vlc_mutex_lock( &p_demux->p_sys->csa_lock ); csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size ); vlc_mutex_unlock( &p_demux->p_sys->csa_lock ); } if( !b_adaptation ) { /* We don't have any adaptation_field, so payload starts * immediately after the 4 byte TS header */ i_skip = 4; } else { /* p[4] is adaptation_field_length minus one */ i_skip = 5 + p[4]; if( p[4] > 0 ) { /* discontinuity indicator found in stream */ b_discontinuity = (p[5]&0x80) ? true : false; if( b_discontinuity && pid->es->p_pes ) { msg_Warn( p_demux, "discontinuity indicator (pid=%d) ", pid->i_pid ); /* pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY; */ }#if 0 if( p[5]&0x40 ) msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );#endif } } /* Test continuity counter */ /* continuous when (one of this): * diff == 1 * diff == 0 and payload == 0 * diff == 0 and duplicate packet (playload != 0) <- should we * test the content ? */ i_diff = ( i_cc - pid->i_cc )&0x0f; if( b_payload && i_diff == 1 ) { pid->i_cc = ( pid->i_cc + 1 ) & 0xf; } else { if( pid->i_cc == 0xff ) { msg_Warn( p_demux, "first packet for pid=%d cc=0x%x", pid->i_pid, i_cc ); pid->i_cc = i_cc; } else if( i_diff != 0 && !b_discontinuity ) { msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)", i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid ); pid->i_cc = i_cc; if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES ) { /* Small video artifacts are usually better then * dropping full frames */ pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED; } } } PCRHandle( p_demux, pid, p_bk ); if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out ) { block_Release( p_bk ); return i_ret; } /* We have to gather it */ p_bk->p_buffer += i_skip; p_bk->i_buffer -= i_skip; if( b_unit_start ) { if( pid->es->p_pes ) { ParsePES( p_demux, pid ); i_ret = true; } block_ChainLastAppend( &pid->es->pp_last, p_bk ); if( p_bk->i_buffer > 6 ) { pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] ); if( pid->es->i_pes_size > 0 ) { pid->es->i_pes_size += 6; } } pid->es->i_pes_gathered += p_bk->i_buffer; if( pid->es->i_pes_size > 0 && pid->es->i_pes_gathered >= pid->es->i_pes_size ) { ParsePES( p_demux, pid ); i_ret = true; } } else { if( pid->es->p_pes == NULL ) { /* msg_Dbg( p_demux, "broken packet" ); */ block_Release( p_bk ); } else { block_ChainLastAppend( &pid->es->pp_last, p_bk ); pid->es->i_pes_gathered += p_bk->i_buffer; if( pid->es->i_pes_size > 0 && pid->es->i_pes_gathered >= pid->es->i_pes_size ) { ParsePES( p_demux, pid ); i_ret = true; } } } return i_ret;}static int PIDFillFormat( ts_pid_t *pid, int i_stream_type ){ es_format_t *fmt = &pid->es->fmt; switch( i_stream_type ) { ca
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -