Houston, we have a trunk.
[debian/gnuradio] / gr-atsc / src / python / atsc_utils.py
1 #
2 # Copyright 2006 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
20
21
22 import random
23 import sys
24
25 MPEG_SYNC_BYTE = 0x47
26
27 def make_fake_transport_stream_packet(npkts):
28     """
29     Return a sequence of 8-bit ints that represents an MPEG Transport Stream packet.
30
31     @param npkts: how many 188-byte packets to return
32
33     FYI, each ATSC Data Frame contains two Data Fields, each of which contains
34     312 data segments.  Each transport stream packet maps to a data segment.
35     """
36     r = [0] * (npkts * 188)
37     i = 0
38     for j in range(npkts):
39         r[i+0] = MPEG_SYNC_BYTE
40         r[i+1] = random.randint(0, 127) # top bit (transport error bit) clear
41         i = i + 2
42         for n in range(186):
43             r[i + n] = random.randint(0, 255)
44         i = i + 186
45         
46     return r
47
48
49 def pad_stream(src, sizeof_total, sizeof_pad):
50     sizeof_valid = sizeof_total - sizeof_pad
51     assert sizeof_valid > 0
52     assert (len(src) % sizeof_valid) == 0
53     npkts = len(src) // sizeof_valid
54     dst = [0] * (npkts * sizeof_total)
55     for i in range(npkts):
56         src_s = i * sizeof_valid
57         dst_s = i * sizeof_total
58         dst[dst_s:dst_s + sizeof_valid] = src[src_s:src_s + sizeof_valid]
59     return dst
60
61
62 def depad_stream(src, sizeof_total, sizeof_pad):
63     sizeof_valid = sizeof_total - sizeof_pad
64     assert sizeof_valid > 0
65     assert (len(src) % sizeof_total) == 0
66     npkts = len(src) // sizeof_total
67     dst = [0] * (npkts * sizeof_valid)
68     for i in range(npkts):
69         src_s = i * sizeof_total
70         dst_s = i * sizeof_valid
71         dst[dst_s:dst_s + sizeof_valid] = src[src_s:src_s + sizeof_valid]
72     return dst
73
74