Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 351 lines (277 sloc) 11.75 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
#!/usr/bin/env python

# * Copyright 2011 Alistair Buxton <a.j.buxton@gmail.com>
# *
# * License: This program is free software; you can redistribute it and/or
# * modify it under the terms of the GNU General Public License as published
# * by the Free Software Foundation; either version 3 of the License, or (at
# * your option) any later version. This program is distributed in the hope
# * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.

# This is the main data analyser.

import sys
import os
import numpy as np
from scipy.ndimage import gaussian_filter1d as gauss
from scipy.optimize import fminbound

import pylab

from util import paritybytes, setbyte, normalise, hammbytes, allbytes, mrag, notzero

import util

from guess import Guess

import time

import finders
import math

np.seterr(invalid= 'raise')

class Vbi(object):
    '''This class represents a line of raw vbi data and all our attempts to
decode it.'''

    possible_bytes = [hammbytes]*2 + [paritybytes]*40

    def __init__(self, vbi, bitwidth=5.112, gauss_sd=1.1, gauss_sd_offset=2.0,
                 offset_low = 75.0, offset_high = 119.0,
                 thresh_low = 1.1, thresh_high = 2.36,
                 allow_unmatched = True, find=finders.all_headers):

        # data arrays

        # vbi is the raw line as an array of 2048 floats
        self.vbi = vbi

        # blurring amounts
        self.gauss_sd = gauss_sd
        self.gauss_sd_offset = gauss_sd_offset

        # Offset range to check for signal drift, in samples.
        # The algorithm will check with sub sample accuracy.
        # The offset finder is very fast (it uses bisection)
        # so this range can be relatively large. But not too
        # large or you get false positives.
        self.offset_low = offset_low
        self.offset_high = offset_high

        # black level of the signal
        self.black = np.mean(self.vbi[:80])

        # Threshold multipliers. The black level of the signal
        # is derived from the mean of the area before the VBI
        # begins. This is multiplied by the following factors
        # to give the low/high thresholds. Anything outside
        # this range is assumed to be a 0 or a 1. Tweaking these
        # can improve results, but often at a speed cost.
        self.thresh_low = self.black*thresh_low
        self.thresh_high = self.black*thresh_high

        # Allow vbi.py to emitt packet 0's that don't match
        # any finder? Set to false when you have finders
        # for all headers in the data.
        self.allow_unmatched = allow_unmatched
        self.finders = find

        # vbi packet bytewise
        self._mask0 = np.zeros(42, dtype=np.uint8)
        self._mask1 = np.zeros(42, dtype=np.uint8)

        self.g = Guess(bitwidth=bitwidth)


    def find_offset_and_scale(self):
        '''Tries to find the offset of the vbi data in the raw samples.'''

        # Split into chunks and ensure there is something "interesting" in each
        target = gauss(self.vbi, self.gauss_sd_offset)
        d = [np.std(target[x:x+128]) < 5.0 for x in range(64, 1440, 128)]
        if any(d):
            return False

        low = 64
        high = 256
        target = gauss(self.vbi[low:high], self.gauss_sd_offset)

        def _inner(offset):
            self.g.set_offset(offset)

            self.g.update_cri(low, high)
            guess_scaled = self.g.convolved[low:high]
            mask_scaled = self.g.mask[low:high]

            a = guess_scaled*mask_scaled
            b = np.clip(target*mask_scaled, self.black, 256)

            scale = a.std()/b.std()
            b -= self.black
            b *= scale
            a = np.clip(a, 0, 256*scale)

            return np.sum(np.square(b-a))

        offset = fminbound(_inner, self.offset_low, self.offset_high)

        # call it also to set self.offset and self.scale
        return (_inner(offset) < 10)

    def make_guess_mask(self):
        a = []

        for i in range(42*8):
            (low, high) = self.g.get_bit_pos(i)
            a.append(self.vbi[low:high])

        mins = np.array([min(x) for x in a])
        maxs = np.array([max(x) for x in a])
        avgs = np.array([np.array(x).mean() for x in a])

        for i in range(42):
            mini = mins[i*8:(i+1)*8]
            maxi = maxs[i*8:(i+1)*8]
            avgi = avgs[i*8:(i+1)*8]
            self._mask0[i] = 0xff
            for j in range(8):
                if mini[j] < self.thresh_low:
                    self._mask0[i] &= ~(1<<j)
                if maxi[j] > self.thresh_high:
                    self._mask1[i] |= (1<<j)

        tmp = self._mask1 & self._mask0
        self._mask0 |= self._mask1
        self._mask1 = tmp

    def make_possible_bytes(self, possible_bytes):
        def masked(b, n):
            m0 = util.m0s[self._mask0[n]]
            m1 = util.m1s[self._mask1[n]]
            m = m0 & m1 & b
            if m:
                return m
            else:
                mm0 = m0 & b
                mm1 = m1 & b
                if len(mm0) < len(mm1):
                    return mm0 or mm1 or b
                else:
                    return mm1 or mm0 or b

        self.possible_bytes = [masked(b,n) for n,b in enumerate(possible_bytes)]

    def _deconvolve_make_diff(self, (low, high)):
        a = normalise(self.g.convolved)
        diff_sq = np.square(a - self.target)
        return np.sum(diff_sq)
        # an interesting trick I discovered.
        # bias the result towards the curent area of interest
        return np.sum(diff_sq[:low]) + 2.6*np.sum(diff_sq[low:high]) + np.sum(diff_sq[high:])

    def _deconvolve_pass(self, first=0, last=42):
        for n in range(first, last):
            nb = self.possible_bytes[n]

            changed = self.g.set_update_range(n+4, 1)

            if len(nb) == 100000:
                self.g.set_byte(n, nb[0])
            else:
                ans = []
                for b1 in nb:
                    self.g.set_byte(n, b1)
                    ans.append((self._deconvolve_make_diff(changed),b1))

                best = min(ans)
                self.g.set_byte(n, best[1])
        self.g.update_all()

    def _deconvolve(self):
        for it in range(10):
            self._deconvolve_pass()
            # if this iteration didn't produce a change in the answer
            # then the next one won't either, so stop.
            if (self.g.bytes == self._oldbytes).all():
                #print it
                break
            self._oldbytes[:] = self.g.bytes

    def _nzdeconvolve(self):
        for it in range(10):
            ans=[]
            changed = self.g.set_update_range(4, 2)
            for nb in notzero:
                self.g.set_two_bytes(0, nb[0], nb[1])
                ans.append((self._deconvolve_make_diff(changed),nb))
            best = min(ans)
            self.g.set_two_bytes(0, best[1][0], best[1][1])

            self._deconvolve_pass(first=2)
            # if this iteration didn't produce a change in the answer
            # then the next one won't either, so stop.
            if (self.g.bytes == self._oldbytes).all():
                #print it
                break
            self._oldbytes[:] = self.g.bytes

    def deconvolve(self):
        target = gauss(self.vbi, self.gauss_sd)
        self.target = normalise(target)

        self.make_guess_mask()
        self.make_possible_bytes(Vbi.possible_bytes)

        self._oldbytes = np.zeros(42, dtype=np.uint8)

        self._deconvolve()

        packet = "".join([chr(x) for x in self.g.bytes])

        F = finders.test(self.finders, packet)
        if F:
                sys.stderr.write("matched by finder "+F.name+"\n");
                sys.stderr.flush()
                self.make_possible_bytes(F.possible_bytes)
                self._deconvolve()
                F.find(self.g.bytes)
                packet = F.fixup()
                return packet

        # if the packet did not match any of the finders then it isn't
        # a packet 0 (or 30). if the packet still claims to be a packet 0 it
        # will mess up the page splitter. so redo the deconvolution but with
        # packet 0 (and 30) header removed from possible bytes.

        # note: this doesn't work. i am not sure why. a packet in 63322
        # does not match the finders but still passes through this next check
        # with r=0. which should be impossible.
        ((m,r),e) = mrag(self.g.bytes[:2])
        if r == 0:
            sys.stderr.write("packet falsely claimed to be packet %d\n" % r);
            sys.stderr.flush()
            if not self.allow_unmatched:
                self._nzdeconvolve()
            packet = "".join([chr(x) for x in self.g.bytes])
        # if it's a link packet, it is completely hammed
        elif r == 27:
            self.make_possible_bytes([hammbytes]*42)
            self._deconvolve()
            packet = "".join([chr(x) for x in self.g.bytes])

        return packet
            
        


