Statistics
| Branch: | Revision:

psnr-tools / psnr.sh @ 2c8d6927

History | View | Annotate | Download (5.23 KB)

1 0db6fe80 Csaba Kiraly
#!/bin/bash
2 ad2a1304 Csaba Kiraly
 ###########################################
3
 #  Copyright (c) 2009 Csaba Kiraly        #
4
 #                                         #
5
 #  This is free software; see gpl-3.0.txt #
6
 ###########################################
7 0db6fe80 Csaba Kiraly
8 e7f1eb59 Csaba Kiraly
[[ $PSNR_TOOLS ]] || { echo "\$PSNR_TOOLS not defined"; exit 1; }
9 36907371 Csaba Kiraly
[[ $PSNR_TMPPREFIX ]] || { echo "\$PSNR_TMPPREFIX not defined"; exit 1; }
10 46c2411e Luca
[[ $FFMPEG ]] || { echo "\$FFMPEG not defined"; exit 1; }
11 0db6fe80 Csaba Kiraly
12 b1825363 Csaba Kiraly
mkdir -p `dirname ${PSNR_TMPPREFIX}_`
13
14 36907371 Csaba Kiraly
ORIG_YUV=${PSNR_TMPPREFIX}orig.yuv
15 4b8f86e3 Csaba Kiraly
ORIG_YUV_DESC=${ORIG_YUV}.desc
16 36907371 Csaba Kiraly
ORIG_MP4=${PSNR_TMPPREFIX}orig_compressed.m4v
17
RECEIVED_MP4=${PSNR_TMPPREFIX}received.m4v
18
REFILLED_YUV=${PSNR_TMPPREFIX}refilled.yuv
19 0db6fe80 Csaba Kiraly
20 e7f1eb59 Csaba Kiraly
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 0db6fe80 Csaba Kiraly
26
27 9f26d895 Csaba Kiraly
function usage {
28 4b8f86e3 Csaba Kiraly
   echo "Usage: $0 init <orig video> <codec settings>";
29 fb38685e Csaba Kiraly
   echo "       $0 calc <chunksize> <file with losses>";
30 9f26d895 Csaba Kiraly
   exit $1 
31
}
32
33
function init {
34 4b8f86e3 Csaba Kiraly
  [[ $# -eq 2 ]] || usage 1
35 ea716bdb Csaba Kiraly
36 4b8f86e3 Csaba Kiraly
  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 ea716bdb Csaba Kiraly
40
  #initialize by creating YUV, if needed
41 3519763d Csaba Kiraly
  rm $ORIG_YUV $ORIG_YUV_DESC
42 7b784289 cskiraly
  if [[ $ORIG =~ \.[yY][uU][vV]$ ]]; then #if it is aready a YUV, just link or copy
43 ea716bdb Csaba Kiraly
    ln -s  $ORIG $ORIG_YUV || cp $ORIG $ORIG_YUV
44 4b8f86e3 Csaba Kiraly
    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 ea716bdb Csaba Kiraly
  else
49 4b8f86e3 Csaba Kiraly
    #$FFMPEG -i $ORIG -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r $VIDEO_FPS $ORIG_YUV >${PSNR_TMPPREFIX}ffmpeg.log
50 628daf9f Csaba Kiraly
    $FFMPEG -y -i $ORIG $ORIG_YUV >${PSNR_TMPPREFIX}ffmpeg.log 2>&1
51 4b8f86e3 Csaba Kiraly
52
    # Stream #0.0: Video: h264, yuv420p, 352x288, 25 tbr, 1200k tbn, 50 tbc
53
    TMP=`grep "Stream.*Video:" ${PSNR_TMPPREFIX}ffmpeg.log`
54 7b2f8bba Csaba Kiraly
#    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 4b8f86e3 Csaba Kiraly
      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 ea716bdb Csaba Kiraly
  fi
67 9f26d895 Csaba Kiraly
68 ea716bdb Csaba Kiraly
  #$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 628daf9f Csaba Kiraly
  $FFMPEG -y -s ${VIDEO_XSIZE}x${VIDEO_YSIZE} -r $VIDEO_FPS -i $ORIG_YUV $CODER $ORIG_MP4 >>${PSNR_TMPPREFIX}ffmpeg.log
70 36907371 Csaba Kiraly
  $ANALYSER $ORIG_MP4 > ${PSNR_TMPPREFIX}orig_log.txt
71 9f26d895 Csaba Kiraly
}
72
73
function calc {
74 fb38685e Csaba Kiraly
  [[ $# -eq 2 ]] || usage 1
75 9f26d895 Csaba Kiraly
76
  CHUNK_SIZE=$1
77
  LOST_FILE=$2
78
79 ea716bdb Csaba Kiraly
  . $ORIG_YUV_DESC
80
81 9f26d895 Csaba Kiraly
  #cut lost chunks
82 ea716bdb Csaba Kiraly
  rm $RECEIVED_MP4 # FIXME: I have no idea why this was needed, but sometimes cutter wasn't overwriting
83 46c2411e Luca
  $CUTTER $ORIG_MP4 $RECEIVED_MP4 $LOST_FILE $CHUNK_SIZE 2> ${PSNR_TMPPREFIX}cutter_log.txt
84 9f26d895 Csaba Kiraly
85
  #get chunk count and avgsize
86 36907371 Csaba Kiraly
  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 9f26d895 Csaba Kiraly
  #CHUNK_SIZE_AVG=`(echo - | awk '{ print $BYTES/$CHUNKS}')`
90
91
  #analyse losses
92 36907371 Csaba Kiraly
  $ANALYSER $RECEIVED_MP4 > ${PSNR_TMPPREFIX}received_log.txt
93 9f26d895 Csaba Kiraly
94
  #reconstruct sequence by duplicating 
95 36907371 Csaba Kiraly
  $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 ea716bdb Csaba Kiraly
  $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 9f26d895 Csaba Kiraly
100 36907371 Csaba Kiraly
  PSNR=`awk '/^psnr:/ {print $8}' ${PSNR_TMPPREFIX}psnravg_log.txt`
101 9f26d895 Csaba Kiraly
102 ea716bdb Csaba Kiraly
  $EVALPSNR $VIDEO_XSIZE $VIDEO_YSIZE 420 $ORIG_YUV $REFILLED_YUV ssim >${PSNR_TMPPREFIX}ssim_log.txt 2>${PSNR_TMPPREFIX}ssimavg_log.txt
103 fb92c9bb Csaba Kiraly
104
  SSIM=`awk '/^ssim:/ {print $8}' ${PSNR_TMPPREFIX}ssimavg_log.txt`
105
106 9f26d895 Csaba Kiraly
  echo "bytes: $BYTES"
107
  echo "chunks: $CHUNKS"
108
  echo "chunkslost: $CHUNKSLOST"
109
  echo "frames: $FRAMES"
110
  echo "frameslost: $FRAMESLOST"
111
  echo "psnr: $PSNR"
112 fb92c9bb Csaba Kiraly
  echo "ssim: $SSIM"
113 9f26d895 Csaba Kiraly
}
114
115 2c8d6927 Csaba Kiraly
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 9f26d895 Csaba Kiraly
[[ $# -ge 1 ]] || usage 1
128
CMD=$1;
129
shift
130
131
case $CMD in
132
  init)
133 6a5fc5e8 Csaba Kiraly
    init "$@"
134 9f26d895 Csaba Kiraly
    ;;
135
  calc)
136 6a5fc5e8 Csaba Kiraly
    calc "$@"
137 9f26d895 Csaba Kiraly
    ;;
138 2c8d6927 Csaba Kiraly
  play)
139
    play "$@"
140
    ;;
141 9f26d895 Csaba Kiraly
  *)
142
    usage 1
143
    ;;
144
esac