1 /*****************************************************************************
2  * bat.c: BAT decoder/generator
3  *----------------------------------------------------------------------------
4  * Copyright (C) 2001-2010 VideoLAN
5  * $Id: bat.c 110 2010-04-01 12:52:02Z gbazin $
6  *
7  * Authors: Zhu zhenglu <zhuzlu@gmail.com>
8  *          heavily based on nit.c which was written by
9  *          Johann Hanne
10  *          Jean-Paul Saman <jpsaman@videolan.org>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25  *
26  *----------------------------------------------------------------------------
27  *
28  *****************************************************************************/
29 
30 #include "config.h"
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdbool.h>
35 #include <string.h>
36 
37 #if defined(HAVE_INTTYPES_H)
38 #include <inttypes.h>
39 #elif defined(HAVE_STDINT_H)
40 #include <stdint.h>
41 #endif
42 
43 #include <assert.h>
44 
45 #include "../dvbpsi.h"
46 #include "../dvbpsi_private.h"
47 #include "../psi.h"
48 #include "../descriptor.h"
49 #include "../demux.h"
50 #include "bat.h"
51 #include "bat_private.h"
52 
53 /*****************************************************************************
54  * dvbpsi_bat_attach
55  *****************************************************************************
56  * Initialize a BAT subtable decoder.
57  *****************************************************************************/
dvbpsi_bat_attach(dvbpsi_t * p_dvbpsi,uint8_t i_table_id,uint16_t i_extension,dvbpsi_bat_callback pf_callback,void * p_cb_data)58 bool dvbpsi_bat_attach(dvbpsi_t *p_dvbpsi, uint8_t i_table_id,
59           uint16_t i_extension, dvbpsi_bat_callback pf_callback, void* p_cb_data)
60 {
61     assert(p_dvbpsi);
62     assert(p_dvbpsi->p_decoder);
63 
64     dvbpsi_demux_t* p_demux = (dvbpsi_demux_t*)p_dvbpsi->p_decoder;
65     if (dvbpsi_demuxGetSubDec(p_demux, i_table_id, i_extension))
66     {
67         dvbpsi_error(p_dvbpsi, "BAT decoder",
68                      "Already a decoder for (table_id == 0x%02x,"
69                      "extension == 0x%02x)",
70                      i_table_id, i_extension);
71         return false;
72     }
73 
74     dvbpsi_bat_decoder_t*  p_bat_decoder;
75     p_bat_decoder = (dvbpsi_bat_decoder_t*) dvbpsi_decoder_new(NULL,
76                                              0, true, sizeof(dvbpsi_bat_decoder_t));
77     if (p_bat_decoder == NULL)
78         return false;
79 
80     /* subtable decoder configuration */
81     dvbpsi_demux_subdec_t* p_subdec;
82     p_subdec = dvbpsi_NewDemuxSubDecoder(i_table_id, i_extension, dvbpsi_bat_detach,
83                                          dvbpsi_bat_sections_gather, DVBPSI_DECODER(p_bat_decoder));
84     if (p_subdec == NULL)
85     {
86         dvbpsi_decoder_delete(DVBPSI_DECODER(p_bat_decoder));
87         return false;
88     }
89 
90     /* Attach the subtable decoder to the demux */
91     dvbpsi_AttachDemuxSubDecoder(p_demux, p_subdec);
92 
93     /* BAT decoder information */
94     p_bat_decoder->pf_bat_callback = pf_callback;
95     p_bat_decoder->p_cb_data = p_cb_data;
96     p_bat_decoder->p_building_bat = NULL;
97 
98     return true;
99 }
100 
101 /*****************************************************************************
102  * dvbpsi_bat_detach
103  *****************************************************************************
104  * Close a BAT decoder.
105  *****************************************************************************/
dvbpsi_bat_detach(dvbpsi_t * p_dvbpsi,uint8_t i_table_id,uint16_t i_extension)106 void dvbpsi_bat_detach(dvbpsi_t *p_dvbpsi, uint8_t i_table_id, uint16_t i_extension)
107 {
108     assert(p_dvbpsi);
109     assert(p_dvbpsi->p_decoder);
110 
111     dvbpsi_demux_t *p_demux = (dvbpsi_demux_t *) p_dvbpsi->p_decoder;
112 
113     dvbpsi_demux_subdec_t* p_subdec;
114     p_subdec = dvbpsi_demuxGetSubDec(p_demux, i_table_id, i_extension);
115     if (p_subdec == NULL)
116     {
117         dvbpsi_error(p_dvbpsi, "BAT Decoder",
118                      "No such BAT decoder (table_id == 0x%02x,"
119                      "extension == 0x%02x)",
120                      i_table_id, i_extension);
121         return;
122     }
123 
124     dvbpsi_bat_decoder_t* p_bat_decoder;
125     p_bat_decoder = (dvbpsi_bat_decoder_t*)p_subdec->p_decoder;
126     if (p_bat_decoder->p_building_bat)
127         dvbpsi_bat_delete(p_bat_decoder->p_building_bat);
128     p_bat_decoder->p_building_bat = NULL;
129 
130     dvbpsi_DetachDemuxSubDecoder(p_demux, p_subdec);
131     dvbpsi_DeleteDemuxSubDecoder(p_subdec);
132 }
133 
134 /*****************************************************************************
135  * dvbpsi_bat_init
136  *****************************************************************************
137  * Initialize a pre-allocated dvbpsi_bat_t structure.
138  *****************************************************************************/
dvbpsi_bat_init(dvbpsi_bat_t * p_bat,uint8_t i_table_id,uint16_t i_extension,uint8_t i_version,bool b_current_next)139 void dvbpsi_bat_init(dvbpsi_bat_t* p_bat, uint8_t i_table_id, uint16_t i_extension,
140                      uint8_t i_version, bool b_current_next)
141 {
142     assert(p_bat);
143     p_bat->i_table_id = i_table_id;
144     p_bat->i_extension = i_extension;
145 
146     p_bat->i_version = i_version;
147     p_bat->b_current_next = b_current_next;
148     p_bat->p_first_ts = NULL;
149     p_bat->p_first_descriptor = NULL;
150 }
151 
152 /*****************************************************************************
153  * dvbpsi_bat_new
154  *****************************************************************************
155  * Allocate and initialize a dvbpsi_bat_t structure.
156  *****************************************************************************/
dvbpsi_bat_new(uint8_t i_table_id,uint16_t i_extension,uint8_t i_version,bool b_current_next)157 dvbpsi_bat_t *dvbpsi_bat_new(uint8_t i_table_id, uint16_t i_extension,
158                              uint8_t i_version, bool b_current_next)
159 {
160     dvbpsi_bat_t *p_bat = (dvbpsi_bat_t*)malloc(sizeof(dvbpsi_bat_t));
161     if(p_bat != NULL)
162         dvbpsi_bat_init(p_bat, i_table_id, i_extension, i_version, b_current_next);
163     return p_bat;
164 }
165 
166 /*****************************************************************************
167  * dvbpsi_bat_empty
168  *****************************************************************************
169  * Clean a dvbpsi_bat_t structure.
170  *****************************************************************************/
dvbpsi_bat_empty(dvbpsi_bat_t * p_bat)171 void dvbpsi_bat_empty(dvbpsi_bat_t* p_bat)
172 {
173     dvbpsi_bat_ts_t* p_ts = p_bat->p_first_ts;
174 
175     dvbpsi_DeleteDescriptors(p_bat->p_first_descriptor);
176     p_bat->p_first_descriptor = NULL;
177 
178     while (p_ts != NULL)
179     {
180         dvbpsi_bat_ts_t* p_tmp = p_ts->p_next;
181         dvbpsi_DeleteDescriptors(p_ts->p_first_descriptor);
182         free(p_ts);
183         p_ts = p_tmp;
184     }
185     p_bat->p_first_ts = NULL;
186 }
187 
188 /*****************************************************************************
189  * dvbpsi_bat_delete
190  *****************************************************************************
191  * Empty and Delere a dvbpsi_bat_t structure.
192  *****************************************************************************/
dvbpsi_bat_delete(dvbpsi_bat_t * p_bat)193 void dvbpsi_bat_delete(dvbpsi_bat_t *p_bat)
194 {
195     if (p_bat)
196         dvbpsi_bat_empty(p_bat);
197     free(p_bat);
198 }
199 
200 /*****************************************************************************
201  * dvbpsi_bat_bouquet_descriptor_add
202  *****************************************************************************
203  * Add a descriptor in the BAT.
204  *****************************************************************************/
dvbpsi_bat_bouquet_descriptor_add(dvbpsi_bat_t * p_bat,uint8_t i_tag,uint8_t i_length,uint8_t * p_data)205 dvbpsi_descriptor_t* dvbpsi_bat_bouquet_descriptor_add(dvbpsi_bat_t* p_bat,
206                                                        uint8_t i_tag, uint8_t i_length,
207                                                        uint8_t* p_data)
208 {
209     dvbpsi_descriptor_t* p_descriptor
210                         = dvbpsi_NewDescriptor(i_tag, i_length, p_data);
211     if (p_descriptor == NULL)
212         return NULL;
213 
214     p_bat->p_first_descriptor = dvbpsi_AddDescriptor(p_bat->p_first_descriptor,
215                                                      p_descriptor);
216     assert(p_bat->p_first_descriptor);
217     if (p_bat->p_first_descriptor == NULL)
218         return NULL;
219 
220     return p_descriptor;
221 }
222 
223 /*****************************************************************************
224  * dvbpsi_bat_ts_add
225  *****************************************************************************
226  * Add a TS description at the end of the BAT.
227  *****************************************************************************/
dvbpsi_bat_ts_add(dvbpsi_bat_t * p_bat,uint16_t i_ts_id,uint16_t i_orig_network_id)228 dvbpsi_bat_ts_t *dvbpsi_bat_ts_add(dvbpsi_bat_t* p_bat,
229                                  uint16_t i_ts_id, uint16_t i_orig_network_id)
230 {
231     dvbpsi_bat_ts_t * p_ts
232                 = (dvbpsi_bat_ts_t*)malloc(sizeof(dvbpsi_bat_ts_t));
233     if (p_ts == NULL)
234         return NULL;
235 
236     p_ts->i_ts_id = i_ts_id;
237     p_ts->i_orig_network_id = i_orig_network_id;
238     p_ts->p_next = NULL;
239     p_ts->p_first_descriptor = NULL;
240 
241     if (p_bat->p_first_ts == NULL)
242         p_bat->p_first_ts = p_ts;
243     else
244     {
245         dvbpsi_bat_ts_t * p_last_ts = p_bat->p_first_ts;
246         while(p_last_ts->p_next != NULL)
247             p_last_ts = p_last_ts->p_next;
248         p_last_ts->p_next = p_ts;
249     }
250 
251     return p_ts;
252 }
253 
254 
255 /*****************************************************************************
256  * dvbpsi_bat_ts_descriptor_add
257  *****************************************************************************
258  * Add a descriptor in the BAT TS descriptors, which is in the second loop of BAT.
259  *****************************************************************************/
dvbpsi_bat_ts_descriptor_add(dvbpsi_bat_ts_t * p_bat,uint8_t i_tag,uint8_t i_length,uint8_t * p_data)260 dvbpsi_descriptor_t *dvbpsi_bat_ts_descriptor_add(dvbpsi_bat_ts_t *p_bat,
261                                                uint8_t i_tag, uint8_t i_length,
262                                                uint8_t *p_data)
263 {
264     dvbpsi_descriptor_t * p_descriptor
265                         = dvbpsi_NewDescriptor(i_tag, i_length, p_data);
266     if (p_descriptor == NULL)
267         return NULL;
268 
269     if (p_bat->p_first_descriptor == NULL)
270         p_bat->p_first_descriptor = p_descriptor;
271     else
272     {
273         dvbpsi_descriptor_t *p_last_descriptor = p_bat->p_first_descriptor;
274         while(p_last_descriptor->p_next != NULL)
275             p_last_descriptor = p_last_descriptor->p_next;
276         p_last_descriptor->p_next = p_descriptor;
277     }
278     return p_descriptor;
279 }
280 
281 /* */
dvbpsi_ReInitBAT(dvbpsi_bat_decoder_t * p_decoder,const bool b_force)282 static void dvbpsi_ReInitBAT(dvbpsi_bat_decoder_t* p_decoder, const bool b_force)
283 {
284     assert(p_decoder);
285 
286     dvbpsi_decoder_reset(DVBPSI_DECODER(p_decoder), b_force);
287 
288     /* Force redecoding */
289     if (b_force)
290     {
291         /* Free structures */
292         if (p_decoder->p_building_bat)
293             dvbpsi_bat_delete(p_decoder->p_building_bat);
294     }
295     p_decoder->p_building_bat = NULL;
296 }
297 
dvbpsi_CheckBAT(dvbpsi_t * p_dvbpsi,dvbpsi_bat_decoder_t * p_bat_decoder,dvbpsi_psi_section_t * p_section)298 static bool dvbpsi_CheckBAT(dvbpsi_t *p_dvbpsi, dvbpsi_bat_decoder_t *p_bat_decoder,
299                             dvbpsi_psi_section_t *p_section)
300 {
301     bool b_reinit = false;
302     assert(p_dvbpsi);
303     assert(p_bat_decoder);
304 
305     if (p_bat_decoder->p_building_bat->i_extension != p_section->i_extension)
306     {
307         /* bouquet_id */
308         dvbpsi_error(p_dvbpsi, "BAT decoder", "'bouquet_id' differs"
309                         " whereas no TS discontinuity has occured");
310         b_reinit = true;
311     }
312     else if (p_bat_decoder->p_building_bat->i_version
313                                         != p_section->i_version)
314     {
315         /* version_number */
316         dvbpsi_error(p_dvbpsi, "BAT decoder", "'version_number' differs"
317                         " whereas no discontinuity has occured");
318         b_reinit = true;
319     }
320     else if (p_bat_decoder->i_last_section_number !=
321                                         p_section->i_last_number)
322     {
323         /* last_section_number */
324         dvbpsi_error(p_dvbpsi, "BAT decoder", "'last_section_number' differs"
325                         " whereas no discontinuity has occured");
326         b_reinit = true;
327     }
328 
329     return b_reinit;
330 }
331 
dvbpsi_AddSectionBAT(dvbpsi_t * p_dvbpsi,dvbpsi_bat_decoder_t * p_bat_decoder,dvbpsi_psi_section_t * p_section)332 static bool dvbpsi_AddSectionBAT(dvbpsi_t *p_dvbpsi, dvbpsi_bat_decoder_t *p_bat_decoder,
333                                  dvbpsi_psi_section_t* p_section)
334 {
335     assert(p_dvbpsi);
336     assert(p_bat_decoder);
337     assert(p_section);
338 
339     /* Initialize the structures if it's the first section received */
340     if (!p_bat_decoder->p_building_bat)
341     {
342         p_bat_decoder->p_building_bat = dvbpsi_bat_new(
343                               p_section->i_table_id, p_section->i_extension,
344                               p_section->i_version, p_section->b_current_next);
345         if (!p_bat_decoder->p_building_bat)
346             return false;
347 
348         p_bat_decoder->i_last_section_number = p_section->i_last_number;
349     }
350 
351     /* Add to linked list of sections */
352     if (dvbpsi_decoder_psi_section_add(DVBPSI_DECODER(p_bat_decoder), p_section))
353         dvbpsi_debug(p_dvbpsi, "BAT decoder", "overwrite section number %d",
354                      p_section->i_number);
355 
356     return true;
357 }
358 
359 /*****************************************************************************
360  * dvbpsi_bat_sections_gather
361  *****************************************************************************
362  * Callback for the subtable demultiplexor.
363  *****************************************************************************/
dvbpsi_bat_sections_gather(dvbpsi_t * p_dvbpsi,dvbpsi_decoder_t * p_decoder,dvbpsi_psi_section_t * p_section)364 void dvbpsi_bat_sections_gather(dvbpsi_t *p_dvbpsi,
365                               dvbpsi_decoder_t *p_decoder,
366                               dvbpsi_psi_section_t * p_section)
367 {
368     dvbpsi_demux_t *p_demux = (dvbpsi_demux_t *) p_dvbpsi->p_decoder;
369     dvbpsi_bat_decoder_t * p_bat_decoder = (dvbpsi_bat_decoder_t *) p_decoder;
370 
371     assert(p_dvbpsi);
372     assert(p_dvbpsi->p_decoder);
373 
374     if (!dvbpsi_CheckPSISection(p_dvbpsi, p_section, 0x4a, "BAT decoder"))
375     {
376         dvbpsi_DeletePSISections(p_section);
377         return;
378     }
379 
380     /* We have a valid BAT section */
381     if (p_demux->b_discontinuity)
382     {
383         dvbpsi_ReInitBAT(p_bat_decoder, true);
384         p_bat_decoder->b_discontinuity = false;
385         p_demux->b_discontinuity = false;
386     }
387     else
388     {
389         /* Perform a few sanity checks */
390         if (p_bat_decoder->p_building_bat)
391         {
392             if (dvbpsi_CheckBAT(p_dvbpsi, p_bat_decoder, p_section))
393                 dvbpsi_ReInitBAT(p_bat_decoder, true);
394         }
395         else
396         {
397             if (   (p_bat_decoder->b_current_valid)
398                 && (p_bat_decoder->current_bat.i_version == p_section->i_version)
399                 && (p_bat_decoder->current_bat.b_current_next ==
400                                                p_section->b_current_next))
401             {
402                 /* Don't decode since this version is already decoded */
403                 dvbpsi_debug(p_dvbpsi, "BAT decoder",
404                              "ignoring already decoded section %d",
405                              p_section->i_number);
406                 dvbpsi_DeletePSISections(p_section);
407                 return;
408             }
409 #if 0 /* FIXME: Is this really needed ? */
410             else if (  (!p_bat_decoder->current_bat.b_current_next)
411                      && (p_section->b_current_next))
412             {
413                 /* Signal a new BAT if the previous one wasn't active */
414                 dvbpsi_bat_t *p_bat = (dvbpsi_bat_t*)malloc(sizeof(dvbpsi_bat_t));
415                 if (p_bat)
416                 {
417                     p_bat_decoder->current_bat.b_current_next = true;
418                     memcpy(p_bat, &p_bat_decoder->current_bat, sizeof(dvbpsi_bat_t));
419                     p_bat_decoder->pf_bat_callback(p_bat_decoder->p_cb_data, p_bat);
420                 }
421                 else
422                     dvbpsi_error(p_dvbpsi, "BAT decoder", "Could not signal new BAT.");
423             }
424             dvbpsi_DeletePSISections(p_section);
425             return;
426 #endif
427         }
428     }
429 
430     /* Add section to BAT */
431     if (!dvbpsi_AddSectionBAT(p_dvbpsi, p_bat_decoder, p_section))
432     {
433         dvbpsi_error(p_dvbpsi, "BAT decoder", "failed decoding section %d",
434                      p_section->i_number);
435         dvbpsi_DeletePSISections(p_section);
436         return;
437     }
438 
439     /* Check if we have all the sections */
440     if (dvbpsi_decoder_psi_sections_completed(DVBPSI_DECODER(p_bat_decoder)))
441     {
442         assert(p_bat_decoder->pf_bat_callback);
443 
444         /* Save the current information */
445         p_bat_decoder->current_bat = *p_bat_decoder->p_building_bat;
446         p_bat_decoder->b_current_valid = true;
447         /* Decode the sections */
448         dvbpsi_bat_sections_decode(p_bat_decoder->p_building_bat,
449                                    p_bat_decoder->p_sections);
450         /* signal the new BAT */
451         p_bat_decoder->pf_bat_callback(p_bat_decoder->p_cb_data,
452                                        p_bat_decoder->p_building_bat);
453         /* Delete sections and Reinitialize the structures */
454         dvbpsi_ReInitBAT(p_bat_decoder, false);
455         assert(p_bat_decoder->p_sections == NULL);
456     }
457 }
458 
459 /*****************************************************************************
460  * dvbpsi_DecodeBATSection
461  *****************************************************************************
462  * BAT decoder.
463  * p_bat as the output parameter
464  * p_section as the input parameter
465  * similar to dvbpsi_DecodeNITSection
466  *****************************************************************************/
dvbpsi_bat_sections_decode(dvbpsi_bat_t * p_bat,dvbpsi_psi_section_t * p_section)467 void dvbpsi_bat_sections_decode(dvbpsi_bat_t* p_bat,
468                               dvbpsi_psi_section_t* p_section)
469 {
470     uint8_t* p_byte, * p_end;
471 
472     while(p_section)
473     {
474         /* - first loop descriptors */
475         p_byte = p_section->p_payload_start + 2;
476         p_end = p_byte + (((uint16_t)(p_section->p_payload_start[0] & 0x0f) << 8)
477                           | p_section->p_payload_start[1]);
478         if (p_end > p_section->p_payload_end)
479             p_end = p_section->p_payload_end;
480 
481         while(p_byte + 2 <= p_end)
482         {
483             uint8_t i_tag = p_byte[0];
484             uint8_t i_length = p_byte[1];
485             if (i_length + 2 <= p_end - p_byte)
486                 dvbpsi_bat_bouquet_descriptor_add(p_bat, i_tag, i_length, p_byte + 2);
487             p_byte += 2 + i_length;
488         }
489 
490         p_end = 2 + p_byte + (((uint16_t)(p_byte[0] & 0x0f) << 8) | p_byte[1]);
491         if (p_end > p_section->p_payload_end)
492             p_end = p_section->p_payload_end;
493 
494         /* - TSs */
495         p_byte += 2;
496         while(p_byte + 6 <= p_end)
497         {
498             uint8_t *p_end2;
499             uint16_t i_ts_id = ((uint16_t)p_byte[0] << 8) | p_byte[1];
500             uint16_t i_orig_network_id = ((uint16_t)p_byte[2] << 8) | p_byte[3];
501             uint16_t i_transport_descriptors_length = ((uint16_t)(p_byte[4] & 0x0f) << 8) | p_byte[5];
502 
503             dvbpsi_bat_ts_t* p_ts = dvbpsi_bat_ts_add(p_bat, i_ts_id, i_orig_network_id);
504             if (!p_ts)
505                 break;
506 
507             /* - TS descriptors */
508             p_byte += 6;
509             p_end2 = p_byte + i_transport_descriptors_length;
510             if (p_end2 > p_section->p_payload_end)
511                 p_end2 = p_section->p_payload_end;
512 
513             while (p_byte + 2 <= p_end2)
514             {
515                 uint8_t i_tag = p_byte[0];
516                 uint8_t i_length = p_byte[1];
517                 if (i_length + 2 <= p_end2 - p_byte)
518                     dvbpsi_bat_ts_descriptor_add(p_ts, i_tag, i_length, p_byte + 2);
519                 p_byte += 2 + i_length;
520             }
521         }
522 
523         p_section = p_section->p_next;
524     }
525 }
526 
527 /*****************************************************************************
528  * dvbpsi_bat_sections_generate
529  *****************************************************************************
530  * Generate BAT sections based on the dvbpsi_bat_t structure.
531  * similar to dvbpsi_nit_sections_generate
532  *****************************************************************************/
dvbpsi_bat_sections_generate(dvbpsi_t * p_dvbpsi,dvbpsi_bat_t * p_bat)533 dvbpsi_psi_section_t* dvbpsi_bat_sections_generate(dvbpsi_t *p_dvbpsi, dvbpsi_bat_t* p_bat)
534 {
535     dvbpsi_psi_section_t* p_result = dvbpsi_NewPSISection(1024);
536     dvbpsi_psi_section_t* p_current = p_result;
537     dvbpsi_psi_section_t* p_prev;
538     dvbpsi_descriptor_t* p_descriptor = p_bat->p_first_descriptor;
539     dvbpsi_bat_ts_t* p_ts = p_bat->p_first_ts;
540     uint16_t i_bouquet_descriptors_length, i_transport_stream_loop_length;
541     uint8_t * p_transport_stream_loop_length;
542 
543     if (p_current == NULL)
544     {
545         dvbpsi_error(p_dvbpsi, "BAT encoder", "failed to allocate new PSI section");
546         return NULL;
547     }
548 
549     p_current->i_table_id = 0x4a;
550     p_current->b_syntax_indicator = true;
551     p_current->b_private_indicator = true;
552     p_current->i_length = 13;                     /* including CRC_32 */
553     p_current->i_extension = p_bat->i_extension;
554     p_current->i_version = p_bat->i_version;
555     p_current->b_current_next = p_bat->b_current_next;
556     p_current->i_number = 0;
557     p_current->p_payload_end += 10;
558     p_current->p_payload_start = p_current->p_data + 8;
559 
560     /* first loop descriptors */
561     while (p_descriptor != NULL)
562     {
563         /* New section if needed */
564         /* written_data_length + descriptor_length + 2 > 1024 - CRC_32_length */
565         if(   (p_current->p_payload_end - p_current->p_data)
566                                 + p_descriptor->i_length > 1018)
567         {
568             /* bouquet_descriptors_length */
569             i_bouquet_descriptors_length = (p_current->p_payload_end - p_current->p_payload_start) - 2;
570             p_current->p_data[8] = (i_bouquet_descriptors_length >> 8) | 0xf0;
571             p_current->p_data[9] = i_bouquet_descriptors_length;
572 
573             /* transport_stream_loop_length */
574             p_current->p_payload_end[0] = 0;
575             p_current->p_payload_end[1] = 0;
576             p_current->p_payload_end += 2;
577 
578             p_prev = p_current;
579             p_current = dvbpsi_NewPSISection(1024);
580             if (p_current ==  NULL)
581             {
582                 dvbpsi_error(p_dvbpsi, "BAT encoder", "failed to allocate new PSI section");
583                 goto error;
584             }
585             p_prev->p_next = p_current;
586 
587             p_current->i_table_id = 0x4a;
588             p_current->b_syntax_indicator = true;
589             p_current->b_private_indicator = true;
590             p_current->i_length = 13;                 /* including CRC_32 */
591             p_current->i_extension = p_bat->i_extension;
592             p_current->i_version = p_bat->i_version;
593             p_current->b_current_next = p_bat->b_current_next;
594             p_current->i_number = p_prev->i_number + 1;
595             p_current->p_payload_end += 10;
596             p_current->p_payload_start = p_current->p_data + 8;
597         }
598 
599         /* p_payload_end is where the descriptor begins */
600         p_current->p_payload_end[0] = p_descriptor->i_tag;
601         p_current->p_payload_end[1] = p_descriptor->i_length;
602         memcpy(p_current->p_payload_end + 2,
603                p_descriptor->p_data,
604                p_descriptor->i_length);
605 
606         /* Increase length by descriptor_length + 2 */
607         p_current->p_payload_end += p_descriptor->i_length + 2;
608         p_current->i_length += p_descriptor->i_length + 2;
609 
610         p_descriptor = p_descriptor->p_next;
611     }
612 
613     /* bouquet_descriptors_length */
614     i_bouquet_descriptors_length = (p_current->p_payload_end - p_current->p_payload_start) - 2;
615     p_current->p_data[8] = (i_bouquet_descriptors_length >> 8) | 0xf0;
616     p_current->p_data[9] = i_bouquet_descriptors_length;
617 
618     /* Store the position of the transport_stream_loop_length field
619        and reserve two bytes for it */
620     p_transport_stream_loop_length = p_current->p_payload_end;
621     p_current->p_payload_end += 2;
622 
623     /* second loop: BAT TSs */
624     while (p_ts != NULL)
625     {
626         uint8_t* p_ts_start = p_current->p_payload_end;
627         uint16_t i_transport_descriptors_length = 5;
628 
629         /* Can the current section carry all the descriptors ? */
630         p_descriptor = p_ts->p_first_descriptor;
631         while(    (p_descriptor != NULL)
632                && ((p_ts_start - p_current->p_data) + i_transport_descriptors_length <= 1020))
633         {
634             i_transport_descriptors_length += p_descriptor->i_length + 2;
635             p_descriptor = p_descriptor->p_next;
636         }
637 
638         /* If _no_ and the current section isn't empty and an empty section
639            may carry one more descriptor
640            then create a new section */
641         if(    (p_descriptor != NULL)
642             && (p_ts_start - p_current->p_data != 12)
643             && (i_transport_descriptors_length <= 1008))
644         {
645             /* transport_stream_loop_length */
646             i_transport_stream_loop_length = (p_current->p_payload_end - p_transport_stream_loop_length) - 2;
647             p_transport_stream_loop_length[0] = (i_transport_stream_loop_length >> 8) | 0xf0;
648             p_transport_stream_loop_length[1] = i_transport_stream_loop_length;
649 
650             /* will put more descriptors in an empty section */
651             dvbpsi_debug(p_dvbpsi, "BAT generator",
652                         "create a new section to carry more TS descriptors");
653 
654             p_prev = p_current;
655             p_current = dvbpsi_NewPSISection(1024);
656             p_prev->p_next = p_current;
657 
658             p_current->i_table_id = 0x4a;
659             p_current->b_syntax_indicator = true;
660             p_current->b_private_indicator = true;
661             p_current->i_length = 13;                 /* including CRC_32 */
662             p_current->i_extension = p_bat->i_extension;
663             p_current->i_version = p_bat->i_version;
664             p_current->b_current_next = p_bat->b_current_next;
665             p_current->i_number = p_prev->i_number + 1;
666             p_current->p_payload_end += 10;
667             p_current->p_payload_start = p_current->p_data + 8;
668 
669             /* bouquet_descriptors_length = 0 */
670             p_current->p_data[8] = 0xf0;
671             p_current->p_data[9] = 0x00;
672 
673             /* Store the position of the transport_stream_loop_length field
674                and reserve two bytes for it */
675             p_transport_stream_loop_length = p_current->p_payload_end;
676             p_current->p_payload_end += 2;
677 
678             p_ts_start = p_current->p_payload_end;
679         }
680 
681         /* p_ts_start is where the TS begins */
682         p_ts_start[0] = p_ts->i_ts_id >> 8;
683         p_ts_start[1] = p_ts->i_ts_id & 0xff;
684         p_ts_start[2] = p_ts->i_orig_network_id >> 8;
685         p_ts_start[3] = p_ts->i_orig_network_id & 0xff;
686 
687         /* Increase the length by 6 */
688         p_current->p_payload_end += 6;
689         p_current->i_length += 6;
690 
691         /* TS descriptors */
692         p_descriptor = p_ts->p_first_descriptor;
693         while(    (p_descriptor != NULL)
694                && (   (p_current->p_payload_end - p_current->p_data)
695                     + p_descriptor->i_length <= 1018))
696         {
697             /* p_payload_end is where the descriptor begins */
698             p_current->p_payload_end[0] = p_descriptor->i_tag;
699             p_current->p_payload_end[1] = p_descriptor->i_length;
700             memcpy(p_current->p_payload_end + 2,
701                    p_descriptor->p_data,
702                    p_descriptor->i_length);
703 
704             /* Increase length by descriptor_length + 2 */
705             p_current->p_payload_end += p_descriptor->i_length + 2;
706             p_current->i_length += p_descriptor->i_length + 2;
707 
708             p_descriptor = p_descriptor->p_next;
709         }
710 
711         if (p_descriptor != NULL)
712             dvbpsi_error(p_dvbpsi, "BAT generator", "unable to carry all the TS descriptors");
713 
714         /* transport_descriptors_length */
715         i_transport_descriptors_length = p_current->p_payload_end - p_ts_start - 5;
716         p_ts_start[4] = (i_transport_descriptors_length >> 8) | 0xf0;
717         p_ts_start[5] = i_transport_descriptors_length;
718 
719         p_ts = p_ts->p_next;
720     }
721 
722     /* transport_stream_loop_length */
723     i_transport_stream_loop_length = (p_current->p_payload_end - p_transport_stream_loop_length) - 2;
724     p_transport_stream_loop_length[0] = (i_transport_stream_loop_length >> 8) | 0xf0;
725     p_transport_stream_loop_length[1] = i_transport_stream_loop_length;
726 
727     /* Finalization */
728     p_prev = p_result;
729     while (p_prev != NULL)
730     {
731         p_prev->i_last_number = p_current->i_number;
732         dvbpsi_BuildPSISection(p_dvbpsi, p_prev);
733         p_prev = p_prev->p_next;
734     }
735     return p_result;
736 
737 error:
738     /* Cleanup on error */
739     p_prev = p_result;
740     dvbpsi_DeletePSISections(p_prev);
741     return NULL;
742 }
743