def process_file((inname, outname)):
  print inname, outname
  try:
    data = np.fromstring(file(inname).read(), dtype=np.uint8)
    outfile = file(outname, 'wb')
    for line in range(32):
        offset = line*2048
        vbiraw = data[offset:offset+2048]
        v = Vbi(vbiraw)
        tmp = v.find_offset_and_scale()
        if tmp:
            outfile.write(v.deconvolve())
        else:
            outfile.write("\xff"*42)
    outfile.close()
  except IOError:
    pass


def test_file(outfile):
    return os.path.isfile(outfile) and (os.path.getsize(outfile) == (42 * 32))

def list_files(inputpath, outputpath, first, count, skip):
    for frame in range(first, first+count, skip):
        frame = "%08d" % frame
        if test_file(outputpath + '/' + frame + '.t42'):
            #print "skipping %s\n" % (outputpath + '/' + frame + '.t42')
            pass
        else:
            yield (inputpath + '/' + frame + '.vbi',
                  outputpath + '/' + frame + '.t42')


if __name__ == '__main__':
    import multiprocessing
    from multiprocessing.pool import IMapIterator, Pool
    import cProfile
    import pstats
    def wrapper(func):
      def wrap(self, timeout=None):
        # Note: the timeout of 1 googol seconds introduces a rather subtle
        # bug for Python scripts intended to run many times the age of the universe.
        return func(self, timeout=timeout if timeout is not None else 1e100)
      return wrap
    IMapIterator.next = wrapper(IMapIterator.next)


    try:
        path = sys.argv[1]
    except:
        print "Usage:", sys.argv[0], "<path> [<first> <count>]\n"
        print " path: directory with VBI files to process"
        print " first: first file to process"
        print " count: maximum number of files to process\n"
        exit(-1)

    try:
        first = int(sys.argv[2], 10)
        count = int(sys.argv[3], 10)
        skip = int(sys.argv[4], 10)
    except:
        first = 0
        count = 10000000
        skip = 1


    if not os.path.isdir(path+'/t42/'):
        os.makedirs(path+'/t42/')


    if 1:
        p = Pool(multiprocessing.cpu_count())
        it = p.imap(process_file, list_files(path+'/vbi/', path+'/t42/', first, count, skip), chunksize=1)
        for i in it:
            pass

    else: # single thread mode for debugging
        def doit():
            map(process_file, list_files(path+'/vbi/', path+'/t42/', first, count, skip))
        cProfile.run('doit()', 'myprofile')
        p = pstats.Stats('myprofile')
        p.sort_stats('cumulative').print_stats(50)
Something went wrong with that request. Please try again.