summaryrefslogtreecommitdiff
path: root/sys/dvb/camutils.c
blob: 721b5cd96d24aa2058380aa6690ac096cb1348e3 (plain)
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
/*
 * camutils.c - 
 * Copyright (C) 2007 Alessandro Decina
 * 
 * Authors:
 *   Alessandro Decina <alessandro@nnva.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
 * with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS

#include <gst/gst.h>
#include <string.h>

#include "cam.h"
#include "camutils.h"

#define GST_CAT_DEFAULT cam_debug_cat

/* From the spec: 
 * length_field() {
 *               size_indicator
 *               if (size_indicator == 0)
 *                        length_value
 *               else if (size_indicator == 1) {
 *                        length_field_size
 *                        for (i=0; i<length_field_size; i++) {
 *                                length_value_byte
 *                        }
 *               }
 * }
*/

guint8
cam_calc_length_field_size (guint length)
{
  guint field_len;

  if (length < G_MAXUINT8)
    field_len = 1;
  else if (length <= G_MAXUINT16)
    field_len = 3;
  else if (length <= (1 << 24) - 1)
    field_len = 4;
  else
    field_len = 5;

  return field_len;
}

/* write a length_field */
guint8
cam_write_length_field (guint8 * buff, guint length)
{
  guint8 field_len = cam_calc_length_field_size (length);

  if (buff) {
    switch (field_len) {
      case 1:
        buff[0] = length;
        break;
      case 2:
        g_return_val_if_reached (0);
        break;
      case 3:
        buff[0] = TPDU_HEADER_SIZE_INDICATOR | (field_len - 1);
        buff[1] = length >> 8;
        buff[2] = length & 0xFF;
        break;
      case 4:
        buff[0] = TPDU_HEADER_SIZE_INDICATOR | (field_len - 1);
        buff[1] = length >> 16;
        buff[2] = (length >> 8) & 0xFF;
        buff[3] = length & 0xFF;
        break;
      case 5:
        buff[0] = TPDU_HEADER_SIZE_INDICATOR | (field_len - 1);
        buff[1] = length >> 24;
        buff[2] = (length >> 16) & 0xFF;
        buff[3] = (length >> 8) & 0xFF;
        buff[4] = length & 0xFF;
        break;
      default:
        g_return_val_if_reached (0);
    }
  }

  return field_len;
}

/* read a length_field */
guint8
cam_read_length_field (guint8 * buff, guint * length)
{
  guint i;
  guint field_len;
  guint8 len;

  if (buff[0] <= G_MAXINT8) {
    field_len = 1;
    len = buff[0];
  } else {
    field_len = buff[0] & ~TPDU_HEADER_SIZE_INDICATOR;
    if (field_len > 4) {
      GST_ERROR ("length_field length exceeds 4 bytes: %d", field_len);
      field_len = 0;
      len = 0;
    } else {
      len = 0;
      for (i = 0; i < field_len; ++i)
        len = (len << 8) | *++buff;

      /* count the size indicator byte */
      field_len += 1;
    }
  }

  if (length)
    *length = len;

  return field_len;
}

/*
 * ca_pmt () {
 *    ca_pmt_tag                                              24        uimsbf
 *    length_field()
 *    ca_pmt_list_management                                   8        uimsbf
 *    program_number                                          16        uimsbf
 *    reserved                                                 2        bslbf
 *    version_number                                           5        uimsbf
 *    current_next_indicator                                   1        bslbf
 *    reserved                                                 4        bslbf
 *    program_info_length                                     12        uimsbf
 *    if (program_info_length != 0) {
 *      ca_pmt_cmd_id at program level                         8        uimsbf
 *      for (i=0; i<n; i++) {
 *        CA_descriptor() programme level 
 *      }
 *   }
 *   for (i=0; i<n; i++) {
 *     stream_type                                             8       uimsbf
 *     reserved                                                3       bslbf
 *     elementary_PID  elementary stream PID                  13        uimsbf
 *     reserved                                                4       bslbf
 *     ES_info_length                                         12        uimsbf
 *     if (ES_info_length != 0) {
 *       ca_pmt_cmd_id at ES level                             8       uimsbf
 *       for (i=0; i<n; i++) {
 *         CA_descriptor() elementary stream level 
 *       }
 *     }
 *   }
 * }
 */

