pat.c 14 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * pat.c: PAT decoder/generator
 *----------------------------------------------------------------------------
 * (c)2001-2002 VideoLAN
5
 * $Id: pat.c,v 1.3 2002/01/09 11:22:26 bozo Exp $
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
 *
 * Authors: Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *----------------------------------------------------------------------------
 *
 *****************************************************************************/


#include "config.h"

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

#include "dvbpsi.h"
#include "psi.h"
#include "dvbpsi_private.h"
#include "psi_private.h"
#include "pat.h"
#include "pat_private.h"


/*****************************************************************************
 * dvbpsi_AttachPAT
 *****************************************************************************
 * Initialize a PAT decoder and return a handle on it.
 *****************************************************************************/
dvbpsi_handle dvbpsi_AttachPAT(dvbpsi_pat_callback pf_callback,
                               void* p_cb_data)
{
  dvbpsi_handle h_dvbpsi = (dvbpsi_decoder_t*)malloc(sizeof(dvbpsi_decoder_t));
  dvbpsi_pat_decoder_t* p_pat_decoder;

  if(h_dvbpsi == NULL)
    return NULL;

  p_pat_decoder = (dvbpsi_pat_decoder_t*)malloc(sizeof(dvbpsi_pat_decoder_t));

  if(p_pat_decoder == NULL)
  {
    free(h_dvbpsi);
    return NULL;
  }

  /* PSI decoder configuration */
  h_dvbpsi->pf_callback = &dvbpsi_DecodePATSection;
  h_dvbpsi->p_private_decoder = p_pat_decoder;
  h_dvbpsi->i_section_max_size = 1024;
  /* PSI decoder initial state */
  h_dvbpsi->i_continuity_counter = 31;
  h_dvbpsi->b_discontinuity = 1;
  h_dvbpsi->p_current_section = NULL;

  /* PAT decoder information */
  p_pat_decoder->pf_callback = pf_callback;
  p_pat_decoder->p_cb_data = p_cb_data;
  /* PAT decoder initial state */
  dvbpsi_InitPAT(&p_pat_decoder->current_pat, 1, 0, 0);
  p_pat_decoder->b_building_next = 0;

  return h_dvbpsi;
}


/*****************************************************************************
 * dvbpsi_DetachPAT
 *****************************************************************************
 * Close a PAT decoder. The handle isn't valid any more.
 *****************************************************************************/
void dvbpsi_DetachPAT(dvbpsi_handle h_dvbpsi)
{
  dvbpsi_pat_decoder_t* p_pat_decoder
                        = (dvbpsi_pat_decoder_t*)h_dvbpsi->p_private_decoder;

  dvbpsi_EmptyPAT(&p_pat_decoder->current_pat);
  dvbpsi_EmptyPAT(&p_pat_decoder->next_pat);

  free(h_dvbpsi->p_private_decoder);
  free(h_dvbpsi);
}


/*****************************************************************************
 * dvbpsi_InitPAT
 *****************************************************************************
 * Initialize a pre-allocated dvbpsi_pat_t structure.
 *****************************************************************************/
void dvbpsi_InitPAT(dvbpsi_pat_t* p_pat, uint16_t i_ts_id, uint8_t i_version,
                    int b_current_next)
{
  p_pat->i_ts_id = i_ts_id;
  p_pat->i_version = i_version;
  p_pat->b_current_next = b_current_next;
  p_pat->p_first_program = NULL;
  p_pat->b_complete = 0;
}


/*****************************************************************************
 * dvbpsi_EmptyPAT
 *****************************************************************************
 * Clean a dvbpsi_pat_t structure.
 *****************************************************************************/
void dvbpsi_EmptyPAT(dvbpsi_pat_t* p_pat)
{
  dvbpsi_pat_program_t* p_program = p_pat->p_first_program;

  while(p_program != NULL)
  {
    dvbpsi_pat_program_t* p_tmp = p_program->p_next;
    free(p_program);
    p_program = p_tmp;
  }

  p_pat->p_first_program = NULL;
}


/*****************************************************************************
 * dvbpsi_PATAddProgram
 *****************************************************************************
 * Add a program at the end of the PAT.
 *****************************************************************************/
dvbpsi_pat_program_t* dvbpsi_PATAddProgram(dvbpsi_pat_t* p_pat,
                                           uint16_t i_number, uint16_t i_pid)
{
  dvbpsi_pat_program_t* p_program
                = (dvbpsi_pat_program_t*)malloc(sizeof(dvbpsi_pat_program_t));

  if(p_program)
  {
    p_program->i_number = i_number;
    p_program->i_pid = i_pid;
    p_program->p_next = NULL;

    if(p_pat->p_first_program == NULL)
    {
      p_pat->p_first_program = p_program;
    }
    else
    {
      dvbpsi_pat_program_t* p_last_program = p_pat->p_first_program;
      while(p_last_program->p_next != NULL)
        p_last_program = p_last_program->p_next;
      p_last_program->p_next = p_program;
    }
  }

  return p_program;
}


