File: autotune

package info (click to toggle)
libvideo-capture-v4l-perl 0.902-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 584 kB
  • sloc: perl: 2,749; ansic: 1,494; sh: 22; makefile: 3
file content (177 lines) | stat: -rwxr-xr-x 4,517 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
#!/usr/bin/perl

# dumps "interesting" data about tv channels

use Getopt::Long;

use Video::Frequencies;
use Video::Capture::V4l;
use Video::Capture::VBI qw/:DEFAULT %VPS_CNI %VT_NI/;
use Video::XawTV;

GetOptions "+verbose|v" => \$verbose
   or exit 1;

$rc = new Video::XawTV;
eval { $rc->load("$ENV{HOME}/.xawtv") };

$ftab = $rc->opt('freqtab') || "pal-europe";
$freq = $CHANLIST{$ftab} or die "no such frequency table: $ftab";

$v4l = new Video::Capture::V4l;
$tuner = $v4l->tuner(0);
$channel = $v4l->channel(0);

$tuner->mode(MODE_PAL); $tuner->set;
$channel->norm(MODE_PAL); $channel->set;

$vbi = new Video::Capture::V4l::VBI or die;
$vbi_fd = $vbi->fileno;

$|=1;

my @channels;

for $chan (sort keys %$freq) {
   my $f = $freq->{$chan};
   print "tuning to $chan ($f)...";
   $v4l->freq($f);
   select undef,undef,undef,0.2; # shit!
   $vbi->backlog (10);
   %fea = ();
   scan_vbi (50);
   $vbi->backlog (0);
   if ($tuner->signal > 30000) {
      my $cni = $VPS_CNI{$fea{CNI} & 0xfff};
      my $ni = $VT_NI{$fea{NI}};
      my $name;
      if (defined $ni) {
         $name = "$ni->[0] ($ni->[1])";
      } elsif (defined $cni) {
         $name = $cni;
      } elsif ($fea{NAME}) {
         $name = $fea{NAME};
      } elsif (length $fea{VT} > 1) {
         $name = $fea{VT};
      } else {
         $name = "channel $chan";
      }
      $name =~ s/\s*"\s*/ /g;
      $name =~ s/\s*\(.*?\)\s*/ /g;
      $name =~ s/^\s+//;
      $name =~ s/\s+$//;
      print " $name";
      print " [";
      while(my($k,$v)=each %fea) {
         print " $k","[$v]";
      }
      print " ]";
      my $c = { name => $name, channel => $chan, capture => 'on' };
      if (1||$verbose) {
         $c->{features} = join(":", %fea);
      }
      my $key = find_key ($name);
      $c->{key} = $key if $key;
      push @channels, $c;
   } else {
      print " no signal";
   }
   print "\n";
}

$rc->channels(@channels);
$rc->save("xawtvrc");

print "\nnew xawtvrc saved as ./xawtvrc\n";

sub scan_vbi {
   my $frames = shift;

   my($name_,$name,$name2);

   while ($frames) {
      my $vbi_alloc;
      $tuner->get; return if $tuner->signal < 30000;
      $frames--;

      return if (defined $VT_NI{$fea{NI}} || defined $VPS_CNI{$fea{CNI}}) && !$verbose;
      
      for (decode_field $vbi->field, VBI_VT|VBI_VPS|VBI_OTHER|VBI_EMPTY) {
         if ($_->[0] == VBI_VPS) {
            $fea{CNI}=$_->[3];
            if (ord($_->[1]) > 127 or length $name_ >= 12) {
               if ($name eq $name_) {
                  $fea{NAME}=$name;
               }
               $name = $name_;
            }
            $name_ .= $_->[1] & "\x7f";
            $fea{VPS}=sprintf "%04x", $_->[3];
            $vbi_alloc .= "V";
         } elsif ($_->[0] == VBI_VT) {
            if ($_->[2] == 0) {
               if ($_->[4] == 0x1df) {
                  $fea{EPG}="";
               } else {
                  $fea{VT}=vt_2_name($_->[3]);
               }
            } elsif ($_->[2] == 30) {
               if (($_->[3]>>1) == 0) {
                  $fea{NI} = $_->[6];
                  $fea{'NI30/1'}=sprintf "%04x", $_->[6];
               } elsif (($_->[3]>>1) == 8) {
                  $fea{PDC}="";
               } else {
                  $fea{"30"}="$_->[3]";
               }
            } elsif ($_->[2] == 31) {
               if ($_->[4] == 0x500) {
                  $fea{"$_->[1]/IC"}="";
               } else {
                  $fea{sprintf "$_->[1]/31[%x]",$_->[4]}++;
               }
            }
            $vt++;
            $vbi_alloc .= "T";
         } elsif ($_->[0] == VBI_OTHER) {
            $vbi_alloc .= $_->[1] == 1 ? "c" : "O";
         } elsif ($_->[0] == VBI_EMPTY) {
            $vbi_alloc .= ".";
         } else {
            $others++;
         }
      }
      $fea{ALLOC}=$vbi_alloc;
      select undef,undef,undef,0.1 unless $vbi->queued;
   }
}

# try to guess sender name from videotext
sub vt_2_name {
   local $_ = substr (shift, 8, 20) & ("\x7f") x 20;
   s/^\d+//;
   s/^[\x00-\x1f ]+//;
   s/\s*[\x00-\x1f].*//;
   s/\W?text.*//i;
   $_;
}

sub find_key  {
   local $_ = shift;
   return '.' if /3sat/;
   return '1' if /ARD/;
   return '2' if /ZDF/;
   return '3' if /SW 3/;
   return '7' if /PRO 7/;
   return 'r' if /RTL Plus/;
   return 't' if /RTL 2/;
   return 'i' if /VIVA 2/;
   return 'v' if /VIVA/;
   return 'm' if /MTV/;
   return 's' if /SAT 1/;
   return 'k' if /Kabel 1/;
   return 'e' if /EuroNews/;
   return 'a' if /Arte/;
   return 'x' if /VOX/;
   ();
}