Statistics
| Branch: | Revision:

psnr-tools / psnr.sh @ 604fe120

History | View | Annotate | Download (5.51 KB)

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

    
8
[[ $PSNR_TOOLS ]] || { echo "\$PSNR_TOOLS not defined"; exit 1; }
9
[[ $PSNR_TMPPREFIX ]] || { echo "\$PSNR_TMPPREFIX not defined"; exit 1; }
10
[[ $FFMPEG ]] || { echo "\$FFMPEG not defined"; exit 1; }
11

    
12
mkdir -p `dirname ${PSNR_TMPPREFIX}_`
13

    
14
ORIG_YUV=${PSNR_TMPPREFIX}orig.yuv
15
ORIG_YUV_DESC=${ORIG_YUV}.desc
16
ORIG_MP4=${PSNR_TMPPREFIX}orig_compressed.m4v
17
RECEIVED_MP4=${PSNR_TMPPREFIX}received.m4v
18
REFILLED_YUV=${PSNR_TMPPREFIX}refilled.yuv
19

    
20
ANALYSER=$PSNR_TOOLS/Analyser/analyser
21
CUTTER=$PSNR_TOOLS/Cutter/cutter
22
LOGPARSER=$PSNR_TOOLS/LogParser/logparser
23
REFILLER=$PSNR_TOOLS/ReFiller/refiller
24
EVALPSNR=$PSNR_TOOLS/EvalVid/psnr
25

    
26

    
27
function usage {
28
   echo "Usage: $0 init <orig video> <codec settings>";
29
   echo "       $0 calc <chunksize> <file with losses>";
30
   echo "       $0 play <chunksize> <file with losses>";
31
   echo "examples:";
32
   echo " $0 init ~/video/foreman_cif_100GOP.yuv '-f h264 -vcodec libx264 -vpre libx264-default -keyint_min 12  -g 12 -b 0.4M '";
33
   echo " $0 calc 0 lost.txt";
34
   echo " $0 play 1500 lost.txt";
35
   echo "";
36
   exit $1 
37
}
38

    
39
function init {
40
  [[ $# -eq 2 ]] || usage 1
41

    
42
  ORIG=$1 # the original sequence. Can be a YUV but also a something encoded. If it is a YUV, you need a .desc file as well!!!
43
  ORIG_DESC=${ORIG}.desc
44
  CODER=$2 # conversion parameters for streaming
45

    
46
  #initialize by creating YUV, if needed
47
  rm $ORIG_YUV $ORIG_YUV_DESC
48
  if [[ $ORIG =~ \.[yY][uU][vV]$ ]]; then #if it is aready a YUV, just link or copy
49
    ln -s  $ORIG $ORIG_YUV || cp $ORIG $ORIG_YUV
50
    ln -s  $ORIG_DESC $ORIG_YUV_DESC || cp $ORIG_DESC $ORIG_YUV_DESC || { echo "$ORIG_DESC missing. You need a description file for a YUV"; exit 1; }
51
    . $ORIG_YUV_DESC
52
    [[ $VIDEO_XSIZE != "" && $VIDEO_YSIZE != "" && $VIDEO_FPS != "" ]] || { echo "$ORIG_YUV_DESC not valid. You need a description file for a YUV"; exit 1; }
53

    
54
  else
55
    #$FFMPEG -i $ORIG -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r $VIDEO_FPS $ORIG_YUV >${PSNR_TMPPREFIX}ffmpeg.log
56
    $FFMPEG -y -i $ORIG $ORIG_YUV >${PSNR_TMPPREFIX}ffmpeg.log 2>&1
57

    
58
    # Stream #0.0: Video: h264, yuv420p, 352x288, 25 tbr, 1200k tbn, 50 tbc
59
    TMP=`grep "Stream.*Video:" ${PSNR_TMPPREFIX}ffmpeg.log`
60
#    if [[ $TMP =~ Stream.*Video:.*\ ([0-9]+)x([0-9]+),\ ([0-9]+)\ tbr ]]; then #working on GNU bash, version 3.2.48(1)-release (x86_64-pc-linux-gnu)
61
    if [[ $TMP =~ Stream.*Video:.*\ \([0-9]+\)x\([0-9]+\),\ \([0-9]+\)\ tbr ]]; then #working on GNU bash, version 3.1.17(1)-release (x86_64-pc-linux-gnu), also on version 3.2.48(1)-release (x86_64-pc-linux-gnu)
62
      VIDEO_XSIZE=${BASH_REMATCH[1]}
63
      VIDEO_YSIZE=${BASH_REMATCH[2]}
64
      VIDEO_FPS=${BASH_REMATCH[3]}
65
      echo "VIDEO_XSIZE=$VIDEO_XSIZE"  >$ORIG_YUV_DESC
66
      echo "VIDEO_YSIZE=$VIDEO_YSIZE" >>$ORIG_YUV_DESC
67
      echo "VIDEO_FPS=$VIDEO_FPS" >>$ORIG_YUV_DESC
68
    else
69
      echo "Can't get video description (XSIZE, YSIZE or FPS). You need to create a description file by hand"
70
      exit 1
71
    fi
72
  fi
73

    
74
  #$FFMPEG -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r 25 -i $ORIG_YUV -f m4v -mbd 2 -b 400k $ORIG_MP4 >>${PSNR_TMPPREFIX}ffmpeg.log
75
  $FFMPEG -y -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r $VIDEO_FPS -i $ORIG_YUV $CODER $ORIG_MP4 >>${PSNR_TMPPREFIX}ffmpeg.log
76
  $ANALYSER $ORIG_MP4 > ${PSNR_TMPPREFIX}orig_log.txt
77
}
78

    
79
function calc {
80
  [[ $# -eq 2 ]] || usage 1
81

    
82
  CHUNK_SIZE=$1
83
  LOST_FILE=$2
84

    
85
  . $ORIG_YUV_DESC
86

    
87
  #cut lost chunks
88
  rm $RECEIVED_MP4 # FIXME: I have no idea why this was needed, but sometimes cutter wasn't overwriting
89
  $CUTTER $ORIG_MP4 $RECEIVED_MP4 $LOST_FILE $CHUNK_SIZE 2> ${PSNR_TMPPREFIX}cutter_log.txt
90

    
91
  #get chunk count and avgsize
92
  BYTES=`awk '/^bytes:/ {print $2}' ${PSNR_TMPPREFIX}cutter_log.txt`
93
  CHUNKS=`awk '/^chunks:/ {print $2}' ${PSNR_TMPPREFIX}cutter_log.txt`
94
  CHUNKSLOST=`awk '/^chunkslost:/ {print $2}' ${PSNR_TMPPREFIX}cutter_log.txt`
95
  #CHUNK_SIZE_AVG=`(echo - | awk '{ print $BYTES/$CHUNKS}')`
96

    
97
  #analyse losses
98
  $ANALYSER $RECEIVED_MP4 > ${PSNR_TMPPREFIX}received_log.txt
99

    
100
  #reconstruct sequence by duplicating 
101
  $LOGPARSER ${PSNR_TMPPREFIX}orig_log.txt ${PSNR_TMPPREFIX}received_log.txt > ${PSNR_TMPPREFIX}received_dup.txt
102
  $REFILLER $RECEIVED_MP4 $REFILLED_YUV ${PSNR_TMPPREFIX}received_dup.txt > ${PSNR_TMPPREFIX}refiller_log.txt
103
  $EVALPSNR $VIDEO_XSIZE $VIDEO_YSIZE 420 $ORIG_YUV $REFILLED_YUV >${PSNR_TMPPREFIX}psnr_log.txt 2>${PSNR_TMPPREFIX}psnravg_log.txt
104
  $EVALPSNR $VIDEO_XSIZE $VIDEO_YSIZE 420 $ORIG_YUV $REFILLED_YUV >${PSNR_TMPPREFIX}psnr_log.txt 2>${PSNR_TMPPREFIX}psnravg_log.txt
105

    
106
  PSNR=`awk '/^psnr:/ {print $8}' ${PSNR_TMPPREFIX}psnravg_log.txt`
107

    
108
  $EVALPSNR $VIDEO_XSIZE $VIDEO_YSIZE 420 $ORIG_YUV $REFILLED_YUV ssim >${PSNR_TMPPREFIX}ssim_log.txt 2>${PSNR_TMPPREFIX}ssimavg_log.txt
109

    
110
  SSIM=`awk '/^ssim:/ {print $8}' ${PSNR_TMPPREFIX}ssimavg_log.txt`
111

    
112
  echo "bytes: $BYTES"
113
  echo "chunks: $CHUNKS"
114
  echo "chunkslost: $CHUNKSLOST"
115
  echo "frames: $FRAMES"
116
  echo "frameslost: $FRAMESLOST"
117
  echo "psnr: $PSNR"
118
  echo "ssim: $SSIM"
119
}
120

    
121
function play {
122
  [[ $# -eq 2 ]] || usage 1
123
  [[ $FFPLAY ]] || { echo "\$FFPLAY not defined"; exit 1; }
124

    
125
  #run all the calculations
126
  calc "$@"
127

    
128
  #play the resulting video
129
  #$FFPLAY -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r $VIDEO_FPS -i $ORIG_YUV
130
  $FFPLAY -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} $REFILLED_YUV
131
}
132

    
133
[[ $# -ge 1 ]] || usage 1
134
CMD=$1;
135
shift
136

    
137
case $CMD in
138
  init)
139
    init "$@"
140
    ;;
141
  calc)
142
    calc "$@"
143
    ;;
144
  play)
145
    play "$@"
146
    ;;
147
  *)
148
    usage 1
149
    ;;
150
esac