SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Public Attributes | Private Attributes | List of all members
leansdr::mpeg_sync< Tbyte, BYTE_ERASED > Struct Template Reference

#include <dvb.h>

+ Inheritance diagram for leansdr::mpeg_sync< Tbyte, BYTE_ERASED >:
+ Collaboration diagram for leansdr::mpeg_sync< Tbyte, BYTE_ERASED >:

Public Member Functions

 mpeg_sync (scheduler *sch, pipebuf< Tbyte > &_in, pipebuf< Tbyte > &_out, deconvol_sync< Tbyte, 0 > *_deconv, pipebuf< int > *_state_out=NULL, pipebuf< unsigned long > *_locktime_out=NULL)
 
void run ()
 
void run_searching ()
 
void run_searching_fast ()
 
bool search_sync ()
 
void run_decoding ()
 
- Public Member Functions inherited from leansdr::runnable
 runnable (scheduler *_sch, const char *name)
 
- Public Member Functions inherited from leansdr::runnable_common
 runnable_common (const char *_name)
 
virtual ~runnable_common ()
 
virtual void shutdown ()
 

Public Attributes

int scan_syncs
 
int want_syncs
 
unsigned long lock_timeout
 
bool fastlock
 
int resync_period
 
- Public Attributes inherited from leansdr::runnable_common
const char * name
 

Private Attributes

pipereader< Tbyte > in
 
pipewriter< Tbyte > out
 
deconvol_sync< Tbyte, 0 > * deconv
 
unsigned char polarity
 
int resync_phase
 
int bitphase
 
bool synchronized
 
int next_sync_count
 
int phase8
 
unsigned long lock_timeleft
 
unsigned long locktime
 
pipewriter< int > * state_out
 
pipewriter< unsigned long > * locktime_out
 
bool report_state
 

Additional Inherited Members

- Protected Attributes inherited from leansdr::runnable
schedulersch
 

Detailed Description

template<typename Tbyte, Tbyte BYTE_ERASED>
struct leansdr::mpeg_sync< Tbyte, BYTE_ERASED >

Definition at line 832 of file dvb.h.

Constructor & Destructor Documentation

◆ mpeg_sync()

template<typename Tbyte, Tbyte BYTE_ERASED>
leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::mpeg_sync ( scheduler sch,
pipebuf< Tbyte > &  _in,
pipebuf< Tbyte > &  _out,
deconvol_sync< Tbyte, 0 > *  _deconv,
pipebuf< int > *  _state_out = NULL,
pipebuf< unsigned long > *  _locktime_out = NULL 
)
inline

Definition at line 839 of file dvb.h.

844  : runnable(sch, "sync_detect"),
845  scan_syncs(8),
846  want_syncs(4),
847  lock_timeout(4),
848  fastlock(false),
849  resync_period(1),
850  in(_in),
851  out(_out, SIZE_RSPACKET * (scan_syncs + 1)),
852  deconv(_deconv),
853  polarity(0),
854  resync_phase(0),
855  bitphase(0),
856  synchronized(false),
857  next_sync_count(0),
858  report_state(true)
859  {
860  state_out = _state_out ? new pipewriter<int>(*_state_out) : NULL;
861  locktime_out = _locktime_out ? new pipewriter<unsigned long>(*_locktime_out) : NULL;
862  }
int resync_period
Definition: dvb.h:837
int want_syncs
Definition: dvb.h:834
pipewriter< unsigned long > * locktime_out
Definition: dvb.h:1088
unsigned char polarity
Definition: dvb.h:1079
runnable(scheduler *_sch, const char *name)
Definition: framework.h:193
unsigned long lock_timeout
Definition: dvb.h:835
pipereader< Tbyte > in
Definition: dvb.h:1076
pipewriter< Tbyte > out
Definition: dvb.h:1077
int scan_syncs
Definition: dvb.h:834
int resync_phase
Definition: dvb.h:1080
scheduler * sch
Definition: framework.h:199
bool report_state
Definition: dvb.h:1089
int next_sync_count
Definition: dvb.h:1083
pipewriter< int > * state_out
Definition: dvb.h:1087
bool fastlock
Definition: dvb.h:836
deconvol_sync< Tbyte, 0 > * deconv
Definition: dvb.h:1078

Member Function Documentation

◆ run()

