Commit 706083eb authored by François Cartegnie's avatar François Cartegnie 🤞

demux: factorize PES header stuff

parent aa9976b1
......@@ -295,7 +295,7 @@ endif
EXTRA_LTLIBRARIES += libvcdx_plugin.la
access_LTLIBRARIES += $(LTLIBvcdx)
libdvdnav_plugin_la_SOURCES = access/dvdnav.c
libdvdnav_plugin_la_SOURCES = access/dvdnav.c ../demux/mpeg/ps.h ../demux/mpeg/pes.h
libdvdnav_plugin_la_CFLAGS = $(AM_CFLAGS) $(DVDNAV_CFLAGS)
libdvdnav_plugin_la_LIBADD = $(DVDNAV_LIBS)
libdvdnav_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(accessdir)'
......@@ -305,7 +305,7 @@ endif
access_LTLIBRARIES += $(LTLIBdvdnav)
EXTRA_LTLIBRARIES += libdvdnav_plugin.la
libdvdread_plugin_la_SOURCES = access/dvdread.c
libdvdread_plugin_la_SOURCES = access/dvdread.c ../demux/mpeg/ps.h ../demux/mpeg/pes.h
libdvdread_plugin_la_CFLAGS = $(AM_CFLAGS) $(DVDREAD_CFLAGS)
libdvdread_plugin_la_LIBADD = $(DVDREAD_LIBS)
libdvdread_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(accessdir)'
......
......@@ -60,6 +60,7 @@
#include <dvdnav/dvdnav.h>
#include "../demux/mpeg/pes.h"
#include "../demux/mpeg/ps.h"
/*****************************************************************************
......@@ -1353,7 +1354,7 @@ static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
ESNew( p_demux, i_id );
}
if( tk->b_seen && tk->es &&
!ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
!ps_pkt_parse_pes( p_demux, p_pkt, tk->i_skip ) )
{
es_out_Send( p_demux->out, tk->es, p_pkt );
}
......
......@@ -48,6 +48,7 @@
#include <vlc_iso_lang.h>
#include "../demux/mpeg/pes.h"
#include "../demux/mpeg/ps.h"
#include <sys/types.h>
......@@ -615,7 +616,7 @@ static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
ESNew( p_demux, i_id, 0 );
}
if( tk->b_seen && tk->es &&
!ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
!ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) )
{
es_out_Send( p_demux->out, tk->es, p_pkt );
}
......
......@@ -37,7 +37,7 @@ demux_LTLIBRARIES += libnsv_plugin.la
libreal_plugin_la_SOURCES = demux/real.c
demux_LTLIBRARIES += libreal_plugin.la
libps_plugin_la_SOURCES = demux/mpeg/ps.c demux/mpeg/ps.h
libps_plugin_la_SOURCES = demux/mpeg/ps.c demux/mpeg/ps.h demux/mpeg/pes.h
demux_LTLIBRARIES += libps_plugin.la
libmod_plugin_la_SOURCES = demux/mod.c
......@@ -63,7 +63,8 @@ demux_LTLIBRARIES += libsubtitle_plugin.la
libty_plugin_la_SOURCES = demux/ty.c codec/cc.h
demux_LTLIBRARIES += libty_plugin.la
libvobsub_plugin_la_SOURCES = demux/vobsub.c demux/vobsub.h
libvobsub_plugin_la_SOURCES = demux/vobsub.c demux/vobsub.h \
demux/mpeg/ps.h demux/mpeg/pes.h
demux_LTLIBRARIES += libvobsub_plugin.la
libvoc_plugin_la_SOURCES = demux/voc.c
......@@ -228,6 +229,7 @@ demux_LTLIBRARIES += libplaylist_plugin.la
libts_plugin_la_SOURCES = demux/mpeg/ts.c \
demux/mpeg/mpeg4_iod.c demux/mpeg/mpeg4_iod.h \
demux/mpeg/pes.h \
mux/mpeg/csa.c mux/mpeg/dvbpsi_compat.h \
mux/mpeg/streams.h mux/mpeg/tables.c mux/mpeg/tables.h \
mux/mpeg/tsutil.c mux/mpeg/tsutil.h \
......
/*****************************************************************************
* pes.h: PES Packet helpers
*****************************************************************************
* Copyright (C) 2004-2015 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*****************************************************************************/
#define FROM_SCALE_NZ(x) ((x) * 100 / 9)
#define TO_SCALE_NZ(x) ((x) * 9 / 100)
#define FROM_SCALE(x) (VLC_TS_0 + FROM_SCALE_NZ(x))
#define TO_SCALE(x) TO_SCALE_NZ((x) - VLC_TS_0)
static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
{
return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
(mtime_t)(p_data[1] << 22)|
((mtime_t)(p_data[2]&0xfe) << 14)|
(mtime_t)(p_data[3] << 7)|
(mtime_t)(p_data[4] >> 1);
}
static inline mtime_t ExtractMPEG1PESTimestamp( const uint8_t *p_data )
{
return ((mtime_t)(p_data[ 0]&0x38 ) << 27)|
((mtime_t)(p_data[0]&0x03 ) << 28)|
(mtime_t)(p_data[1] << 20)|
((mtime_t)(p_data[2]&0xf8 ) << 12)|
((mtime_t)(p_data[2]&0x03 ) << 13)|
(mtime_t)(p_data[3] << 5) |
(mtime_t)(p_data[4] >> 3);
}
static int ParsePESHeader( vlc_object_t *p_object, const uint8_t *p_header, size_t i_header,
unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts,
uint8_t *pi_stream_id )
{
unsigned i_skip;
if ( i_header < 9 )
return VLC_EGENERIC;
*pi_stream_id = p_header[3];
switch( p_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( ( p_header[6]&0xC0 ) == 0x80 )
{
/* mpeg2 PES */
i_skip = p_header[8] + 9;
if( p_header[7]&0x80 ) /* has pts */
{
if( i_header < 9 + 5 )
return VLC_EGENERIC;
*pi_pts = ExtractPESTimestamp( &p_header[9] );
if( p_header[7]&0x40 ) /* has dts */
{
if( i_header < 14 + 5 )
return VLC_EGENERIC;
*pi_dts = ExtractPESTimestamp( &p_header[14] );
}
}
}
else
{
i_skip = 6;
if( i_header < i_skip + 1 )
return VLC_EGENERIC;
while( i_skip < 23 && p_header[i_skip] == 0xff )
{
i_skip++;
if( i_header < i_skip + 1 )
return VLC_EGENERIC;
}
if( i_skip == 23 )
{
msg_Err( p_object, "too much MPEG-1 stuffing" );
return VLC_EGENERIC;
}
if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
{
i_skip += 2;
}
if( i_header < i_skip + 1 )
return VLC_EGENERIC;
if( p_header[i_skip]&0x20 )
{
if( i_header < i_skip + 5 )
return VLC_EGENERIC;
*pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
if( p_header[i_skip]&0x10 ) /* has dts */
{
if( i_header < i_skip + 10 )
return VLC_EGENERIC;
*pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
i_skip += 10;
}
else
{
i_skip += 5;
}
}
else
{
i_skip += 1;
}
}
break;
}
*pi_skip = i_skip;
return VLC_SUCCESS;
}
......@@ -33,6 +33,7 @@
#include <vlc_plugin.h>
#include <vlc_demux.h>
#include "pes.h"
#include "ps.h"
/* TODO:
......@@ -221,7 +222,8 @@ static int Demux2( demux_t *p_demux, bool b_end )
if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
{
ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
if( !ps_pkt_parse_pes( p_pkt, tk->i_skip ) && p_pkt->i_pts > VLC_TS_INVALID )
if( !ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) &&
p_pkt->i_pts > VLC_TS_INVALID )
{
if( b_end && p_pkt->i_pts > tk->i_last_pts )
{
......@@ -408,7 +410,7 @@ static int Demux( demux_t *p_demux )
p_sys->i_scr = -1;
if( tk->b_seen && tk->es &&
!ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
!ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) )
{
if( !b_new && !p_sys->b_have_pack &&
(tk->fmt.i_cat == AUDIO_ES) &&
......
......@@ -364,24 +364,12 @@ static inline int ps_pkt_parse_pack( block_t *p_pkt, int64_t *pi_scr,
uint8_t *p = p_pkt->p_buffer;
if( p_pkt->i_buffer >= 14 && (p[4] >> 6) == 0x01 )
{
*pi_scr =((((int64_t)p[4]&0x38) << 27 )|
(((int64_t)p[4]&0x03) << 28 )|
((int64_t)p[5] << 20 )|
(((int64_t)p[6]&0xf8) << 12 )|
(((int64_t)p[6]&0x03) << 13 )|
((int64_t)p[7] << 5 )|
((int64_t)p[8] >> 3 )) * 100 / 9;
*pi_scr = FROM_SCALE_NZ( ExtractMPEG1PESTimestamp( &p[4] ) );
*pi_mux_rate = ( p[10] << 14 )|( p[11] << 6 )|( p[12] >> 2);
}
else if( p_pkt->i_buffer >= 12 && (p[4] >> 4) == 0x02 )
{
*pi_scr =((((int64_t)p[4]&0x0e) << 29 )|
((int64_t)p[5] << 22 )|
(((int64_t)p[6]&0xfe) << 14 )|
((int64_t)p[7] << 7 )|
((int64_t)p[8] >> 1 )) * 100 / 9;
*pi_scr = FROM_SCALE_NZ( ExtractPESTimestamp( &p[4] ) );
*pi_mux_rate = ( ( p[9]&0x7f )<< 15 )|( p[10] << 7 )|( p[11] >> 1);
}
else
......@@ -424,97 +412,16 @@ static inline int ps_pkt_parse_system( block_t *p_pkt, ps_psm_t *p_psm,
}
/* Parse a PES (and skip i_skip_extra in the payload) */
static inline int ps_pkt_parse_pes( block_t *p_pes, int i_skip_extra )
static inline int ps_pkt_parse_pes( vlc_object_t *p_object, block_t *p_pes, int i_skip_extra )
{
uint8_t header[34];
unsigned int i_skip = 0;
int64_t i_pts = -1;
int64_t i_dts = -1;
memcpy( header, p_pes->p_buffer, __MIN( p_pes->i_buffer, 34 ) );
switch( header[3] )
{
case 0xBC: /* Program stream map */
case 0xBE: /* Padding */
case 0xBF: /* Private stream 2 */
case 0xB0: /* ECM */
case 0xB1: /* 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" ); */
return VLC_EGENERIC;
}
if( ( header[i_skip] & 0xC0 ) == 0x40 )
{
i_skip += 2;
}
mtime_t i_pts = -1;
mtime_t i_dts = -1;
uint8_t i_stream_id = 0;
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;
}
}
}
if( ParsePESHeader( p_object, p_pes->p_buffer, p_pes->i_buffer,
&i_skip, &i_dts, &i_pts, &i_stream_id ) != VLC_SUCCESS )
return VLC_EGENERIC;
if( i_skip_extra >= 0 )
i_skip += i_skip_extra;
......@@ -531,9 +438,9 @@ static inline int ps_pkt_parse_pes( block_t *p_pes, int i_skip_extra )
p_pes->i_buffer -= i_skip;
if( i_dts >= 0 )
p_pes->i_dts = VLC_TS_0 + 100 * i_dts / 9;
p_pes->i_dts = FROM_SCALE( i_dts );
if( i_pts >= 0 )
p_pes->i_pts = VLC_TS_0 + 100 * i_pts / 9;
p_pes->i_pts = FROM_SCALE( i_pts );
return VLC_SUCCESS;
}
......
......@@ -70,6 +70,7 @@
#include "../opus.h"
#include "pes.h"
#include "mpeg4_iod.h"
#ifdef HAVE_ARIBB24
......@@ -342,9 +343,6 @@ typedef struct
#define MAX_ES_PID 8190
#define MIN_PAT_INTERVAL CLOCK_FREQ // DVB is 500ms
#define FROM_SCALE(x) (VLC_TS_0 + ((x) * 100 / 9))
#define TO_SCALE(x) (((x) - VLC_TS_0) * 9 / 100)
#define PID_ALLOC_CHUNK 16
struct demux_sys_t
......@@ -652,15 +650,6 @@ static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, unsigned *pi_header_
return DetectPacketSize( p_demux, pi_header_size, 0 );
}
static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
{
return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
(mtime_t)(p_data[1] << 22)|
((mtime_t)(p_data[2]&0xfe) << 14)|
(mtime_t)(p_data[3] << 7)|
(mtime_t)(p_data[4] >> 1);
}
static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, size_t i_data, bool b_adaptfield )
{
demux_sys_t *p_sys = p_demux->p_sys;
......@@ -2063,103 +2052,6 @@ static block_t *Opus_Parse(demux_t *demux, block_t *block)
/****************************************************************************
* gathering stuff
****************************************************************************/
static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header, size_t i_header,
unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts,
uint8_t *pi_stream_id )
{
unsigned i_skip;
if ( i_header < 9 )
return VLC_EGENERIC;
*pi_stream_id = p_header[3];
switch( p_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( ( p_header[6]&0xC0 ) == 0x80 )
{
/* mpeg2 PES */
i_skip = p_header[8] + 9;
if( p_header[7]&0x80 ) /* has pts */
{
if( i_header < 9 + 5 )
return VLC_EGENERIC;
*pi_pts = ExtractPESTimestamp( &p_header[9] );
if( p_header[7]&0x40 ) /* has dts */
{
if( i_header < 14 + 5 )
return VLC_EGENERIC;
*pi_dts = ExtractPESTimestamp( &p_header[14] );
}
}
}
else
{
i_skip = 6;
if( i_header < i_skip + 1 )
return VLC_EGENERIC;
while( i_skip < 23 && p_header[i_skip] == 0xff )
{
i_skip++;
if( i_header < i_skip + 1 )
return VLC_EGENERIC;
}
if( i_skip == 23 )
{
msg_Err( p_demux, "too much MPEG-1 stuffing" );
return VLC_EGENERIC;
}
if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
{
i_skip += 2;
}
if( i_header < i_skip + 1 )
return VLC_EGENERIC;
if( p_header[i_skip]&0x20 )
{
if( i_header < i_skip + 5 )
return VLC_EGENERIC;
*pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
if( p_header[i_skip]&0x10 ) /* has dts */
{
if( i_header < i_skip + 10 )
return VLC_EGENERIC;
*pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
i_skip += 10;
}
else
{
i_skip += 5;
}
}
else
{
i_skip += 1;
}
}
break;
}
*pi_skip = i_skip;
return VLC_SUCCESS;
}
static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
{
uint8_t header[34];
......@@ -2190,7 +2082,7 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
return;
}
if( ParsePESHeader( p_demux, (uint8_t*)&header, i_max, &i_skip,
if( ParsePESHeader( VLC_OBJECT(p_demux), (uint8_t*)&header, i_max, &i_skip,
&i_dts, &i_pts, &i_stream_id ) == VLC_EGENERIC )
{
block_ChainRelease( p_pes );
......@@ -2832,7 +2724,7 @@ static int SeekToTime( demux_t *p_demux, ts_pmt_t *p_pmt, int64_t i_scaledtime )
mtime_t i_dts = -1;
mtime_t i_pts = -1;
uint8_t i_stream_id;
if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
if ( VLC_SUCCESS == ParsePESHeader( VLC_OBJECT(p_demux), &p_pkt->p_buffer[i_skip],
p_pkt->i_buffer - i_skip, &i_skip,
&i_dts, &i_pts, &i_stream_id ) )
{
......@@ -2980,7 +2872,7 @@ static int ProbeChunk( demux_t *p_demux, int i_program, bool b_end, int64_t *pi_
if ( b_adaptfield ) // adaptation field
i_skip += 1 + p_pkt->p_buffer[4];
if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
if ( VLC_SUCCESS == ParsePESHeader( VLC_OBJECT(p_demux), &p_pkt->p_buffer[i_skip],
p_pkt->i_buffer - i_skip, &i_skip,
&i_dts, &i_pts, &i_stream_id ) )
{
......
......@@ -36,6 +36,7 @@
#include <vlc_plugin.h>
#include <vlc_demux.h>
#include "mpeg/pes.h"
#include "mpeg/ps.h"
#include "vobsub.h"
......@@ -685,7 +686,7 @@ static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
i_id = ps_pkt_id( p_pkt );
if( (i_id&0xffe0) != 0xbd20 ||
ps_pkt_parse_pes( p_pkt, 1 ) )
ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, 1 ) )
{
block_Release( p_pkt );
continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment