Improve gop detection

The previous version assumed there was no padding done with
adaptation field. Since this happens in our files, modified it
to check for start of PES packet and set GOP start when the
random access indicator is 1.

Change-Id: Ide0004547ca115ee4aa90ee04d8f89ee915f656f
diff --git a/probatron/analyzer b/probatron/analyzer
index e90d06d..0b02941 100755
--- a/probatron/analyzer
+++ b/probatron/analyzer
@@ -77,14 +77,35 @@
 
 
 ## Calculate the GOP between each keyframe given an input file (1) to the given output file (2)
-function calulateGOP() {
+function calculateGOP() {
   OUTPUT_FILE=$2
 
   ## GOP - Algorithm stolen Shamelessly From Zeev Leiber
   PID=$(ffprobe -i $1 2>&1 | grep "Video:" | sed 's/[][]/ /g' | awk '{print "echo $((" $3 "))"}' | bash)
   echo "   Processing $1 (Video PID $PID)..." 
-  
-  GOPs=$(dvbsnoop -s ts -if $1 $PID | grep random_access | awk '{print $2}')
+
+  ## Searching for random_access of 0 could cause invalid results because of padding adaptation fields
+  linecount=0
+  random_access=0
+  ## 14 (1 for match, 12 after, 1 separator) lines per packet to decide if it is a new GOP
+  GOPs=$(
+    dvbsnoop -s ts -if $1 $PID | grep -A 12 'Packet data starts' | while read -r h
+    do
+      if echo $h | grep 'random_access_indicator: 1' > /dev/null
+      then
+        random_access=1
+      fi
+      linecount=`expr $linecount + 1`
+      if [[ $linecount -ge 14 ]]
+      then
+        echo $random_access
+        random_access=0
+        linecount=0
+      fi
+    done
+    echo $random_access
+  )
+
   FRAME_COUNT=0
   for g in $GOPs
   do
@@ -93,13 +114,14 @@
       if [[ $FRAME_COUNT -gt 0 ]]
       then
         echo $NUM_GOP	$1	$FRAME_COUNT >> "$OUTPUT_FILE"
-        FRAME_COUNT=1
-        NUM_GOP=`expr $NUM_GOP + 1`
       fi
+      FRAME_COUNT=1
+      NUM_GOP=`expr $NUM_GOP + 1`
     else 
       FRAME_COUNT=`expr $FRAME_COUNT + 1`
     fi
   done
+  echo $NUM_GOP $1  $FRAME_COUNT >> "$OUTPUT_FILE"
 }
 
 
@@ -170,7 +192,7 @@
   NUM_GOP=1
   for f in $FILES
   do
-  	calulateGOP $f $1
+  	calculateGOP $f $1
   done
 
   echo
@@ -184,7 +206,7 @@
   echo "# NUM	File	GOP" > $2
 
   NUM_GOP=1
-  calulateGOP $1 $2
+  calculateGOP $1 $2
 
   echo
   echo "Result written to $2"