File: getepg

package info (click to toggle)
libvideo-capture-v4l-perl 0.224-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 572 kB
  • ctags: 563
  • sloc: ansic: 4,924; perl: 2,746; makefile: 57; sh: 22
file content (185 lines) | stat: -rwxr-xr-x 4,536 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/perl

# EPG (Electronic Program Guide Decoder)
# Commercial Name: nexTView

use Video::Capture::V4l;
use Video::Capture::VBI;

package Video::Capture::VBI::EPG;

use Video::Capture::VBI qw(:DEFAULT %VPS_CNI %VT_NI);
use Storable;

use base 'Video::Capture::VBI::VT';

sub new {
   my $class = shift;
   my $self = $class->SUPER::new(@_);
   $self;
}

my $lastcni = -1;
sub feed {
   my $self = shift;
   my @r;
   for ($self->SUPER::feed(@_)) {
      if ($_->[0] == VBI_VPS) {
         if ($_->[3] != $lastcni) {
            $lastcni = $_->[3];
            printf "CNI %04x (%s)\n", $_->[3], $VPS_CNI{$_->[3] & 0xfff};
         }
      } else {
         push @r, $_;
      }
   }
   @r;
}

sub mydump {
   local $_ = shift;
   if (ref $_ eq "HASH") {
      print "{ ";
      while(my($k,$v)=each %$_) {
         print ", $k => ";
         mydump($v);
      }
      print " }";
   } elsif (ref $_ eq "ARRAY") {
      print "[ ";
      for (@$_) {
         print ", ";
         mydump($_);
      }
      print " ]";
   } else {
      y/\x20-\x7e//cd;
      print $_;
   }
}

sub set_epg {
   my $self = shift;
   my $name = shift;
   my $new_db = main::gen_db_name ($name);
   print "SET_EPG $new_db <= ", $self->{db_name}, "\n";
   if ($self->{db_name} and $new_db eq $self->{db_name}) {
      print "saving ",$new_db,"...";
      Storable::nstore($self->db, "$new_db~");
      rename "$new_db~", $new_db;
      print "ok\n";
   } else {
      $self->{db_name} = $new_db;
      print "trying to load $new_db...";
      $self->db(eval { $self->db(Storable::retrieve($new_db)) });
      print "ok\n";
   }
}

# just display the page
sub enter_page {
   my($self,$page)=@_;

   $pages++;

   return unless $page->{page} == 0x1df;

   my $sub = $page->{ctrl};
   my $stream = VTX_S3($sub);
   my $height = VTX_S4($sub)<<3 | VTX_S2($sub);
   my $seq    = VTX_S1($sub);

   my $s = \%{$self->{stream}{$stream}};

   delete $s->{raw} unless (($seq-1)&15) == $s->{seq};
   $s->{seq}=$seq;
   push @{$s->{raw}}, @{$page->{packet}}[1..$height];

   $packets[$stream]++;
   #print "stream $stream, height $height, seq $seq, ",scalar @{$s->{raw}},"<\n";

   for(decode_stream $s->{raw})  {
      if (my ($dt, $bi) = decode_block $_, \@{$self->{bundle}}) {
         if ($dt == 1) {
            my $ai = $self->{db}{ai};
            $self->set_epg($bi->{service_name});
            if ($ai) {
               print "$ai->{epg_version}, $ai->{epg_version_swo}, $ai->{this_network_op}, $ai->{service_name}\n";
               if ($bi->{epg_version} != $ai->{epg_version}
                   || $bi->{epg_version_swo} != $ai->{epg_version_swo}
                   || $bi->{this_network_op} != $ai->{this_network_op}) {
                  print "CHANNEL SWITCH, WIPING DATABASE\n";
                  delete $self->{db};
               }
            }
            $self->{db}{ai} = $bi;
         } elsif ($dt == 2) {
            $self->{db}{pi}{$bi->{netwop_no}}{$bi->{block_no}} = $bi;
         } elsif ($dt == 3) {
            $self->{db}{ni}{$bi->{block_no}} = $bi;
         } elsif ($dt == 4) {
            $self->{db}{oi}{$bi->{block_no}} = $bi;
         } elsif ($dt == 5) {
            $self->{db}{mi}{$bi->{block_no}} = $bi;
         } else {
            print "UNKNOWN BLOCK TYPE $dt found\n";
         }
      }
   }
   $|=1;
   printf "%7d %6d %6d %5d %5d %5d %5d\r", $pages, $packets[0], $packets[1],
          1*%{$self->{db}{pi}},
          1*%{$self->{db}{ni}},
          1*%{$self->{db}{oi}},
          1*%{$self->{db}{mi}};

   #printf "received epg page %04x\n", $sub;
}

sub enter_packet {
   my $self = shift;
   my $p = shift;
   return unless $p->[3] <= 1 && $p->[2] == 30;
   #printf "packet(@$p %04x)", $p->[6];
   print "\n";
}

sub db {
   my $self = shift;
   $self->{db} = shift if @_;
   $self->{db};
}

package main;

$vbi = new Video::Capture::V4l::VBI or die;

# the next line is optional (it enables buffering)
$vbi->backlog(150); # max. 5 seconds backlog (~4.6Mb)

$inp_fd = fileno STDIN;
$vbi_fd = $vbi->fileno;

$vt = new Video::Capture::VBI::EPG;

sub gen_db_name {
   shift() . ".epg";
}

print "Capturing VBI block. Make sure you have tuned in to a channel with EPG!\n";
print "Aquisition may take up to twenty minutes (and more)!\n";

for(;;) {
   my $r="";
   vec($r,$inp_fd,1)=1;
   vec($r,$vbi_fd,1)=1;
   select $r,undef,undef,0.04;
   $vt->feed(decode_field $vbi->field, VBI_VT|VBI_VPS) while $vbi->queued;
   if (vec($r,$inp_fd,1)) {
      $_ = <STDIN>;
      if (/^q/i) {
         last;
      }
   }
}