desc_81.h 7.43 KB
Newer Older
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
/*****************************************************************************
 * desc_81.h: ATSC A/52 Descriptor 0x81: AC-3 descriptor
 *****************************************************************************
 * Copyright (C) 2019 OpenHeadend
 *
 * Authors: Arnaud de Turckheim <adeturckheim@openheadend.tv>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *****************************************************************************/

#ifndef __BITSTREAM_ATSC_DESC_81_H__
#define __BITSTREAM_ATSC_DESC_81_H__

#include <bitstream/common.h>
#include <bitstream/mpeg/psi/descriptors.h>

#ifdef __cplusplus
extern "C"
{
#endif

#define DESC81_HEADER_SIZE      (DESC_HEADER_SIZE + 3)

static inline void desc81_init(uint8_t *p_desc)
{
    desc_set_tag(p_desc, 0x81);
}

#define DESC81_SAMPLE_RATE_CODE_48                  0x00
#define DESC81_SAMPLE_RATE_CODE_44_1                0x01
#define DESC81_SAMPLE_RATE_CODE_32                  0x02
#define DESC81_SAMPLE_RATE_CODE_48_OR_44_1          0x04
#define DESC81_SAMPLE_RATE_CODE_48_OR_32            0x05
#define DESC81_SAMPLE_RATE_CODE_44_1_OR_32          0x06
#define DESC81_SAMPLE_RATE_CODE_48_OR_44_1_OR_32    0x07

static inline void desc81_set_sample_rate_code(uint8_t *p_desc,
                                               uint8_t sample_rate)
{
    p_desc[2] = (p_desc[2] & 0x3f) | (sample_rate << 5);
}

static inline uint8_t desc81_get_sample_rate_code(const uint8_t *p_desc)
{
    return p_desc[2] >> 5;
}

static inline void desc81_set_bsid(uint8_t *p_desc, uint8_t bsid)
{
    p_desc[2] = (p_desc[2] & 0xd0) | (bsid & 0x1f);
}

static inline uint8_t desc81_get_bsid(const uint8_t *p_desc)
{
    return p_desc[2] & 0x1f;
}

static inline uint8_t desc81_bit_rate_code_from_octetrate(uint64_t octetrate)
{
    unsigned table[19] = {
        32, 40, 48, 56, 64, 80,
        96, 112, 128, 160, 192, 224,
        256, 320, 384, 448, 512, 576,
        640,
    };
    for (unsigned i = 0; i < 19; i++) {
84
        if (octetrate * 8 == table[i] * 1000)
85
            return i;
86
        else if (octetrate * 8 < table[i] * 1000)
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
            return 0x20 | i;
    }
    return 0xff;
}

static inline void desc81_set_bit_rate_code(uint8_t *p_desc,
                                            uint8_t bit_rate_code)
{
    p_desc[3] = (p_desc[3] & 0x03) | (bit_rate_code << 2);
}

static inline uint8_t desc81_get_bit_rate_code(const uint8_t *p_desc)
{
    return p_desc[3] >> 2;
}

static inline void desc81_set_surround_mode(uint8_t *p_desc,
                                            uint8_t surround_mode)
{
    p_desc[3] = (p_desc[3] & 0xfc) | (surround_mode & 0x03);
}

static inline uint8_t desc81_get_surround_mode(const uint8_t *p_desc)
{
    return p_desc[3] & 0x03;
}

static inline void desc81_set_bsmod(uint8_t *p_desc, uint8_t bsmod)
{
    p_desc[4] = (p_desc[4] & 0x1f) | (bsmod << 5);
}

static inline uint8_t desc81_get_bsmod(const uint8_t *p_desc)
{
    return p_desc[4] >> 5;
}

static inline void desc81_set_num_channels(uint8_t *p_desc,
                                           uint8_t num_channels)
{
    p_desc[4] = (p_desc[4] & 0xe1) | ((num_channels & 0x0f) << 1);
}

static inline uint8_t desc81_get_num_channels(const uint8_t *p_desc)
{
    return (p_desc[4] & 0x1e) >> 1;
}

static inline void desc81_set_full_svc(uint8_t *p_desc, uint8_t full_svc)
{
    p_desc[4] = (p_desc[4] & 0xfe) | (full_svc & 0x01);
}

static inline uint8_t desc81_get_full_svc(const uint8_t *p_desc)
{
    return p_desc[4] & 0x1;
}

static inline void desc81_set_langcod(uint8_t *p_desc, uint8_t langcod)
{
    p_desc[5] = langcod;
}

static inline uint8_t desc81_get_langcod(const uint8_t *p_desc)
{
    return p_desc[5];
}

static inline void desc81_set_langcod2(uint8_t *p_desc, uint8_t langcod2)
{
    if (desc81_get_num_channels(p_desc) == 0)
        p_desc[6] = langcod2;
}

static inline uint8_t desc81_get_langcod2(const uint8_t *p_desc)
{
    return p_desc[6];
}

static inline void desc81_set_mainid(uint8_t *p_desc, uint8_t mainid)
{
    if (desc81_get_bsmod(p_desc) < 2)
        p_desc[7] = (p_desc[7] & 0x1f) | (mainid << 5);
}

static inline uint8_t desc81_get_mainid(const uint8_t *p_desc)
{
    return p_desc[7] >> 5;
}

static inline void desc81_set_priority(uint8_t *p_desc, uint8_t priority)
{
    p_desc[7] = (p_desc[7] & 0xe7) | ((priority & 0x3) << 3);
}

static inline uint8_t desc81_get_priority(const uint8_t *p_desc)
{
    return (p_desc[7] & 0x18) >> 3;
}

static inline void desc81_set_asvcflags(uint8_t *p_desc, uint8_t asvcflags)
{
    p_desc[7] = asvcflags;
}

static inline uint8_t desc81_get_asvcflags(const uint8_t *p_desc)
{
    return p_desc[7];
}

static inline bool desc81_validate(const uint8_t *p_desc)
{
    uint8_t i_size = (DESC81_HEADER_SIZE - DESC_HEADER_SIZE);
    return desc_get_length(p_desc) >= i_size;
}

static inline void desc81_print(const uint8_t *p_desc, f_print pf_print,
                                void *opaque, print_type_t i_print_type)
{
    uint8_t sample_rate_code = desc81_get_sample_rate_code(p_desc);
    uint8_t bsid = desc81_get_bsid(p_desc);
    uint8_t bit_rate_code = desc81_get_bit_rate_code(p_desc);
    uint8_t surround_mode = desc81_get_surround_mode(p_desc);
    uint8_t bsmod = desc81_get_bsmod(p_desc);
    uint8_t num_channels = desc81_get_num_channels(p_desc);
    uint8_t full_svc = desc81_get_full_svc(p_desc);

    switch (i_print_type) {
    case PRINT_XML:
        pf_print(opaque,
                 "<AC3_DESC"
                 " sample_rate=\"0x%02x\""
                 " bsid=\"0x%02x\""
                 " bit_rate=\"0x%02x\""
                 " surround_mode=\"0x%02x\""
                 " bsmod=\"0x%02x\""
                 " num_channels=\"0x%02x\""
                 " full_svc=\"%u\"/>",
                 sample_rate_code,
                 bsid,
                 bit_rate_code,
                 surround_mode,
                 bsmod,
                 num_channels,
                 full_svc);
        break;
    default:
        pf_print(opaque,
                 "    - desc 81 ac-3"
                 " sample_rate=\"0x%02x\""
                 " bsid=\"0x%02x\""
                 " bit_rate=\"0x%02x\""
                 " surround_mode=\"0x%02x\""
                 " bsmod=\"0x%02x\""
                 " num_channels=\"0x%02x\""
                 " full_svc=\"%u\"",
                 sample_rate_code,
                 bsid,
                 bit_rate_code,
                 surround_mode,
                 bsmod,
                 num_channels,
                 full_svc);
        break;
    }
}

#ifdef __cplusplus
}
#endif

#endif