bat.c 24.9 KB
Newer Older
Zlu Zu's avatar
Zlu Zu committed
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
/*****************************************************************************
 * bat.c: BAT decoder/generator
 *----------------------------------------------------------------------------
 * Copyright (C) 2001-2010 VideoLAN
 * $Id: bat.c 110 2010-04-01 12:52:02Z gbazin $
 *
 * Authors: Zhu zhenglu <zhuzlu@gmail.com>
 *          heavily based on nit.c which was written by
 *          Johann Hanne
 *
 * 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
 *
 *----------------------------------------------------------------------------
 *
 *****************************************************************************/

#include "config.h"

#include <stdio.h>
#include <stdlib.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 "../psi.h"
#include "../descriptor.h"
#include "../demux.h"
#include "bat.h"
#include "bat_private.h"


/*****************************************************************************
 * dvbpsi_AttachBAT
 *****************************************************************************
 * Initialize a BAT subtable decoder.
 *****************************************************************************/
int dvbpsi_AttachBAT(dvbpsi_decoder_t * p_psi_decoder, uint8_t i_table_id,
          uint16_t i_extension, dvbpsi_bat_callback pf_callback, void* p_cb_data)
{
  dvbpsi_demux_t* p_demux = (dvbpsi_demux_t*)p_psi_decoder->p_private_decoder;
  dvbpsi_demux_subdec_t* p_subdec;
  dvbpsi_bat_decoder_t*  p_bat_decoder;
  unsigned int i;

  if(dvbpsi_demuxGetSubDec(p_demux, i_table_id, i_extension))
  {
    DVBPSI_ERROR_ARG("BAT decoder",
                     "Already a decoder for (table_id == 0x%02x,"
                     "extension == 0x%02x)",
                     i_table_id, i_extension);

    return 1;
  }

  p_subdec = (dvbpsi_demux_subdec_t*)malloc(sizeof(dvbpsi_demux_subdec_t));
  if(p_subdec == NULL)
  {
    return 1;
  }

  p_bat_decoder = (dvbpsi_bat_decoder_t*)malloc(sizeof(dvbpsi_bat_decoder_t));
  if(p_bat_decoder == NULL)
  {
    free(p_subdec);
    return 1;
  }

  /* subtable decoder configuration */
  p_subdec->pf_callback = &dvbpsi_GatherBATSections;
  p_subdec->p_cb_data = p_bat_decoder;
  p_subdec->i_id = (uint32_t)i_table_id << 16 | (uint32_t)i_extension;
  p_subdec->pf_detach = dvbpsi_DetachBAT;

  /* Attach the subtable decoder to the demux */
  p_subdec->p_next = p_demux->p_first_subdec;
  p_demux->p_first_subdec = p_subdec;

  /* BAT decoder information */
  p_bat_decoder->pf_callback = pf_callback;
  p_bat_decoder->p_cb_data = p_cb_data;
  /* BAT decoder initial state */
  p_bat_decoder->b_current_valid = 0;
  p_bat_decoder->p_building_bat = NULL;

  for(i = 0; i < 256; i++)
    p_bat_decoder->ap_sections[i] = NULL;

  return 0;
}

/*****************************************************************************
 * dvbpsi_DetachBAT
 *****************************************************************************
 * Close a BAT decoder.
 *****************************************************************************/
void dvbpsi_DetachBAT(dvbpsi_demux_t * p_demux, uint8_t i_table_id,
          uint16_t i_extension)
{
  dvbpsi_demux_subdec_t* p_subdec;
  dvbpsi_demux_subdec_t** pp_prev_subdec;
  dvbpsi_bat_decoder_t* p_bat_decoder;
  unsigned int i;

  p_subdec = dvbpsi_demuxGetSubDec(p_demux, i_table_id, i_extension);
  if(p_subdec == NULL)
  {
    DVBPSI_ERROR_ARG("BAT Decoder",
                     "No such BAT decoder (table_id == 0x%02x,"
                     "extension == 0x%02x)",
                     i_table_id, i_extension);
    return;
  }

  p_bat_decoder = (dvbpsi_bat_decoder_t*)p_subdec->p_cb_data;
  free(p_bat_decoder->p_building_bat);

  for(i = 0; i < 256; i++)
  {
    if(p_bat_decoder->ap_sections[i])
      dvbpsi_DeletePSISections(p_bat_decoder->ap_sections[i]);
  }

  free(p_subdec->p_cb_data);

  pp_prev_subdec = &p_demux->p_first_subdec;
  while(*pp_prev_subdec != p_subdec)
    pp_prev_subdec = &(*pp_prev_subdec)->p_next;

  *pp_prev_subdec = p_subdec->p_next;
  free(p_subdec);
}