template<typename Tbyte, Tbyte BYTE_ERASED>
void leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::run ( )
inlinevirtual

Reimplemented from leansdr::runnable_common.

Definition at line 864 of file dvb.h.

865  {
866  if (report_state && state_out && state_out->writable() >= 1)
867  {
868  // Report unlocked state on first invocation.
869  state_out->write(0);
870  report_state = false;
871  }
872 
873  if (synchronized)
874  {
875  run_decoding();
876  }
877  else
878  {
879  if (fastlock)
881  else
882  run_searching();
883  }
884  }
void run_decoding()
Definition: dvb.h:1028
void run_searching_fast()
Definition: dvb.h:924
void run_searching()
Definition: dvb.h:886
void write(const T &e)
Definition: framework.h:319
bool report_state
Definition: dvb.h:1089
pipewriter< int > * state_out
Definition: dvb.h:1087
bool fastlock
Definition: dvb.h:836

◆ run_decoding()

template<typename Tbyte, Tbyte BYTE_ERASED>
void leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::run_decoding ( )
inline

Definition at line 1028 of file dvb.h.

1029  {
1030  while (in.readable() >= SIZE_RSPACKET + 1 && out.writable() >= SIZE_RSPACKET && (!state_out || state_out->writable() >= 1) && (!locktime_out || locktime_out->writable() >= 1))
1031  {
1032  Tbyte *pin = in.rd(), *pend = pin + SIZE_RSPACKET;
1033  Tbyte *pout = out.wr();
1034  unsigned short w = *pin++;
1035 
1036  for (; pin <= pend; ++pin, ++pout)
1037  {
1038  w = (w << 8) | *pin;
1039  *pout = (w >> bitphase) ^ polarity;
1040  }
1041 
1042  in.read(SIZE_RSPACKET);
1043  Tbyte syncbyte = *out.wr();
1044  out.written(SIZE_RSPACKET);
1045  ++locktime;
1046 
1047  if (locktime_out)
1049 
1050  // Reset timer if sync byte is correct
1051  Tbyte expected = phase8 ? MPEG_SYNC : MPEG_SYNC_INV;
1052 
1053  if (syncbyte == expected)
1055 
1056  phase8 = (phase8 + 1) & 7;
1057  --lock_timeleft;
1058 
1059  if (!lock_timeleft)
1060  {
1061  if (sch->debug)
1062  fprintf(stderr, "Unlocked\n");
1063 
1064  synchronized = false;
1065  next_sync_count = 0;
1066 
1067  if (state_out)
1068  state_out->write(0);
1069 
1070  return;
1071  }
1072  }
1073  }
pipewriter< unsigned long > * locktime_out
Definition: dvb.h:1088
unsigned long locktime
Definition: dvb.h:1086
unsigned char polarity
Definition: dvb.h:1079
unsigned long lock_timeout
Definition: dvb.h:835
pipereader< Tbyte > in
Definition: dvb.h:1076
pipewriter< Tbyte > out
Definition: dvb.h:1077
void written(unsigned long n)
Definition: framework.h:308
scheduler * sch
Definition: framework.h:199
void write(const T &e)
Definition: framework.h:319
unsigned long lock_timeleft
Definition: dvb.h:1085
int next_sync_count
Definition: dvb.h:1083
pipewriter< int > * state_out
Definition: dvb.h:1087
void read(unsigned long n)
Definition: framework.h:367

◆ run_searching()

template<typename Tbyte, Tbyte BYTE_ERASED>
void leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::run_searching ( )
inline

Definition at line 886 of file dvb.h.

887  {
888  bool next_sync = false;
889  int chunk = SIZE_RSPACKET * scan_syncs;
890 
891  while (in.readable() >= chunk + 1 // Need 1 ahead for bit shifting
892  && out.writable() >= chunk // Use as temp buffer
893  && (!state_out || state_out->writable() >= 1))
894  {
895  if (search_sync())
896  return;
897 
898  in.read(chunk);
899  // Switch to next bit alignment
900  ++bitphase;
901 
902  if (bitphase == 8)
903  {
904  bitphase = 0;
905  next_sync = true;
906  }
907  }
908 
909  if (next_sync)
910  {
911  // No lock this time
912  ++next_sync_count;
913 
914  if (next_sync_count >= 3)
915  {
916  // After a few cycles without a lock, resync the deconvolver.
917  next_sync_count = 0;
918  if (deconv)
919  deconv->next_sync();
920  }
921  }
922  }
bool search_sync()
Definition: dvb.h:949
pipereader< Tbyte > in
Definition: dvb.h:1076
pipewriter< Tbyte > out
Definition: dvb.h:1077
int scan_syncs
Definition: dvb.h:834
void next_sync()
Definition: dvb.h:195
int next_sync_count
Definition: dvb.h:1083
pipewriter< int > * state_out
Definition: dvb.h:1087
void read(unsigned long n)
Definition: framework.h:367
deconvol_sync< Tbyte, 0 > * deconv
Definition: dvb.h:1078

