aboutsummaryrefslogtreecommitdiffstats
path: root/src/descriptors/dr_a1.c
blob: e855c1989e7bb33935ff353742058f313308316a (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
/*
Copyright (C) 2012  Yonathan Yusim

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

dr_a1.c

Service Location Descriptor.

*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#if defined(HAVE_INTTYPES_H)
#include <inttypes.h>
#elif defined(HAVE_STDINT_H)
#include <stdint.h>
#endif

#include "../dvbpsi.h"
#include "../dvbpsi_private.h"
#include "../descriptor.h"

#include "dr_a1.h"


/*****************************************************************************
 * dvbpsi_DecodeServiceLocationDr
 *****************************************************************************/
dvbpsi_service_location_dr_t *
dvbpsi_DecodeServiceLocationDr (dvbpsi_descriptor_t * p_descriptor)
{
  dvbpsi_service_location_dr_t *p_decoded;
  uint8_t *buf = p_descriptor->p_data;

  /* Check the tag */
  if (p_descriptor->i_tag != 0xa1)
    return NULL;

  /* Don't decode twice */
  if (p_descriptor->p_decoded)
    return p_descriptor->p_decoded;

  /* Check length */
  if ((p_descriptor->i_length - 3) % 6)
    return NULL;

  /* Allocate memory */
  p_decoded =
    (dvbpsi_service_location_dr_t *)
    malloc (sizeof (dvbpsi_service_location_dr_t));
  if (!p_decoded)
    return NULL;

  memset (p_decoded, 0, sizeof (dvbpsi_service_location_dr_t));

  p_descriptor->p_decoded = (void *) p_decoded;

  p_decoded->i_pcr_pid = ((uint16_t) (buf[0] & 0x1f) << 8) | buf[1];
  p_decoded->i_number_elements = buf[2];

  buf += 3;

  for (int i = 0; i < p_decoded->i_number_elements; i++)
    {
      dvbpsi_service_location_element_t *p_element =
	(dvbpsi_service_location_element_t *)
	malloc (sizeof (dvbpsi_service_location_element_t));

      if (!p_element)
	return NULL;

      memset (p_element, 0, sizeof (dvbpsi_service_location_element_t));

      p_element->i_stream_type = buf[0];
      p_element->i_elementary_pid =
	((uint16_t) (buf[1] & 0x1f) << 8) | buf[2];
      memcpy (p_element->i_iso_639_code, &buf[3], 3);

      if (p_decoded->p_first_element == NULL)
	p_decoded->p_first_element = p_element;
      else
	{
	  dvbpsi_service_location_element_t *p_last_element =
	    p_decoded->p_first_element;
	  while (p_last_element->p_next != NULL)
	    p_last_element = p_last_element->p_next;
	  p_last_element->p_next = p_element;
	}

      buf += 6;
    }


  return p_decoded;
}

/*****************************************************************************
 * dvbpsi_FreeServiceLocationDr
 *****************************************************************************/
void
dvbpsi_FreeServiceLocationDr (dvbpsi_service_location_dr_t * descriptor)
{
  dvbpsi_service_location_element_t *p_element = NULL;

  if (descriptor == NULL)
    return;

  p_element = descriptor->p_first_element;

  while (p_element)
    {
      dvbpsi_service_location_element_t *p_next = p_element->p_next;
      free (p_element);
      p_element = p_next;
    }
}

#if 0
/*****************************************************************************
 * dvbpsi_GenServiceDr
 *****************************************************************************/
dvbpsi_descriptor_t *
dvbpsi_GenServiceDr (dvbpsi_service_location_dr_t * p_decoded,
		     bool b_duplicate)
{
  /* Create the descriptor */
  dvbpsi_descriptor_t *p_descriptor =
    dvbpsi_NewDescriptor (0x48,
			  3 + p_decoded->i_service_location_name_length +
			  p_decoded->i_service_location_provider_name_length,
			  NULL);

  if (p_descriptor)
    {
      /* Encode data */
      p_descriptor->p_data[0] = p_decoded->i_service_type;
      p_descriptor->p_data[1] = p_decoded->i_service_provider_name_length;
      if (p_decoded->i_service_provider_name_length)
	memcpy (p_descriptor->p_data + 2,
		p_decoded->i_service_provider_name,
		p_decoded->i_service_provider_name_length);
      p_descriptor->p_data[2 + p_decoded->i_service_provider_name_length] =
	p_decoded->i_service_name_length;
      if (p_decoded->i_service_name_length)
	memcpy (p_descriptor->p_data + 3 +
		p_decoded->i_service_provider_name_length,
		p_decoded->i_service_name, p_decoded->i_service_name_length);

      if (b_duplicate)
	{
	  /* Duplicate decoded data */
	  dvbpsi_service_dr_t *p_dup_decoded =
	    (dvbpsi_service_dr_t *) malloc (sizeof (dvbpsi_service_dr_t));
	  if (p_dup_decoded)
	    memcpy (p_dup_decoded, p_decoded, sizeof (dvbpsi_service_dr_t));

	  p_descriptor->p_decoded = (void *) p_dup_decoded;
	}
    }

  return p_descriptor;
}
#endif

Privacy Policy