/*****************************************************************************
 * dvbpsi_InitBAT
 *****************************************************************************
 * Initialize a pre-allocated dvbpsi_bat_t structure.
 *****************************************************************************/
void dvbpsi_InitBAT(dvbpsi_bat_t* p_bat, uint16_t i_bouquet_id, uint8_t i_version,
                    int b_current_next)
{
  p_bat->i_bouquet_id = i_bouquet_id;
  p_bat->i_version = i_version;
  p_bat->b_current_next = b_current_next;
  p_bat->p_first_ts = NULL;
  p_bat->p_first_descriptor = NULL;
}

/*****************************************************************************
 * dvbpsi_EmptyBAT
 *****************************************************************************
 * Clean a dvbpsi_bat_t structure.
 *****************************************************************************/
void dvbpsi_EmptyBAT(dvbpsi_bat_t* p_bat)
{
  dvbpsi_bat_ts_t* p_ts = p_bat->p_first_ts;

  dvbpsi_DeleteDescriptors(p_bat->p_first_descriptor);
  p_bat->p_first_descriptor = NULL;

  while(p_ts != NULL)
  {
    dvbpsi_bat_ts_t* p_tmp = p_ts->p_next;
    dvbpsi_DeleteDescriptors(p_ts->p_first_descriptor);
    free(p_ts);
    p_ts = p_tmp;
  }

  p_bat->p_first_ts = NULL;
}

/*****************************************************************************
 * dvbpsi_BATAddTS
 *****************************************************************************
 * Add a TS description at the end of the BAT.
 *****************************************************************************/
dvbpsi_bat_ts_t *dvbpsi_BATAddTS(dvbpsi_bat_t* p_bat,
                                 uint16_t i_ts_id, uint16_t i_orig_network_id)
{
  dvbpsi_bat_ts_t * p_ts
                = (dvbpsi_bat_ts_t*)malloc(sizeof(dvbpsi_bat_ts_t));
  if(p_ts)
  {
    p_ts->i_ts_id = i_ts_id;
    p_ts->i_orig_network_id = i_orig_network_id;
    p_ts->p_next = NULL;
    p_ts->p_first_descriptor = NULL;

    if(p_bat->p_first_ts == NULL)
    {
      p_bat->p_first_ts = p_ts;
    }
    else
    {
      dvbpsi_bat_ts_t * p_last_ts = p_bat->p_first_ts;
      while(p_last_ts->p_next != NULL)
        p_last_ts = p_last_ts->p_next;
      p_last_ts->p_next = p_ts;
    }
  }

  return p_ts;
}

/*****************************************************************************
 * dvbpsi_BATBouquetAddDescriptor
 *****************************************************************************
 * Add a descriptor in the BAT Bouquet descriptors (the first loop description),
 *  which is in the first loop of BAT.
 *****************************************************************************/
dvbpsi_descriptor_t *dvbpsi_BATBouquetAddDescriptor(
                                               dvbpsi_bat_t *p_bat,
                                               uint8_t i_tag, uint8_t i_length,
                                               uint8_t *p_data)
{
  dvbpsi_descriptor_t * p_descriptor
                        = dvbpsi_NewDescriptor(i_tag, i_length, p_data);
  if(p_descriptor)
  {
    if(p_bat->p_first_descriptor == NULL)
    {
      p_bat->p_first_descriptor = p_descriptor;
    }
    else
    {
      dvbpsi_descriptor_t * p_last_descriptor = p_bat->p_first_descriptor;
      while(p_last_descriptor->p_next != NULL)
        p_last_descriptor = p_last_descriptor->p_next;
      p_last_descriptor->p_next = p_descriptor;
    }
  }

  return p_descriptor;
}

/*****************************************************************************
 * dvbpsi_BATTSAddDescriptor
 *****************************************************************************
 * Add a descriptor in the BAT TS descriptors, which is in the second loop of BAT.
 *****************************************************************************/
