xref: /aosp_15_r20/external/libopus/dnn/torch/rdovae/packets/fec_packets.py (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1"""
2/* Copyright (c) 2022 Amazon
3   Written by Jan Buethe */
4/*
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   - Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11
12   - Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in the
14   documentation and/or other materials provided with the distribution.
15
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28"""
29
30import numpy as np
31
32
33
34def write_fec_packets(filename, packets, rates=None):
35    """ writes packets in binary format """
36
37    assert np.dtype(np.float32).itemsize == 4
38    assert np.dtype(np.int16).itemsize == 2
39
40    # derive some sizes
41    num_packets             = len(packets)
42    subframes_per_packet    = packets[0].shape[-2]
43    num_features            = packets[0].shape[-1]
44
45    # size of float is 4
46    subframe_size           = num_features * 4
47    packet_size             = subframe_size * subframes_per_packet + 2 # two bytes for rate
48
49    version = 1
50    # header size (version, header_size, num_packets, packet_size, subframe_size, subrames_per_packet, num_features)
51    header_size = 14
52
53    with open(filename, 'wb') as f:
54
55        # header
56        f.write(np.int16(version).tobytes())
57        f.write(np.int16(header_size).tobytes())
58        f.write(np.int16(num_packets).tobytes())
59        f.write(np.int16(packet_size).tobytes())
60        f.write(np.int16(subframe_size).tobytes())
61        f.write(np.int16(subframes_per_packet).tobytes())
62        f.write(np.int16(num_features).tobytes())
63
64        # packets
65        for i, packet in enumerate(packets):
66            if type(rates) == type(None):
67                rate = 0
68            else:
69                rate = rates[i]
70
71            f.write(np.int16(rate).tobytes())
72
73            features = np.flip(packet, axis=-2)
74            f.write(features.astype(np.float32).tobytes())
75
76
77def read_fec_packets(filename):
78    """ reads packets from binary format """
79
80    assert np.dtype(np.float32).itemsize == 4
81    assert np.dtype(np.int16).itemsize == 2
82
83    with open(filename, 'rb') as f:
84
85        # header
86        version                 = np.frombuffer(f.read(2), dtype=np.int16).item()
87        header_size             = np.frombuffer(f.read(2), dtype=np.int16).item()
88        num_packets             = np.frombuffer(f.read(2), dtype=np.int16).item()
89        packet_size             = np.frombuffer(f.read(2), dtype=np.int16).item()
90        subframe_size           = np.frombuffer(f.read(2), dtype=np.int16).item()
91        subframes_per_packet    = np.frombuffer(f.read(2), dtype=np.int16).item()
92        num_features            = np.frombuffer(f.read(2), dtype=np.int16).item()
93
94        dummy_features          = np.zeros((subframes_per_packet, num_features), dtype=np.float32)
95
96        # packets
97        rates = []
98        packets = []
99        for i in range(num_packets):
100
101            rate = np.frombuffer(f.read(2), dtype=np.int16).item
102            rates.append(rate)
103
104            features = np.reshape(np.frombuffer(f.read(subframe_size * subframes_per_packet), dtype=np.float32), dummy_features.shape)
105            packet = np.flip(features, axis=-2)
106            packets.append(packet)
107
108    return packets