static guint
get_ca_descriptors_length (GValueArray * descriptors)
{
  guint i;
  guint len = 0;
  GValue *value;
  GString *desc;

  if (descriptors != NULL) {
    for (i = 0; i < descriptors->n_values; ++i) {
      value = g_value_array_get_nth (descriptors, i);
      desc = (GString *) g_value_get_boxed (value);

      if (desc->str[0] == 0x09)
        len += desc->len;
    }
  }

  return len;
}

static guint8 *
write_ca_descriptors (guint8 * body, GValueArray * descriptors)
{
  guint i;
  GValue *value;
  GString *desc;

  if (descriptors != NULL) {
    for (i = 0; i < descriptors->n_values; ++i) {
      value = g_value_array_get_nth (descriptors, i);
      desc = (GString *) g_value_get_boxed (value);

      if (desc->str[0] == 0x09) {
        memcpy (body, desc->str, desc->len);
        body += desc->len;
      }
    }
  }

  return body;
}

guint8 *
cam_build_ca_pmt (GstStructure * pmt, guint8 list_management, guint8 cmd_id,
    guint * size)
{
  guint body_size = 0;
  guint8 *buffer;
  guint8 *body;
  GList *lengths = NULL;
  guint len = 0;
  const GValue *streams;
  guint program_number;
  guint version_number;
  guint i;
  const GValue *value;
  GstStructure *stream;
  GValueArray *program_descriptors = NULL;
  GValueArray *stream_descriptors = NULL;

  gst_structure_get_uint (pmt, "program-number", &program_number);
  gst_structure_get_uint (pmt, "version-number", &version_number);
  streams = gst_structure_get_value (pmt, "streams");
  value = gst_structure_get_value (pmt, "descriptors");
  if (value != NULL) {
    program_descriptors = g_value_get_boxed (value);
    /* get the length of program level CA_descriptor()s */
    len = get_ca_descriptors_length (program_descriptors);
    if (len > 0)
      /* add one byte for the program level cmd_id */
      len += 1;
  }
  lengths = g_list_append (lengths, GINT_TO_POINTER (len));
  body_size += 6 + len;

  /* get the length of stream level CA_descriptor()s */
  if (streams != NULL) {
    for (i = 0; i < gst_value_list_get_size (streams); ++i) {
      value = gst_value_list_get_value (streams, i);
      stream = g_value_get_boxed (value);

      value = gst_structure_get_value (stream, "descriptors");
      if (value != NULL) {
        stream_descriptors = g_value_get_boxed (value);

        len = get_ca_descriptors_length (stream_descriptors);
        if (len > 0)
          /* one byte for the stream level cmd_id */
          len += 1;
      }

      lengths = g_list_append (lengths, GINT_TO_POINTER (len));
      body_size += 5 + len;

    }
  }
  buffer = g_malloc0 (body_size);
  body = buffer;

  *body++ = list_management;

  GST_WRITE_UINT16_BE (body, program_number);
  body += 2;

  *body++ = (version_number << 1) | 0x01;

  len = GPOINTER_TO_INT (lengths->data);
  lengths = g_list_delete_link (lengths, lengths);
  GST_WRITE_UINT16_BE (body, len);
  body += 2;

  if (len != 0) {
    *body++ = cmd_id;

    body = write_ca_descriptors (body, program_descriptors);
  }

  for (i = 0; i < gst_value_list_get_size (streams); ++i) {
    guint stream_type;
    guint stream_pid;

    value = gst_value_list_get_value (streams, i);
    stream = g_value_get_boxed (value);

    gst_structure_get_uint (stream, "stream-type", &stream_type);
    gst_structure_get_uint (stream, "pid", &stream_pid);
    value = gst_structure_get_value (stream, "descriptors");
    stream_descriptors = g_value_get_boxed (value);

    *body++ = stream_type;
    GST_WRITE_UINT16_BE (body, stream_pid);
    body += 2;
    len = GPOINTER_TO_INT (lengths->data);
    lengths = g_list_delete_link (lengths, lengths);
    GST_WRITE_UINT16_BE (body, len);
    body += 2;

    if (len != 0) {
      *body++ = cmd_id;
      body = write_ca_descriptors (body, stream_descriptors);
    }
  }

  *size = body_size;
  return buffer;
}