dvbpsi_descriptor_t *dvbpsi_BATTSAddDescriptor(
                                               dvbpsi_bat_ts_t *p_ts,
                                               uint8_t i_tag, uint8_t i_length,
                                               uint8_t *p_data)
{
  dvbpsi_descriptor_t * p_descriptor
                        = dvbpsi_NewDescriptor(i_tag, i_length, p_data);
  if(p_descriptor)
  {
    if(p_ts->p_first_descriptor == NULL)
    {
      p_ts->p_first_descriptor = p_descriptor;
    }
    else
    {
      dvbpsi_descriptor_t *p_last_descriptor = p_ts->p_first_descriptor;
      while(p_last_descriptor->p_next != NULL)
        p_last_descriptor = p_last_descriptor->p_next;
      p_last_descriptor->p_next = p_descriptor;
    }
  }

  return p_descriptor;
}

/*****************************************************************************
 * dvbpsi_GatherBATSections
 *****************************************************************************
 * Callback for the subtable demultiplexor.
 *****************************************************************************/
void dvbpsi_GatherBATSections(dvbpsi_decoder_t * p_psi_decoder,
                              void * p_private_decoder,
                              dvbpsi_psi_section_t * p_section)
{
  dvbpsi_bat_decoder_t * p_bat_decoder
                        = (dvbpsi_bat_decoder_t*)p_private_decoder;
  int b_append = 1;
  int b_reinit = 0;
  unsigned int i;

  DVBPSI_DEBUG_ARG("BAT decoder",
                   "Table version %2d, " "i_table_id %2d, " "i_extension %5d, "
                   "section %3d up to %3d, " "current %1d",
                   p_section->i_version, p_section->i_table_id,
                   p_section->i_extension,
                   p_section->i_number, p_section->i_last_number,
                   p_section->b_current_next);

  if(!p_section->b_syntax_indicator)
  {
    /* Invalid section_syntax_indicator */
    DVBPSI_ERROR("BAT decoder",
                 "invalid section (section_syntax_indicator == 0)");
    b_append = 0;
  }

  /* Now if b_append is true then we have a valid BAT section */
  if(b_append)
  {
    /* TS discontinuity check */
    if(p_psi_decoder->b_discontinuity)
    {
      b_reinit = 1;
      p_psi_decoder->b_discontinuity = 0;
    }
    else
    {
      /* Perform a few sanity checks */
      if(p_bat_decoder->p_building_bat)
      {
        if(p_bat_decoder->p_building_bat->i_bouquet_id != p_section->i_extension)
        {
          /* bouquet_id */
          DVBPSI_ERROR("BAT decoder",
                       "'bouquet_id' differs"
                       " whereas no TS discontinuity has occured");
          b_reinit = 1;
        }
        else if(p_bat_decoder->p_building_bat->i_version
                                                != p_section->i_version)
        {
          /* version_number */
          DVBPSI_ERROR("BAT decoder",
                       "'version_number' differs"
                       " whereas no discontinuity has occured");
          b_reinit = 1;
        }
        else if(p_bat_decoder->i_last_section_number !=
                                                p_section->i_last_number)
        {
          /* last_section_number */
          DVBPSI_ERROR("BAT decoder",
                       "'last_section_number' differs"
                       " whereas no discontinuity has occured");
          b_reinit = 1;
        }
      }
      else
      {
        if(    (p_bat_decoder->b_current_valid)
            && (p_bat_decoder->current_bat.i_version == p_section->i_version))
        {
          /* Signal a new BAT if the previous one wasn't active */
          if(    (!p_bat_decoder->current_bat.b_current_next)
              && (p_section->b_current_next))
          {
            dvbpsi_bat_t *p_bat = NULL;
            p_bat_decoder->current_bat.b_current_next = 1;
            *p_bat = p_bat_decoder->current_bat;
            p_bat_decoder->pf_callback(p_bat_decoder->p_cb_data, p_bat);
          }

          /* Don't decode since this version is already decoded */
          b_append = 0;
        }
      }
    }
  }

  /* Reinit the decoder if wanted */
  if(b_reinit)
  {
    /* Force redecoding */
    p_bat_decoder->b_current_valid = 0;
    /* Free structures */
    if(p_bat_decoder->p_building_bat)
    {
      free(p_bat_decoder->p_building_bat);
      p_bat_decoder->p_building_bat = NULL;
    }
    /* Clear the section array */
    for(i = 0; i < 256; i++)
    {
      if(p_bat_decoder->ap_sections[i] != NULL)
      {
        dvbpsi_DeletePSISections(p_bat_decoder->ap_sections[i]);
        p_bat_decoder->ap_sections[i] = NULL;
      }
    }
  }

  /* Append the section to the list if wanted */
  if(b_append)
  {
    int b_complete;

    /* Initialize the structures if it's the first section received */
    if(!p_bat_decoder->p_building_bat)
    {
      p_bat_decoder->p_building_bat =
                                (dvbpsi_bat_t*)malloc(sizeof(dvbpsi_bat_t));
      dvbpsi_InitBAT(p_bat_decoder->p_building_bat,
                     p_section->i_extension,
                     p_section->i_version,
                     p_section->b_current_next);
      p_bat_decoder->i_last_section_number = p_section->i_last_number;
    }

    /* Fill the section array */
    if(p_bat_decoder->ap_sections[p_section->i_number] != NULL)
    {
      DVBPSI_DEBUG_ARG("BAT decoder", "overwrite section number %d",
                       p_section->i_number);
      dvbpsi_DeletePSISections(p_bat_decoder->ap_sections[p_section->i_number]);
    }
    p_bat_decoder->ap_sections[p_section->i_number] = p_section;

    /* Check if we have all the sections */
    b_complete = 0;
    for(i = 0; i <= p_bat_decoder->i_last_section_number; i++)
    {
      if(!p_bat_decoder->ap_sections[i])
        break;

      if(i == p_bat_decoder->i_last_section_number)
        b_complete = 1;
    }

    if(b_complete)
    {
      /* Save the current information */
      p_bat_decoder->current_bat = *p_bat_decoder->p_building_bat;
      p_bat_decoder->b_current_valid = 1;
      /* Chain the sections */
      if(p_bat_decoder->i_last_section_number)
      {
        int j;
        for(j = 0; j <= p_bat_decoder->i_last_section_number - 1; j++)
          p_bat_decoder->ap_sections[j]->p_next =
                                        p_bat_decoder->ap_sections[j + 1];
      }
      /* Decode the sections */
      dvbpsi_DecodeBATSections(p_bat_decoder->p_building_bat,
                               p_bat_decoder->ap_sections[0]);
      /* Delete the sections */
      dvbpsi_DeletePSISections(p_bat_decoder->ap_sections[0]);
      /* signal the new BAT */
      p_bat_decoder->pf_callback(p_bat_decoder->p_cb_data,
                                 p_bat_decoder->p_building_bat);
      /* Reinitialize the structures */
      p_bat_decoder->p_building_bat = NULL;
      for(i = 0; i <= p_bat_decoder->i_last_section_number; i++)
        p_bat_decoder->ap_sections[i] = NULL;
    }
  }
  else
  {
    dvbpsi_DeletePSISections(p_section);
  }
}

