1 /*****************************************************************************
2 * sdt.c: SDT decoder/generator
3 *----------------------------------------------------------------------------
4 * Copyright (C) 2001-2011 VideoLAN
5 * $Id$
6 *
7 * Authors: Johan Bilien <jobi@via.ecp.fr>\
8 * Jean-Paul Saman <jpsaman@videolan.org>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 *----------------------------------------------------------------------------
25 *
26 *****************************************************************************/
27
28 #include "config.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdbool.h>
33 #include <string.h>
34
35 #if defined(HAVE_INTTYPES_H)
36 #include <inttypes.h>
37 #elif defined(HAVE_STDINT_H)
38 #include <stdint.h>
39 #endif
40
41 #include <assert.h>
42
43 #include "../dvbpsi.h"
44 #include "../dvbpsi_private.h"
45 #include "../psi.h"
46 #include "../descriptor.h"
47 #include "../demux.h"
48 #include "sdt.h"
49 #include "sdt_private.h"
50
51 /*****************************************************************************
52 * dvbpsi_sdt_attach
53 *****************************************************************************
54 * Initialize a SDT subtable decoder.
55 *****************************************************************************/
dvbpsi_sdt_attach(dvbpsi_t * p_dvbpsi,uint8_t i_table_id,uint16_t i_extension,dvbpsi_sdt_callback pf_callback,void * p_cb_data)56 bool dvbpsi_sdt_attach(dvbpsi_t *p_dvbpsi, uint8_t i_table_id, uint16_t i_extension,
57 dvbpsi_sdt_callback pf_callback, void* p_cb_data)
58 {
59 assert(p_dvbpsi);
60 assert(p_dvbpsi->p_decoder);
61
62 dvbpsi_demux_t* p_demux = (dvbpsi_demux_t*)p_dvbpsi->p_decoder;
63
64 if (dvbpsi_demuxGetSubDec(p_demux, i_table_id, i_extension))
65 {
66 dvbpsi_error(p_dvbpsi, "SDT decoder",
67 "Already a decoder for (table_id == 0x%02x,"
68 "extension == 0x%02x)",
69 i_table_id, i_extension);
70 return false;
71 }
72
73 dvbpsi_sdt_decoder_t* p_sdt_decoder;
74 p_sdt_decoder = (dvbpsi_sdt_decoder_t*) dvbpsi_decoder_new(NULL,
75 0, true, sizeof(dvbpsi_sdt_decoder_t));
76 if (p_sdt_decoder == NULL)
77 return false;
78
79 /* subtable decoder configuration */
80 dvbpsi_demux_subdec_t* p_subdec;
81 p_subdec = dvbpsi_NewDemuxSubDecoder(i_table_id, i_extension, dvbpsi_sdt_detach,
82 dvbpsi_sdt_sections_gather, DVBPSI_DECODER(p_sdt_decoder));
83 if (p_subdec == NULL)
84 {
85 dvbpsi_decoder_delete(DVBPSI_DECODER(p_sdt_decoder));
86 return false;
87 }
88
89 /* Attach the subtable decoder to the demux */
90 dvbpsi_AttachDemuxSubDecoder(p_demux, p_subdec);
91
92 /* SDT decoder information */
93 p_sdt_decoder->pf_sdt_callback = pf_callback;
94 p_sdt_decoder->p_cb_data = p_cb_data;
95 p_sdt_decoder->p_building_sdt = NULL;
96
97 return true;
98 }
99
100 /*****************************************************************************
101 * dvbpsi_sdt_detach
102 *****************************************************************************
103 * Close a SDT decoder.
104 *****************************************************************************/
dvbpsi_sdt_detach(dvbpsi_t * p_dvbpsi,uint8_t i_table_id,uint16_t i_extension)105 void dvbpsi_sdt_detach(dvbpsi_t *p_dvbpsi, uint8_t i_table_id, uint16_t i_extension)
106 {
107 assert(p_dvbpsi);
108 assert(p_dvbpsi->p_decoder);
109
110 dvbpsi_demux_t *p_demux = (dvbpsi_demux_t *) p_dvbpsi->p_decoder;
111
112 dvbpsi_demux_subdec_t* p_subdec;
113 p_subdec = dvbpsi_demuxGetSubDec(p_demux, i_table_id, i_extension);
114 if (p_subdec == NULL)
115 {
116 dvbpsi_error(p_dvbpsi, "SDT Decoder",
117 "No such SDT decoder (table_id == 0x%02x,"
118 "extension == 0x%02x)",
119 i_table_id, i_extension);
120 return;
121 }
122
123 assert(p_subdec->p_decoder);
124
125 dvbpsi_sdt_decoder_t* p_sdt_decoder;
126 p_sdt_decoder = (dvbpsi_sdt_decoder_t*)p_subdec->p_decoder;
127 if (p_sdt_decoder->p_building_sdt)
128 dvbpsi_sdt_delete(p_sdt_decoder->p_building_sdt);
129 p_sdt_decoder->p_building_sdt = NULL;
130
131 /* Free sub table decoder */
132 dvbpsi_DetachDemuxSubDecoder(p_demux, p_subdec);
133 dvbpsi_DeleteDemuxSubDecoder(p_subdec);
134 }
135
136 /*****************************************************************************
137 * dvbpsi_sdt_init
138 *****************************************************************************
139 * Initialize a pre-allocated dvbpsi_sdt_t structure.
140 *****************************************************************************/
dvbpsi_sdt_init(dvbpsi_sdt_t * p_sdt,uint8_t i_table_id,uint16_t i_extension,uint8_t i_version,bool b_current_next,uint16_t i_network_id)141 void dvbpsi_sdt_init(dvbpsi_sdt_t* p_sdt, uint8_t i_table_id, uint16_t i_extension,
142 uint8_t i_version, bool b_current_next, uint16_t i_network_id)
143 {
144 assert(p_sdt);
145
146 p_sdt->i_table_id = i_table_id;
147 p_sdt->i_extension = i_extension;
148
149 p_sdt->i_version = i_version;
150 p_sdt->b_current_next = b_current_next;
151 p_sdt->i_network_id = i_network_id;
152 p_sdt->p_first_service = NULL;
153 }
154
155 /*****************************************************************************
156 * dvbpsi_sdt_new
157 *****************************************************************************
158 * Allocate and Initialize a new dvbpsi_sdt_t structure.
159 *****************************************************************************/
dvbpsi_sdt_new(uint8_t i_table_id,uint16_t i_extension,uint8_t i_version,bool b_current_next,uint16_t i_network_id)160 dvbpsi_sdt_t *dvbpsi_sdt_new(uint8_t i_table_id, uint16_t i_extension, uint8_t i_version,
161 bool b_current_next, uint16_t i_network_id)
162 {
163 dvbpsi_sdt_t *p_sdt = (dvbpsi_sdt_t*)malloc(sizeof(dvbpsi_sdt_t));
164 if (p_sdt != NULL)
165 dvbpsi_sdt_init(p_sdt, i_table_id, i_extension, i_version,
166 b_current_next, i_network_id);
167 return p_sdt;
168 }
169
170 /*****************************************************************************
171 * dvbpsi_sdt_empty
172 *****************************************************************************
173 * Clean a dvbpsi_sdt_t structure.
174 *****************************************************************************/
dvbpsi_sdt_empty(dvbpsi_sdt_t * p_sdt)175 void dvbpsi_sdt_empty(dvbpsi_sdt_t* p_sdt)
176 {
177 dvbpsi_sdt_service_t* p_service = p_sdt->p_first_service;
178
179 while (p_service != NULL)
180 {
181 dvbpsi_sdt_service_t* p_tmp = p_service->p_next;
182 dvbpsi_DeleteDescriptors(p_service->p_first_descriptor);
183 free(p_service);
184 p_service = p_tmp;
185 }
186 p_sdt->p_first_service = NULL;
187 }
188
189 /*****************************************************************************
190 * dvbpsi_sdt_delete
191 *****************************************************************************
192 * Clean and Delete dvbpsi_sdt_t structure.
193 *****************************************************************************/
dvbpsi_sdt_delete(dvbpsi_sdt_t * p_sdt)194 void dvbpsi_sdt_delete(dvbpsi_sdt_t *p_sdt)
195 {
196 if (p_sdt)
197 dvbpsi_sdt_empty(p_sdt);
198 free(p_sdt);
199 }
200
201 /*****************************************************************************
202 * dvbpsi_sdt_service_add
203 *****************************************************************************
204 * Add a service description at the end of the SDT.
205 *****************************************************************************/
dvbpsi_sdt_service_add(dvbpsi_sdt_t * p_sdt,uint16_t i_service_id,bool b_eit_schedule,bool b_eit_present,uint8_t i_running_status,bool b_free_ca)206 dvbpsi_sdt_service_t *dvbpsi_sdt_service_add(dvbpsi_sdt_t* p_sdt,
207 uint16_t i_service_id,
208 bool b_eit_schedule,
209 bool b_eit_present,
210 uint8_t i_running_status,
211 bool b_free_ca)
212 {
213 dvbpsi_sdt_service_t * p_service;
214 p_service = (dvbpsi_sdt_service_t*)calloc(1, sizeof(dvbpsi_sdt_service_t));
215 if (p_service == NULL)
216 return NULL;
217
218 p_service->i_service_id = i_service_id;
219 p_service->b_eit_schedule = b_eit_schedule;
220 p_service->b_eit_present = b_eit_present;
221 p_service->i_running_status = i_running_status;
222 p_service->b_free_ca = b_free_ca;
223 p_service->p_next = NULL;
224 p_service->p_first_descriptor = NULL;
225
226 if (p_sdt->p_first_service == NULL)
227 p_sdt->p_first_service = p_service;
228 else
229 {
230 dvbpsi_sdt_service_t * p_last_service = p_sdt->p_first_service;
231 while(p_last_service->p_next != NULL)
232 p_last_service = p_last_service->p_next;
233 p_last_service->p_next = p_service;
234 }
235
236 return p_service;
237 }
238
239 /*****************************************************************************
240 * dvbpsi_sdt_service_descriptor_add
241 *****************************************************************************
242 * Add a descriptor in the SDT service description.
243 *****************************************************************************/
dvbpsi_sdt_service_descriptor_add(dvbpsi_sdt_service_t * p_service,uint8_t i_tag,uint8_t i_length,uint8_t * p_data)244 dvbpsi_descriptor_t *dvbpsi_sdt_service_descriptor_add(
245 dvbpsi_sdt_service_t *p_service,
246 uint8_t i_tag, uint8_t i_length,
247 uint8_t *p_data)
248 {
249 dvbpsi_descriptor_t * p_descriptor;
250 p_descriptor = dvbpsi_NewDescriptor(i_tag, i_length, p_data);
251 if (p_descriptor == NULL)
252 return NULL;
253
254 p_service->p_first_descriptor = dvbpsi_AddDescriptor(p_service->p_first_descriptor,
255 p_descriptor);
256 assert(p_service->p_first_descriptor);
257 if (p_service->p_first_descriptor == NULL)
258 return NULL;
259
260 return p_descriptor;
261 }
262
263 /* */
dvbpsi_ReInitSDT(dvbpsi_sdt_decoder_t * p_decoder,const bool b_force)264 static void dvbpsi_ReInitSDT(dvbpsi_sdt_decoder_t* p_decoder, const bool b_force)
265 {
266 assert(p_decoder);
267
268 dvbpsi_decoder_reset(DVBPSI_DECODER(p_decoder), b_force);
269
270 /* Force redecoding */
271 if (b_force)
272 {
273 /* Free structures */
274 if (p_decoder->p_building_sdt)
275 dvbpsi_sdt_delete(p_decoder->p_building_sdt);
276 }
277 p_decoder->p_building_sdt = NULL;
278 }
279
dvbpsi_CheckSDT(dvbpsi_t * p_dvbpsi,dvbpsi_sdt_decoder_t * p_sdt_decoder,dvbpsi_psi_section_t * p_section)280 static bool dvbpsi_CheckSDT(dvbpsi_t *p_dvbpsi, dvbpsi_sdt_decoder_t *p_sdt_decoder,
281 dvbpsi_psi_section_t *p_section)
282 {
283 bool b_reinit = false;
284 assert(p_dvbpsi);
285 assert(p_sdt_decoder);
286
287 if (p_sdt_decoder->p_building_sdt->i_extension != p_section->i_extension)
288 {
289 /* transport_stream_id */
290 dvbpsi_error(p_dvbpsi, "SDT decoder",
291 "'transport_stream_id' differs"
292 " whereas no TS discontinuity has occured");
293 b_reinit = true;
294 }
295 else if (p_sdt_decoder->p_building_sdt->i_version != p_section->i_version)
296 {
297 /* version_number */
298 dvbpsi_error(p_dvbpsi, "SDT decoder",
299 "'version_number' differs"
300 " whereas no discontinuity has occured");
301 b_reinit = true;
302 }
303 else if (p_sdt_decoder->i_last_section_number != p_section->i_last_number)
304 {
305 /* last_section_number */
306 dvbpsi_error(p_dvbpsi, "SDT decoder",
307 "'last_section_number' differs"
308 " whereas no discontinuity has occured");
309 b_reinit = true;
310 }
311
312 return b_reinit;
313 }
314
dvbpsi_AddSectionSDT(dvbpsi_t * p_dvbpsi,dvbpsi_sdt_decoder_t * p_sdt_decoder,dvbpsi_psi_section_t * p_section)315 static bool dvbpsi_AddSectionSDT(dvbpsi_t *p_dvbpsi, dvbpsi_sdt_decoder_t *p_sdt_decoder,
316 dvbpsi_psi_section_t* p_section)
317 {
318 assert(p_dvbpsi);
319 assert(p_sdt_decoder);
320 assert(p_section);
321
322 /* Initialize the structures if it's the first section received */
323 if (!p_sdt_decoder->p_building_sdt)
324 {
325 p_sdt_decoder->p_building_sdt =
326 dvbpsi_sdt_new(p_section->i_table_id, p_section->i_extension,
327 p_section->i_version, p_section->b_current_next,
328 ((uint16_t)(p_section->p_payload_start[0]) << 8)
329 | p_section->p_payload_start[1]);
330
331 if (p_sdt_decoder->p_building_sdt == NULL)
332 return false;
333 p_sdt_decoder->i_last_section_number = p_section->i_last_number;
334 }
335
336 /* Add to linked list of sections */
337 if (dvbpsi_decoder_psi_section_add(DVBPSI_DECODER(p_sdt_decoder), p_section))
338 dvbpsi_debug(p_dvbpsi, "SDT decoder", "overwrite section number %d",
339 p_section->i_number);
340
341 return true;
342 }
343
344 /*****************************************************************************
345 * dvbpsi_sdt_sections_gather
346 *****************************************************************************
347 * Callback for the subtable demultiplexor.
348 *****************************************************************************/
dvbpsi_sdt_sections_gather(dvbpsi_t * p_dvbpsi,dvbpsi_decoder_t * p_private_decoder,dvbpsi_psi_section_t * p_section)349 void dvbpsi_sdt_sections_gather(dvbpsi_t *p_dvbpsi,
350 dvbpsi_decoder_t *p_private_decoder,
351 dvbpsi_psi_section_t * p_section)
352 {
353 assert(p_dvbpsi);
354 assert(p_dvbpsi->p_decoder);
355
356 const uint8_t i_table_id = (p_section->i_table_id == 0x42 ||
357 p_section->i_table_id == 0x46) ?
358 p_section->i_table_id : 0x42;
359
360 if (!dvbpsi_CheckPSISection(p_dvbpsi, p_section, i_table_id, "SDT decoder"))
361 {
362 dvbpsi_DeletePSISections(p_section);
363 return;
364 }
365
366 /* We have a valid SDT section */
367 dvbpsi_demux_t *p_demux = (dvbpsi_demux_t *)p_dvbpsi->p_decoder;
368 dvbpsi_sdt_decoder_t *p_sdt_decoder
369 = (dvbpsi_sdt_decoder_t*)p_private_decoder;
370
371 /* TS discontinuity check */
372 if (p_demux->b_discontinuity)
373 {
374 dvbpsi_ReInitSDT(p_sdt_decoder, true);
375 p_sdt_decoder->b_discontinuity = false;
376 p_demux->b_discontinuity = false;
377 }
378 else
379 {
380 /* Perform a few sanity checks */
381 if (p_sdt_decoder->p_building_sdt)
382 {
383 if (dvbpsi_CheckSDT(p_dvbpsi, p_sdt_decoder, p_section))
384 dvbpsi_ReInitSDT(p_sdt_decoder, true);
385 }
386 else
387 {
388 if( (p_sdt_decoder->b_current_valid)
389 && (p_sdt_decoder->current_sdt.i_version == p_section->i_version)
390 && (p_sdt_decoder->current_sdt.b_current_next == p_section->b_current_next))
391 {
392 /* Don't decode since this version is already decoded */
393 dvbpsi_debug(p_dvbpsi, "SDT decoder",
394 "ignoring already decoded section %d",
395 p_section->i_number);
396 dvbpsi_DeletePSISections(p_section);
397 return;
398 }
399 }
400 }
401
402 /* Add section to SDT */
403 if (!dvbpsi_AddSectionSDT(p_dvbpsi, p_sdt_decoder, p_section))
404 {
405 dvbpsi_error(p_dvbpsi, "SDT decoder", "failed decoding section %d",
406 p_section->i_number);
407 dvbpsi_DeletePSISections(p_section);
408 return;
409 }
410
411 /* Check if we have all the sections */
412 if (dvbpsi_decoder_psi_sections_completed(DVBPSI_DECODER(p_sdt_decoder)))
413 {
414 assert(p_sdt_decoder->pf_sdt_callback);
415
416 /* Save the current information */
417 p_sdt_decoder->current_sdt = *p_sdt_decoder->p_building_sdt;
418 p_sdt_decoder->b_current_valid = true;
419 /* Decode the sections */
420 dvbpsi_sdt_sections_decode(p_sdt_decoder->p_building_sdt,
421 p_sdt_decoder->p_sections);
422 /* signal the new SDT */
423 p_sdt_decoder->pf_sdt_callback(p_sdt_decoder->p_cb_data,
424 p_sdt_decoder->p_building_sdt);
425 /* Delete sections and Reinitialize the structures */
426 dvbpsi_ReInitSDT(p_sdt_decoder, false);
427 assert(p_sdt_decoder->p_sections == NULL);
428 }
429 }
430
431 /*****************************************************************************
432 * dvbpsi_sdt_sections_decode
433 *****************************************************************************
434 * SDT decoder.
435 *****************************************************************************/
dvbpsi_sdt_sections_decode(dvbpsi_sdt_t * p_sdt,dvbpsi_psi_section_t * p_section)436 void dvbpsi_sdt_sections_decode(dvbpsi_sdt_t* p_sdt,
437 dvbpsi_psi_section_t* p_section)
438 {
439 uint8_t *p_byte, *p_end;
440
441 while (p_section)
442 {
443 for (p_byte = p_section->p_payload_start + 3;
444 p_byte + 4 < p_section->p_payload_end;)
445 {
446 uint16_t i_service_id = ((uint16_t)(p_byte[0]) << 8) | p_byte[1];
447 bool b_eit_schedule = ((p_byte[2] & 0x2) >> 1);
448 bool b_eit_present = ((p_byte[2]) & 0x1);
449 uint8_t i_running_status = (uint8_t)(p_byte[3]) >> 5;
450 bool b_free_ca = ((p_byte[3] & 0x10) >> 4);
451 uint16_t i_srv_length = ((uint16_t)(p_byte[3] & 0xf) <<8) | p_byte[4];
452 dvbpsi_sdt_service_t* p_service = dvbpsi_sdt_service_add(p_sdt,
453 i_service_id, b_eit_schedule, b_eit_present,
454 i_running_status, b_free_ca);
455
456 /* Service descriptors */
457 p_byte += 5;
458 p_end = p_byte + i_srv_length;
459 if( p_end > p_section->p_payload_end ) break;
460
461 while(p_byte + 2 <= p_end)
462 {
463 uint8_t i_tag = p_byte[0];
464 uint8_t i_length = p_byte[1];
465 if (i_length + 2 <= p_end - p_byte)
466 dvbpsi_sdt_service_descriptor_add(p_service, i_tag, i_length, p_byte + 2);
467 p_byte += 2 + i_length;
468 }
469 }
470 p_section = p_section->p_next;
471 }
472 }
473
474 /*****************************************************************************
475 * dvbpsi_sdt_sections_generate
476 *****************************************************************************
477 * Generate SDT sections based on the dvbpsi_sdt_t structure.
478 *****************************************************************************/
dvbpsi_sdt_sections_generate(dvbpsi_t * p_dvbpsi,dvbpsi_sdt_t * p_sdt)479 dvbpsi_psi_section_t *dvbpsi_sdt_sections_generate(dvbpsi_t *p_dvbpsi, dvbpsi_sdt_t* p_sdt)
480 {
481 dvbpsi_psi_section_t *p_result = dvbpsi_NewPSISection(1024);
482 dvbpsi_psi_section_t *p_current = p_result;
483 dvbpsi_psi_section_t *p_prev;
484
485 dvbpsi_sdt_service_t *p_service = p_sdt->p_first_service;
486
487 p_current->i_table_id = 0x42;
488 p_current->b_syntax_indicator = true;
489 p_current->b_private_indicator = true;
490 p_current->i_length = 12; /* header + CRC_32 */
491 p_current->i_extension = p_sdt->i_extension; /* is transport_stream_id */
492 p_current->i_version = p_sdt->i_version;
493 p_current->b_current_next = p_sdt->b_current_next;
494 p_current->i_number = 0;
495 p_current->p_payload_end += 11; /* just after the header */
496 p_current->p_payload_start = p_current->p_data + 8;
497
498 /* Original Network ID */
499 p_current->p_data[8] = (p_sdt->i_network_id >> 8) ;
500 p_current->p_data[9] = p_sdt->i_network_id;
501 p_current->p_data[10] = 0xff;
502
503 /* SDT service */
504 while (p_service != NULL)
505 {
506 uint8_t * p_service_start = p_current->p_payload_end;
507 uint16_t i_service_length = 5;
508
509 dvbpsi_descriptor_t * p_descriptor = p_service->p_first_descriptor;
510
511 while ((p_descriptor != NULL)&& ((p_service_start - p_current->p_data) + i_service_length <= 1020))
512 {
513 i_service_length += p_descriptor->i_length + 2;
514 p_descriptor = p_descriptor->p_next;
515 }
516
517 if ((p_descriptor != NULL) && (p_service_start - p_current->p_data != 11) && (i_service_length <= 1009))
518 {
519 /* will put more descriptors in an empty section */
520 dvbpsi_debug(p_dvbpsi, "SDT generator","create a new section to carry more Service descriptors");
521
522 p_prev = p_current;
523 p_current = dvbpsi_NewPSISection(1024);
524 p_prev->p_next = p_current;
525
526 p_current->i_table_id = 0x42;
527 p_current->b_syntax_indicator = true;
528 p_current->b_private_indicator = true;
529 p_current->i_length = 12; /* header + CRC_32 */
530 p_current->i_extension = p_sdt->i_extension;;
531 p_current->i_version = p_sdt->i_version;
532 p_current->b_current_next = p_sdt->b_current_next;
533 p_current->i_number = p_prev->i_number + 1;
534 p_current->p_payload_end += 11; /* just after the header */
535 p_current->p_payload_start = p_current->p_data + 8;
536
537 /* Original Network ID */
538 p_current->p_data[8] = (p_sdt->i_network_id >> 8) ;
539 p_current->p_data[9] = p_sdt->i_network_id;
540 p_current->p_data[10] = 0xff;
541
542 p_service_start = p_current->p_payload_end;
543 }
544
545 p_service_start[0] = (p_service->i_service_id >>8);
546 p_service_start[1] = (p_service->i_service_id );
547 p_service_start[2] = 0xfc | (p_service-> b_eit_schedule ? 0x2 : 0x0) | (p_service->b_eit_present ? 0x01 : 0x00);
548 p_service_start[3] = ((p_service->i_running_status & 0x07) << 5 ) | ((p_service->b_free_ca & 0x1) << 4);
549
550 /* Increase the length by 5 */
551 p_current->p_payload_end += 5;
552 p_current->i_length += 5;
553
554 /* ES descriptors */
555 p_descriptor = p_service->p_first_descriptor;
556 while ((p_descriptor != NULL) && ( (p_current->p_payload_end - p_current->p_data) + p_descriptor->i_length <= 1018))
557 {
558 /* p_payload_end is where the descriptor begins */
559 p_current->p_payload_end[0] = p_descriptor->i_tag;
560 p_current->p_payload_end[1] = p_descriptor->i_length;
561 memcpy(p_current->p_payload_end + 2, p_descriptor->p_data, p_descriptor->i_length);
562 /* Increase length by descriptor_length + 2 */
563 p_current->p_payload_end += p_descriptor->i_length + 2;
564 p_current->i_length += p_descriptor->i_length + 2;
565 p_descriptor = p_descriptor->p_next;
566 }
567
568 if (p_descriptor != NULL)
569 dvbpsi_error(p_dvbpsi, "SDT generator", "unable to carry all the descriptors");
570
571 /* ES_info_length */
572 i_service_length = p_current->p_payload_end - p_service_start - 5;
573 p_service_start[3] |= ((i_service_length >> 8) & 0x0f);
574 p_service_start[4] = i_service_length;
575
576 p_service = p_service->p_next;
577 }
578
579 /* Finalization */
580 p_prev = p_result;
581 while (p_prev != NULL)
582 {
583 p_prev->i_last_number = p_current->i_number;
584 dvbpsi_BuildPSISection(p_dvbpsi, p_prev);
585 p_prev = p_prev->p_next;
586 }
587 return p_result;
588 }
589