/*****************************************************************************
 * dvbpsi_DecodePATSection
 *****************************************************************************
 * Callback for the PSI decoder.
 *****************************************************************************/
void dvbpsi_DecodePATSection(dvbpsi_decoder_t* p_decoder,
                             dvbpsi_psi_section_t* p_section)
{
  dvbpsi_pat_decoder_t* p_pat_decoder
                        = (dvbpsi_pat_decoder_t*)p_decoder->p_private_decoder;
  int b_decode = 1;
  int b_reinit = 0;
  uint8_t* p_byte;

  DVBPSI_DEBUG_ARG("PAT decoder",
186 187
                   "Table version %2d, " "i_extension %5d, "
                   "section %3d up to %3d, " "current %1d",
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
                   p_section->i_version, p_section->i_extension,
                   p_section->i_number, p_section->i_last_number,
                   p_section->b_current_next);

  if(p_section->i_table_id != 0x00)
  {
    /* Invalid table_id value */
    DVBPSI_ERROR_ARG("PAT decoder",
                     "invalid section (table_id == 0x%02x)",
                     p_section->i_table_id);
    b_decode = 0;
    b_reinit = 1;
  }
  else if(!p_section->b_syntax_indicator)
  {
    /* Invalid section_syntax_indicator */
    DVBPSI_ERROR("PAT decoder",
                 "invalid section (section_syntax_indicator == 0)");
    b_decode = 0;
    b_reinit = 1;
  }
  else if(p_decoder->b_discontinuity)
  {
    /* A TS discontinuity has occured */
    b_reinit = 1;
    p_decoder->b_discontinuity = 0;
  }
  else
  {
    /* No TS discontinuity has occured */

    /* Perform some few sanity checks */
    if(p_pat_decoder->b_building_next)
    {
      if(p_pat_decoder->next_pat.i_ts_id != p_section->i_extension)
      {
        /* transport_stream_id */
        DVBPSI_ERROR("PAT decoder",
                     "'transport_stream_id' differs"
                     " whereas no TS discontinuity has occured");
        b_reinit = 1;
      }
      else if(p_pat_decoder->next_pat.i_version != p_section->i_version)
      {
        /* version_number */
        DVBPSI_ERROR("PAT decoder",
                     "'version_number' differs"
                     " whereas no discontinuity has occured");
        b_reinit = 1;
      }
      else if(p_pat_decoder->i_last_section_number != p_section->i_last_number)
      {
        /* last_section_number */
        DVBPSI_ERROR("PAT decoder",
                     "'last_section_number' differs"
                     " whereas no discontinuity has occured");
        b_reinit = 1;
      }
      else if(p_pat_decoder->i_previous_section_number >= p_section->i_number)
      {
        /* section_number */
        DVBPSI_ERROR("PAT decoder",
                     "backward 'section_number'"
                     " whereas no discontinuity has occured");
        b_reinit = 1;
      }
    }
    else
    {
      if(p_pat_decoder->current_pat.i_version == p_section->i_version)
      {
        /* signal a new PAT if the previous one wasn't active */
        if(    (p_pat_decoder->current_pat.b_complete)
            && (!p_pat_decoder->current_pat.b_current_next)
            && (p_section->b_current_next))
        {
          dvbpsi_pat_t* p_pat = (dvbpsi_pat_t*)malloc(sizeof(dvbpsi_pat_t));

          p_pat_decoder->current_pat.b_current_next = 1;
          /* Don't need the programs because the PAT is active */
          *p_pat = p_pat_decoder->current_pat;
          p_pat_decoder->current_pat.p_first_program = NULL;
          p_pat_decoder->pf_callback(p_pat_decoder->p_cb_data, p_pat);
        }

        /* Don't decode if the version is already decoded */
        if(p_pat_decoder->current_pat.b_complete)
          b_decode = 0;
      }
    }
  }

  /* Reinit the decoder if wanted */
  if(b_reinit)
  {
    dvbpsi_EmptyPAT(&p_pat_decoder->current_pat);
    p_pat_decoder->current_pat.b_complete = 0; /* force redecoding */
    dvbpsi_EmptyPAT(&p_pat_decoder->next_pat);
    p_pat_decoder->b_building_next = 0;
  }

  /* Decode the section if wanted */
  if(b_decode)
  {
    if(!p_pat_decoder->b_building_next)
    {
      p_pat_decoder->next_pat.i_ts_id = p_section->i_extension;
      p_pat_decoder->next_pat.i_version = p_section->i_version;
      p_pat_decoder->next_pat.b_current_next = p_section->b_current_next;
      p_pat_decoder->i_last_section_number = p_section->i_last_number;
      p_pat_decoder->b_section_lost = (p_section->i_number != 0);
      p_pat_decoder->b_building_next = 1;
    }
    else
    {
      p_pat_decoder->b_section_lost =
                        (    p_section->i_number
                          != p_pat_decoder->i_previous_section_number + 1);
    }

    /* Decode section */
    p_pat_decoder->i_previous_section_number = p_section->i_number;
    for(p_byte = p_section->p_payload_start;
        p_byte < p_section->p_payload_end;
        p_byte += 4)
    {
      uint16_t i_program_number = ((uint16_t)(p_byte[0]) << 8) | p_byte[1];
      uint16_t i_pid = ((uint16_t)(p_byte[2] & 0x1f) << 8) | p_byte[3];
      dvbpsi_PATAddProgram(&p_pat_decoder->next_pat, i_program_number, i_pid);
    }

    if(p_section->i_number == p_pat_decoder->i_last_section_number)
    {
      /* PAT is finished */
      dvbpsi_pat_t* p_pat = (dvbpsi_pat_t*)malloc(sizeof(dvbpsi_pat_t));

      p_pat_decoder->next_pat.b_complete = !p_pat_decoder->b_section_lost;

      *p_pat = p_pat_decoder->next_pat;

      dvbpsi_EmptyPAT(&p_pat_decoder->current_pat);
      p_pat_decoder->current_pat.i_ts_id = p_pat->i_ts_id;
      p_pat_decoder->current_pat.i_version = p_pat->i_version;
      p_pat_decoder->current_pat.b_current_next = p_pat->b_current_next;
      p_pat_decoder->current_pat.b_complete = p_pat->b_complete;
      /* signal the new PAT */
      p_pat_decoder->pf_callback(p_pat_decoder->p_cb_data, p_pat);
      p_pat_decoder->next_pat.p_first_program = NULL;
      p_pat_decoder->b_building_next = 0;
    }
  }

  dvbpsi_DeletePSISection(p_section);
}


