Statistics
| Branch: | Revision:

psnr-tools / psnr.sh @ 2c8d6927

History | View | Annotate | Download (5.23 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
   exit $1 
31
}
32

    
33
function init {
34
  [[ $# -eq 2 ]] || usage 1
35

    
36
  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!!!
37
  ORIG_DESC=${ORIG}.desc
38
  CODER=$2 # conversion parameters for streaming
39

    
40
  #initialize by creating YUV, if needed
41
  rm $ORIG_YUV $ORIG_YUV_DESC
42
  if [[ $ORIG =~ \.[yY][uU][vV]$ ]]; then #if it is aready a YUV, just link or copy
43
    ln -s  $ORIG $ORIG_YUV || cp $ORIG $ORIG_YUV
44
    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; }
45
    . $ORIG_YUV_DESC
46
    [[ $VIDEO_XSIZE != "" && $VIDEO_YSIZE != "" && $VIDEO_FPS != "" ]] || { echo "$ORIG_YUV_DESC not valid. You need a description file for a YUV"; exit 1; }
47

    
48
  else
49
    #$FFMPEG -i $ORIG -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r $VIDEO_FPS $ORIG_YUV >${PSNR_TMPPREFIX}ffmpeg.log
50
    $FFMPEG -y -i $ORIG $ORIG_YUV >${PSNR_TMPPREFIX}ffmpeg.log 2>&1
51

    
52
    # Stream #0.0: Video: h264, yuv420p, 352x288, 25 tbr, 1200k tbn, 50 tbc
53
    TMP=`grep "Stream.*Video:" ${PSNR_TMPPREFIX}ffmpeg.log`
54
#    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)
55
    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)
56
      VIDEO_XSIZE=${BASH_REMATCH[1]}
57
      VIDEO_YSIZE=${BASH_REMATCH[2]}
58
      VIDEO_FPS=${BASH_REMATCH[3]}
59
      echo "VIDEO_XSIZE=$VIDEO_XSIZE"  >$ORIG_YUV_DESC
60
      echo "VIDEO_YSIZE=$VIDEO_YSIZE" >>$ORIG_YUV_DESC
61
      echo "VIDEO_FPS=$VIDEO_FPS" >>$ORIG_YUV_DESC
62
    else
63
      echo "Can't get video description (XSIZE, YSIZE or FPS). You need to create a description file by hand"
64
      exit 1
65
    fi
66
  fi
67

    
68
  #$FFMPEG -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r 25 -i $ORIG_YUV -f m4v -mbd 2 -b 400k $ORIG_MP4 >>${PSNR_TMPPREFIX}ffmpeg.log
69
  $FFMPEG -y -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r $VIDEO_FPS -i $ORIG_YUV $CODER $ORIG_MP4 >>${PSNR_TMPPREFIX}ffmpeg.log
70
  $ANALYSER $ORIG_MP4 > ${PSNR_TMPPREFIX}orig_log.txt
71
}
72

    
73
function calc {
74
  [[ $# -eq 2 ]] || usage 1
75

    
76
  CHUNK_SIZE=$1
77
  LOST_FILE=$2
78

    
79
  . $ORIG_YUV_DESC
80

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

    
85
  #get chunk count and avgsize
86
  BYTES=`awk '/^bytes:/ {print $2}' ${PSNR_TMPPREFIX}cutter_log.txt`
87
  CHUNKS=`awk '/^chunks:/ {print $2}' ${PSNR_TMPPREFIX}cutter_log.txt`
88
  CHUNKSLOST=`awk '/^chunkslost:/ {print $2}' ${PSNR_TMPPREFIX}cutter_log.txt`
89
  #CHUNK_SIZE_AVG=`(echo - | awk '{ print $BYTES/$CHUNKS}')`
90

    
91
  #analyse losses
92
  $ANALYSER $RECEIVED_MP4 > ${PSNR_TMPPREFIX}received_log.txt
93

    
94
  #reconstruct sequence by duplicating 
95
  $LOGPARSER ${PSNR_TMPPREFIX}orig_log.txt ${PSNR_TMPPREFIX}received_log.txt > ${PSNR_TMPPREFIX}received_dup.txt
96
  $REFILLER $RECEIVED_MP4 $REFILLED_YUV ${PSNR_TMPPREFIX}received_dup.txt > ${PSNR_TMPPREFIX}refiller_log.txt
97
  $EVALPSNR $VIDEO_XSIZE $VIDEO_YSIZE 420 $ORIG_YUV $REFILLED_YUV >${PSNR_TMPPREFIX}psnr_log.txt 2>${PSNR_TMPPREFIX}psnravg_log.txt
98
  $EVALPSNR $VIDEO_XSIZE $VIDEO_YSIZE 420 $ORIG_YUV $REFILLED_YUV >${PSNR_TMPPREFIX}psnr_log.txt 2>${PSNR_TMPPREFIX}psnravg_log.txt
99

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

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

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

    
106
  echo "bytes: $BYTES"
107
  echo "chunks: $CHUNKS"
108
  echo "chunkslost: $CHUNKSLOST"
109
  echo "frames: $FRAMES"
110
  echo "frameslost: $FRAMESLOST"
111
  echo "psnr: $PSNR"
112
  echo "ssim: $SSIM"
113
}
114

    
115
function play {
116
  [[ $# -eq 2 ]] || usage 1
117
  [[ $FFPLAY ]] || { echo "\$FFPLAY not defined"; exit 1; }
118

    
119
  #run all the calculations
120
  calc "$@"
121

    
122
  #play the resulting video
123
  #$FFPLAY -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r $VIDEO_FPS -i $ORIG_YUV
124
  $FFPLAY -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} $REFILLED_YUV
125
}
126

    
127
[[ $# -ge 1 ]] || usage 1
128
CMD=$1;
129
shift
130

    
131
case $CMD in
132
  init)
133
    init "$@"
134
    ;;
135
  calc)
136
    calc "$@"
137
    ;;
138
  play)
139
    play "$@"
140
    ;;
141
  *)
142
    usage 1
143
    ;;
144
esac