/*****************************************************************************
 * dvbpsi_DecodeBATSection
 *****************************************************************************
 * BAT decoder.
 * p_bat as the output parameter
 * p_section as the input parameter
 * similar to dvbpsi_DecodeNITSection
 *****************************************************************************/
void dvbpsi_DecodeBATSections(dvbpsi_bat_t* p_bat,
                              dvbpsi_psi_section_t* p_section)
{
  uint8_t* p_byte, * p_end, * p_end2;

  while(p_section)
  {
    /* - first loop descriptors */
    p_byte = p_section->p_payload_start + 2;
    p_end = p_byte + ( ((uint16_t)(p_section->p_payload_start[0] & 0x0f) << 8)
                       | p_section->p_payload_start[1]);

    while(p_byte + 2 <= p_end)
    {
      uint8_t i_tag = p_byte[0];
      uint8_t i_length = p_byte[1];
      if(i_length + 2 <= p_end - p_byte)
        dvbpsi_BATBouquetAddDescriptor(p_bat, i_tag, i_length, p_byte + 2);
      p_byte += 2 + i_length;
    }

    p_end = p_byte + ( ((uint16_t)(p_byte[0] & 0x0f) << 8)
                       | p_byte[1]);
    if(p_end > p_section->p_payload_end)
    {
        p_end = p_section->p_payload_end;
    }
    p_byte += 2;

    /* - TSs */
    for(; p_byte + 6 <= p_end;)
    {
      uint16_t i_ts_id = ((uint16_t)p_byte[0] << 8) | p_byte[1];
      uint16_t i_orig_network_id = ((uint16_t)p_byte[2] << 8) | p_byte[3];
      uint16_t i_transport_descriptors_length = ((uint16_t)(p_byte[4] & 0x0f) << 8) | p_byte[5];
      dvbpsi_bat_ts_t* p_ts = dvbpsi_BATAddTS(p_bat, i_ts_id, i_orig_network_id);
      /* - TS descriptors */
      p_byte += 6;
      p_end2 = p_byte + i_transport_descriptors_length;
      if( p_end2 > p_section->p_payload_end )
      {
            p_end2 = p_section->p_payload_end;
      }
      while(p_byte + 2 <= p_end2)
      {
        uint8_t i_tag = p_byte[0];
        uint8_t i_length = p_byte[1];
        if(i_length + 2 <= p_end2 - p_byte)
          dvbpsi_BATTSAddDescriptor(p_ts, i_tag, i_length, p_byte + 2);
        p_byte += 2 + i_length;
      }
    }

    p_section = p_section->p_next;
  }
}