/*****************************************************************************
 * dvbpsi_GenPATSections
 *****************************************************************************
 * Generate PAT sections based on the dvbpsi_pat_t structure. The third
 * argument is used to limit the number of program in each section (max: 253).
 *****************************************************************************/
dvbpsi_psi_section_t* dvbpsi_GenPATSections(dvbpsi_pat_t* p_pat,
                                            int i_max_pps)
{
  dvbpsi_psi_section_t* p_result = dvbpsi_NewPSISection(1024);
  dvbpsi_psi_section_t* p_current = p_result;
355
  dvbpsi_psi_section_t* p_prev;
356 357 358
  dvbpsi_pat_program_t* p_program = p_pat->p_first_program;
  int i_count = 0;

359
  /* A PAT section can carry up to 253 programs */
360 361 362 363 364 365
  if((i_max_pps <= 0) || (i_max_pps > 253))
    i_max_pps = 253;

  p_current->i_table_id = 0;
  p_current->b_syntax_indicator = 1;
  p_current->b_private_indicator = 0;
366
  p_current->i_length = 9;                      /* header + CRC_32 */
367 368 369 370
  p_current->i_extension = p_pat->i_ts_id;
  p_current->i_version = p_pat->i_version;
  p_current->b_current_next = p_pat->b_current_next;
  p_current->i_number = 0;
371
  p_current->p_payload_end += 8;                /* just after the header */
372 373
  p_current->p_payload_start = p_current->p_payload_end;

374
  /* PAT programs */
375 376
  while(p_program != NULL)
  {
377
    /* New section if needed */
378 379 380 381 382 383 384 385 386 387
    if(++i_count > i_max_pps)
    {
      p_prev = p_current;
      p_current = dvbpsi_NewPSISection(1024);
      p_prev->p_next = p_current;
      i_count = 1;

      p_current->i_table_id = 0;
      p_current->b_syntax_indicator = 1;
      p_current->b_private_indicator = 0;
388
      p_current->i_length = 9;                  /* header + CRC_32 */
389 390 391 392
      p_current->i_extension = p_pat->i_ts_id;
      p_current->i_version = p_pat->i_version;
      p_current->b_current_next = p_pat->b_current_next;
      p_current->i_number = p_prev->i_number + 1;
393
      p_current->p_payload_end += 8;            /* just after the header */
394 395 396
      p_current->p_payload_start = p_current->p_payload_end;
    }

397 398 399 400 401 402 403
    /* p_payload_end is where the program begins */
    p_current->p_payload_end[0] = p_program->i_number >> 8;
    p_current->p_payload_end[1] = p_program->i_number;
    p_current->p_payload_end[2] = (p_program->i_pid >> 8) | 0xe0;
    p_current->p_payload_end[3] = p_program->i_pid;

    /* Increase length by 4 */
404 405 406 407 408 409
    p_current->p_payload_end += 4;
    p_current->i_length += 4;

    p_program = p_program->p_next;
  }

410
  /* Finalization */
411 412 413 414 415 416 417 418 419 420 421
  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;
}