Statistics
| Branch: | Revision:

psnr-tools / QoE.pl @ eb5ce4e7

History | View | Annotate | Download (4.12 KB)

1
#!/usr/bin/perl
2
 ###########################################
3
 #  Copyright (c) 2009 Csaba Kiraly        #
4
 #                                         #
5
 #  This is free software; see gpl-3.0.txt #
6
 ###########################################
7

    
8
use strict;
9
use Statistics::Descriptive;
10
use POSIX qw(ceil floor);
11
use Switch;
12

    
13
my $gl_inputfolder;
14

    
15
($ENV{'PSNR_TOOLS'}) or (print "\$PSNR_TOOLS not defined\n" and die);
16
my $PSNR_TOOLS=$ENV{'PSNR_TOOLS'};
17
($ENV{'PSNR_TMPPREFIX'}) or (print "\$PSNR_TMPPREFIX not defined\n" and die);
18
my $PSNR_TMPPREFIX=$ENV{'PSNR_TMPPREFIX'};
19

    
20
#a value of 'src' for the peer creates an empty lost file
21
sub peer_chunks_lost ($) {
22
  my ($peer)=@_;
23

    
24
  my @losts;
25
  if ($peer=='src') {
26
    @losts=`echo -n > lost.txt`;
27
  } else {
28
    @losts=`$PSNR_TOOLS/chunks_lost.sh $peer $gl_inputfolder| tee lost.txt`;
29
  }
30
  chomp @losts;
31
  return @losts;
32
}
33

    
34
sub peer_qoe ($$) {
35
  my ($peer,$chunksize)=@_;
36

    
37
  my @losts = peer_chunks_lost($peer);
38

    
39
  my %qoe;
40
  #$qoe{ 'chunkloss' } = peer_chunkloss($peer,@losts);
41

    
42
  my $q;
43
  $q=`$PSNR_TOOLS/psnr.sh calc $chunksize lost.txt`;
44

    
45
  $q =~ m/psnr: ([\d\.]+)/;
46
  $qoe{ 'psnr' } = $1;
47

    
48
  $q =~ m/ssim: ([\d\.]+)/;
49
  $qoe{ 'ssim' } = $1;
50

    
51
  $q =~ m/bytes: ([\d\.]+)/;
52
  $qoe{ 'bytes' } = $1;
53

    
54
  $q =~ m/chunks: ([\d\.]+)/;
55
  $qoe{ 'chunks' } = $1;
56

    
57
  $q =~ m/chunkslost: ([\d\.]+)/;
58
  $qoe{ 'chunkslost' } = $1;
59

    
60
  $qoe{ 'chunkloss' } = $qoe{ 'chunkslost' } / $qoe{ 'chunks' } ;
61

    
62
  return \%qoe;
63
}
64

    
65
#sub num_chunks() {
66
#  return 1000;
67
#}
68
#
69
#sub peer_chunkloss ($@) {
70
#  my ($peer,@losts)=@_;
71
#  return (scalar @losts) / num_chunks();
72
#}
73

    
74
sub peers_of_class ($) {
75
  my ($peerclass)=@_;
76

    
77
  my @peers=`$PSNR_TOOLS/peers_of_class.sh $peerclass $gl_inputfolder`;
78
  chomp @peers;
79
  return @peers;
80
}
81

    
82
sub print_stat($\%$) {
83
  my ($name,$qoes,$metric)=@_;
84

    
85
  my $stat = Statistics::Descriptive::Full->new();
86
  foreach my $peer (keys(%$qoes)) {
87
    $stat->add_data($qoes->{$peer}{$metric});
88
  }
89

    
90
  print "$name: ";
91
  print " mean=".$stat->mean(); 
92
  print " var=".$stat->variance();
93
  print " count=".$stat->count();
94
  print "\n";
95

    
96
  return $stat->mean();
97
}
98

    
99
sub print_stats(\%){
100
  my ($qoes)=@_;
101
  my %avgqoe;
102
  $avgqoe{'psnr'} = print_stat("  psnr(uncompressed->received)",%$qoes ,'psnr');
103
  $avgqoe{'ssim'} = print_stat("  ssim",%$qoes,'ssim');
104
  $avgqoe{'chunkloss'} = print_stat("  chunkloss",%$qoes,'chunkloss');
105
  print "\n";
106

    
107
  return \%avgqoe;
108
}
109

    
110
sub qoe($$){
111
  my ($chunksize,$every)=@_;
112

    
113
  my %allqoes;
114
  my @peerclasses=(1,2,3,4);
115

    
116
  foreach my $peerclass (@peerclasses) {
117

    
118
    print " Peer class = $peerclass\n";
119

    
120
    my @peers=peers_of_class($peerclass);
121

    
122
    my %qoes;
123
    my $everycounter=0;
124
    foreach my $peer (@peers) {
125
      if ($everycounter % $every == 0) {
126
        $qoes{$peer} = peer_qoe($peer,$chunksize);
127
      }
128
      $everycounter++;
129
    }
130
    print_stats(%qoes);
131
    @allqoes{keys %qoes} = values %qoes;
132
  }
133

    
134
  print " All peers\n";
135
  my $avgqoe = print_stats(%allqoes);
136

    
137
  return $avgqoe;
138
}
139

    
140
sub init ($$) {
141
  my ($video,$coder)=@_;
142
  my $tmp=`$PSNR_TOOLS/psnr.sh init $video "$coder"`;
143
}
144

    
145
sub calc ($$) {
146
  my ($inputfolder,$every)=@_;
147
  $gl_inputfolder=$inputfolder;
148

    
149
  my $srcqoe = peer_qoe('src',0);
150
  print "psnr(uncompressed->compressed): ".$srcqoe->{'psnr'}."\n";
151
  print "ssim(uncompressed->compressed): ".$srcqoe->{'ssim'}."\n";
152

    
153
  my $chunksize = ceil($srcqoe->{'bytes'}/$srcqoe->{'chunks'}); 
154

    
155
  print "\nQuality with GOP size chunks (avg chunk size = $srcqoe->{'bytes'}/$srcqoe->{'chunks'} bytes )\n";
156
  my $avgqoe_gop = qoe(0,$every);
157
  print "\nQuality with $chunksize bytes chunks\n";
158
  my $avgqoe_nogop = qoe($chunksize,$every);
159
  print "PSNRresults $srcqoe->{'psnr'} $avgqoe_gop->{'psnr'} $avgqoe_nogop->{'psnr'}".
160
                   " $srcqoe->{'ssim'} $avgqoe_gop->{'ssim'} $avgqoe_nogop->{'ssim'}".
161
  "\n";
162
}
163

    
164
#--------------------------------------------------
165

    
166
if ($ARGV[0] eq 'init') {
167
  ($#ARGV + 1 == 3) or die;
168
  my $video=$ARGV[1];
169
  my $coder=$ARGV[2];
170
  init($video,$coder);
171
} elsif ($ARGV[0] eq 'calc') {
172
  ($#ARGV + 1 == 3) or die;
173
  my $inputfolder=$ARGV[1];
174
  my $every=$ARGV[2]; #calculate values only for every "everty"th of peers
175
  calc($inputfolder,$every);
176
} else {
177
  print "Usage: ... see source\n";
178
}
179