◆ run_searching_fast()

template<typename Tbyte, Tbyte BYTE_ERASED>
void leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::run_searching_fast ( )
inline

Definition at line 924 of file dvb.h.

925  {
926  int chunk = SIZE_RSPACKET * scan_syncs;
927 
928  while (in.readable() >= chunk + 1 // Need 1 ahead for bit shifting
929  && out.writable() >= chunk // Use as temp buffer
930  && (!state_out || state_out->writable() >= 1))
931  {
932  if (resync_phase == 0)
933  {
934  // Try all bit alighments
935  for (bitphase = 0; bitphase <= 7; ++bitphase)
936  {
937  if (search_sync())
938  return;
939  }
940  }
941 
942  in.read(SIZE_RSPACKET);
943 
944  if (++resync_phase >= resync_period)
945  resync_phase = 0;
946  }
947  }
int resync_period
Definition: dvb.h:837
bool search_sync()
Definition: dvb.h:949
pipereader< Tbyte > in
Definition: dvb.h:1076
pipewriter< Tbyte > out
Definition: dvb.h:1077
int scan_syncs
Definition: dvb.h:834
int resync_phase
Definition: dvb.h:1080
pipewriter< int > * state_out
Definition: dvb.h:1087
void read(unsigned long n)
Definition: framework.h:367

◆ search_sync()

template<typename Tbyte, Tbyte BYTE_ERASED>
bool leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::search_sync ( )
inline

Definition at line 949 of file dvb.h.

950  {
951  int chunk = SIZE_RSPACKET * scan_syncs;
952  // Bit-shift [scan_sync] packets according to current [bitphase]
953  Tbyte *pin = in.rd(), *pend = pin + chunk;
954  Tbyte *pout = out.wr();
955  unsigned short w = *pin++;
956 
957  for (; pin <= pend; ++pin, ++pout)
958  {
959  w = (w << 8) | *pin;
960  *pout = w >> bitphase;
961  }
962 
963  // Search for [want_sync] start codes at all 204 offsets
964  for (int i = 0; i < SIZE_RSPACKET; ++i)
965  {
966  int nsyncs_p = 0, nsyncs_n = 0; // # start codes assuming pos/neg polarity
967  int phase8_p = -1, phase8_n = -1; // Position in sequence of 8 packets
968  Tbyte *p = &out.wr()[i];
969 
970  for (int j = 0; j < scan_syncs; ++j, p += SIZE_RSPACKET)
971  {
972  Tbyte b = *p;
973 
974  if (b == MPEG_SYNC)
975  {
976  ++nsyncs_p;
977  phase8_n = (8 - j) & 7;
978  }
979 
980  if (b == MPEG_SYNC_INV)
981  {
982  ++nsyncs_n;
983  phase8_p = (8 - j) & 7;
984  }
985  }
986 
987  // Detect most likely polarity
988  int nsyncs;
989 
990  if (nsyncs_p > nsyncs_n)
991  {
992  polarity = 0;
993  nsyncs = nsyncs_p;
994  phase8 = phase8_p;
995  }
996  else
997  {
998  polarity = -1;
999  nsyncs = nsyncs_n;
1000  phase8 = phase8_n;
1001  }
1002 
1003  if (nsyncs >= want_syncs && phase8 >= 0)
1004  {
1005  if (sch->debug)
1006  fprintf(stderr, "Locked\n");
1007 
1008  if (!i)
1009  { // Avoid fixpoint detection in scheduler
1010  i = SIZE_RSPACKET;
1011  phase8 = (phase8 + 1) & 7;
1012  }
1013 
1014  in.read(i); // Skip to first start code
1015  synchronized = true;
1017  locktime = 0;
1018 
1019  if (state_out)
1020  state_out->write(1);
1021 
1022  return true;
1023  }
1024  }
1025  return false;
1026  }
int want_syncs
Definition: dvb.h:834
unsigned long locktime
Definition: dvb.h:1086
unsigned char polarity
Definition: dvb.h:1079
unsigned long lock_timeout
Definition: dvb.h:835
pipereader< Tbyte > in
Definition: dvb.h:1076
pipewriter< Tbyte > out
Definition: dvb.h:1077
int32_t i
Definition: decimators.h:244
int scan_syncs
Definition: dvb.h:834
scheduler * sch
Definition: framework.h:199
void write(const T &e)
Definition: framework.h:319
unsigned long lock_timeleft
Definition: dvb.h:1085
pipewriter< int > * state_out
Definition: dvb.h:1087
void read(unsigned long n)
Definition: framework.h:367