/*****************************************************************************
 * dvbpsi_GenBATSections
 *****************************************************************************
 * Generate BAT sections based on the dvbpsi_bat_t structure.
 * similar to dvbpsi_GenNITSections
 *****************************************************************************/
dvbpsi_psi_section_t* dvbpsi_GenBATSections(dvbpsi_bat_t* p_bat)
{
  dvbpsi_psi_section_t* p_result = dvbpsi_NewPSISection(1024);
  dvbpsi_psi_section_t* p_current = p_result;
  dvbpsi_psi_section_t* p_prev;
  dvbpsi_descriptor_t* p_descriptor = p_bat->p_first_descriptor;
  dvbpsi_bat_ts_t* p_ts = p_bat->p_first_ts;
  uint16_t i_bouquet_descriptors_length, i_transport_stream_loop_length;
  uint8_t * p_transport_stream_loop_length;

  p_current->i_table_id = 0x4a;
  p_current->b_syntax_indicator = 1;
  p_current->b_private_indicator = 1;
  p_current->i_length = 13;                     /* including CRC_32 */
  p_current->i_extension = p_bat->i_bouquet_id;
  p_current->i_version = p_bat->i_version;
  p_current->b_current_next = p_bat->b_current_next;
  p_current->i_number = 0;
  p_current->p_payload_end += 10;
  p_current->p_payload_start = p_current->p_data + 8;

  /* first loop descriptors */
  while(p_descriptor != NULL)
  {
    /* New section if needed */
    /* written_data_length + descriptor_length + 2 > 1024 - CRC_32_length */
    if(   (p_current->p_payload_end - p_current->p_data)
                                + p_descriptor->i_length > 1018)
    {
      /* bouquet_descriptors_length */
      i_bouquet_descriptors_length = (p_current->p_payload_end - p_current->p_payload_start) - 2;
      p_current->p_data[8] = (i_bouquet_descriptors_length >> 8) | 0xf0;
      p_current->p_data[9] = i_bouquet_descriptors_length;

      /* transport_stream_loop_length */
      p_current->p_payload_end[0] = 0;
      p_current->p_payload_end[1] = 0;
      p_current->p_payload_end += 2;

      p_prev = p_current;
      p_current = dvbpsi_NewPSISection(1024);
      p_prev->p_next = p_current;

      p_current->i_table_id = 0x4a;
      p_current->b_syntax_indicator = 1;
      p_current->b_private_indicator = 1;
      p_current->i_length = 13;                 /* including CRC_32 */
      p_current->i_extension = p_bat->i_bouquet_id;
      p_current->i_version = p_bat->i_version;
      p_current->b_current_next = p_bat->b_current_next;
      p_current->i_number = p_prev->i_number + 1;
      p_current->p_payload_end += 10;
      p_current->p_payload_start = p_current->p_data + 8;
    }

    /* p_payload_end is where the descriptor begins */
    p_current->p_payload_end[0] = p_descriptor->i_tag;
    p_current->p_payload_end[1] = p_descriptor->i_length;
    memcpy(p_current->p_payload_end + 2,
           p_descriptor->p_data,
           p_descriptor->i_length);

    /* Increase length by descriptor_length + 2 */
    p_current->p_payload_end += p_descriptor->i_length + 2;
    p_current->i_length += p_descriptor->i_length + 2;

    p_descriptor = p_descriptor->p_next;
  }

  /* bouquet_descriptors_length */
  i_bouquet_descriptors_length = (p_current->p_payload_end - p_current->p_payload_start) - 2;
  p_current->p_data[8] = (i_bouquet_descriptors_length >> 8) | 0xf0;
  p_current->p_data[9] = i_bouquet_descriptors_length;

  /* Store the position of the transport_stream_loop_length field
     and reserve two bytes for it */
  p_transport_stream_loop_length = p_current->p_payload_end;
  p_current->p_payload_end += 2;

  /* second loop: BAT TSs */
  while(p_ts != NULL)
  {
    uint8_t* p_ts_start = p_current->p_payload_end;
    uint16_t i_transport_descriptors_length = 5;

    /* Can the current section carry all the descriptors ? */
    p_descriptor = p_ts->p_first_descriptor;
    while(    (p_descriptor != NULL)
           && ((p_ts_start - p_current->p_data) + i_transport_descriptors_length <= 1020))
    {
      i_transport_descriptors_length += p_descriptor->i_length + 2;
      p_descriptor = p_descriptor->p_next;
    }

    /* If _no_ and the current section isn't empty and an empty section
       may carry one more descriptor
       then create a new section */
    if(    (p_descriptor != NULL)
        && (p_ts_start - p_current->p_data != 12)
        && (i_transport_descriptors_length <= 1008))
    {
      /* transport_stream_loop_length */
      i_transport_stream_loop_length = (p_current->p_payload_end - p_transport_stream_loop_length) - 2;
      p_transport_stream_loop_length[0] = (i_transport_stream_loop_length >> 8) | 0xf0;
      p_transport_stream_loop_length[1] = i_transport_stream_loop_length;

      /* will put more descriptors in an empty section */
      DVBPSI_DEBUG("BAT generator",
                   "create a new section to carry more TS descriptors");
      p_prev = p_current;
      p_current = dvbpsi_NewPSISection(1024);
      p_prev->p_next = p_current;

      p_current->i_table_id = 0x4a;
      p_current->b_syntax_indicator = 1;
      p_current->b_private_indicator = 1;
      p_current->i_length = 13;                 /* including CRC_32 */
      p_current->i_extension = p_bat->i_bouquet_id;
      p_current->i_version = p_bat->i_version;
      p_current->b_current_next = p_bat->b_current_next;
      p_current->i_number = p_prev->i_number + 1;
      p_current->p_payload_end += 10;
      p_current->p_payload_start = p_current->p_data + 8;

      /* bouquet_descriptors_length = 0 */
      p_current->p_data[8] = 0xf0;
      p_current->p_data[9] = 0x00;

      /* Store the position of the transport_stream_loop_length field
         and reserve two bytes for it */
      p_transport_stream_loop_length = p_current->p_payload_end;
      p_current->p_payload_end += 2;

      p_ts_start = p_current->p_payload_end;
    }

    /* p_ts_start is where the TS begins */
    p_ts_start[0] = p_ts->i_ts_id >> 8;
    p_ts_start[1] = p_ts->i_ts_id & 0xff;
    p_ts_start[2] = p_ts->i_orig_network_id >> 8;
    p_ts_start[3] = p_ts->i_orig_network_id & 0xff;

    /* Increase the length by 6 */
    p_current->p_payload_end += 6;
    p_current->i_length += 6;

    /* TS descriptors */
    p_descriptor = p_ts->p_first_descriptor;
    while(    (p_descriptor != NULL)
           && (   (p_current->p_payload_end - p_current->p_data)
                + p_descriptor->i_length <= 1018))
    {
      /* p_payload_end is where the descriptor begins */
      p_current->p_payload_end[0] = p_descriptor->i_tag;
      p_current->p_payload_end[1] = p_descriptor->i_length;
      memcpy(p_current->p_payload_end + 2,
             p_descriptor->p_data,
             p_descriptor->i_length);

      /* Increase length by descriptor_length + 2 */
      p_current->p_payload_end += p_descriptor->i_length + 2;
      p_current->i_length += p_descriptor->i_length + 2;

      p_descriptor = p_descriptor->p_next;
    }

    if(p_descriptor != NULL)
      DVBPSI_ERROR("BAT generator", "unable to carry all the TS descriptors");

    /* transport_descriptors_length */
    i_transport_descriptors_length = p_current->p_payload_end - p_ts_start - 5;
    p_ts_start[4] = (i_transport_descriptors_length >> 8) | 0xf0;
    p_ts_start[5] = i_transport_descriptors_length;

    p_ts = p_ts->p_next;
  }

  /* transport_stream_loop_length */
  i_transport_stream_loop_length = (p_current->p_payload_end - p_transport_stream_loop_length) - 2;
  p_transport_stream_loop_length[0] = (i_transport_stream_loop_length >> 8) | 0xf0;
  p_transport_stream_loop_length[1] = i_transport_stream_loop_length;

  /* Finalization */
  p_prev = p_result;
  while(p_prev != NULL)
  {
    p_prev->i_last_number = p_current->i_number;
    dvbpsi_BuildPSISection(p_prev);
    p_prev = p_prev->p_next;
  }

  return p_result;
}