Member Data Documentation

◆ bitphase

template<typename Tbyte, Tbyte BYTE_ERASED>
int leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::bitphase
private

Definition at line 1081 of file dvb.h.

◆ deconv

template<typename Tbyte, Tbyte BYTE_ERASED>
deconvol_sync<Tbyte, 0>* leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::deconv
private

Definition at line 1078 of file dvb.h.

◆ fastlock

template<typename Tbyte, Tbyte BYTE_ERASED>
bool leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::fastlock

Definition at line 836 of file dvb.h.

◆ in

template<typename Tbyte, Tbyte BYTE_ERASED>
pipereader<Tbyte> leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::in
private

Definition at line 1076 of file dvb.h.

◆ lock_timeleft

template<typename Tbyte, Tbyte BYTE_ERASED>
unsigned long leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::lock_timeleft
private

Definition at line 1085 of file dvb.h.

◆ lock_timeout

template<typename Tbyte, Tbyte BYTE_ERASED>
unsigned long leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::lock_timeout

Definition at line 835 of file dvb.h.

◆ locktime

template<typename Tbyte, Tbyte BYTE_ERASED>
unsigned long leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::locktime
private

Definition at line 1086 of file dvb.h.

◆ locktime_out

template<typename Tbyte, Tbyte BYTE_ERASED>
pipewriter<unsigned long>* leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::locktime_out
private

Definition at line 1088 of file dvb.h.

◆ next_sync_count

template<typename Tbyte, Tbyte BYTE_ERASED>
int leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::next_sync_count
private

Definition at line 1083 of file dvb.h.

◆ out

template<typename Tbyte, Tbyte BYTE_ERASED>
pipewriter<Tbyte> leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::out
private

Definition at line 1077 of file dvb.h.

◆ phase8

template<typename Tbyte, Tbyte BYTE_ERASED>
int leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::phase8
private

Definition at line 1084 of file dvb.h.

◆ polarity

template<typename Tbyte, Tbyte BYTE_ERASED>
unsigned char leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::polarity
private

Definition at line 1079 of file dvb.h.

◆ report_state

template<typename Tbyte, Tbyte BYTE_ERASED>
bool leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::report_state
private

Definition at line 1089 of file dvb.h.

◆ resync_period

template<typename Tbyte, Tbyte BYTE_ERASED>
int leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::resync_period

Definition at line 837 of file dvb.h.

◆ resync_phase

template<typename Tbyte, Tbyte BYTE_ERASED>
int leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::resync_phase
private

Definition at line 1080 of file dvb.h.

◆ scan_syncs

template<typename Tbyte, Tbyte BYTE_ERASED>
int leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::scan_syncs

Definition at line 834 of file dvb.h.

◆ state_out

template<typename Tbyte, Tbyte BYTE_ERASED>
pipewriter<int>* leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::state_out
private

Definition at line 1087 of file dvb.h.

◆ synchronized

template<typename Tbyte, Tbyte BYTE_ERASED>
bool leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::synchronized
private

Definition at line 1082 of file dvb.h.

◆ want_syncs

template<typename Tbyte, Tbyte BYTE_ERASED>
int leansdr::mpeg_sync< Tbyte, BYTE_ERASED >::want_syncs

Definition at line 834 of file dvb.h.


The documentation for this struct was generated from the following file: