libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/

pat_desc.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * PAT descriptor generator
  3. * Copyright (C) 2010-2011 Unix Solutions Ltd.
  4. *
  5. * Released under MIT license.
  6. * See LICENSE-MIT.txt for license terms.
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <netdb.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "tsfuncs.h"
  14. static void ts_pat_regenerate_packet_data(struct ts_pat *pat) {
  15. uint8_t *ts_packets;
  16. int num_packets;
  17. ts_pat_generate(pat, &ts_packets, &num_packets);
  18. memcpy(pat->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  19. pat->section_header->num_packets = num_packets;
  20. free(ts_packets);
  21. }
  22. struct ts_pat *ts_pat_alloc_init(uint16_t transport_stream_id) {
  23. struct ts_pat *pat = ts_pat_alloc();
  24. pat->ts_header.pid = 0x00;
  25. pat->ts_header.pusi = 1;
  26. pat->ts_header.payload_field = 1;
  27. pat->ts_header.payload_offset = 4;
  28. pat->section_header->table_id = 0x00;
  29. pat->section_header->version_number = 1;
  30. pat->section_header->current_next_indicator = 1;
  31. pat->section_header->section_syntax_indicator = 1;
  32. pat->section_header->private_indicator = 0;
  33. pat->section_header->section_length = 9; // Empty section (9)
  34. pat->section_header->reserved1 = 3;
  35. pat->section_header->reserved2 = 3;
  36. pat->section_header->ts_id_number = transport_stream_id;
  37. pat->initialized = 1;
  38. ts_pat_regenerate_packet_data(pat);
  39. return pat;
  40. }
  41. int ts_pat_add_program(struct ts_pat *pat, uint16_t program, uint16_t pat_pid) {
  42. int i;
  43. if (pat->programs_max == pat->programs_num)
  44. return 0;
  45. for (i=0;i<pat->programs_num;i++) {
  46. struct ts_pat_program *prg = pat->programs[i];
  47. if (program == prg->program) {
  48. ts_LOGf("!!! Program 0x%04x (%d) already exists in PAT!\n", program, program);
  49. return 0;
  50. }
  51. }
  52. pat->section_header->version_number++;
  53. pat->section_header->section_length += 4;
  54. struct ts_pat_program *pinfo = calloc(1, sizeof(struct ts_pat_program));
  55. pinfo->program = program;
  56. pinfo->reserved = 7; // All three bits are up
  57. pinfo->pid = pat_pid;
  58. pat->programs[pat->programs_num] = pinfo;
  59. pat->programs_num++;
  60. ts_pat_regenerate_packet_data(pat);
  61. return 1;
  62. }
  63. int ts_pat_del_program(struct ts_pat *pat, uint16_t program) {
  64. int i, ok=1, del_pos=0;
  65. if (!pat->programs_num)
  66. return 0;
  67. for (i=0;i<pat->programs_num;i++) {
  68. struct ts_pat_program *prg = pat->programs[i];
  69. if (program == prg->program) {
  70. ts_LOGf("!!! Found program 0x%04x (%d) for deleting.\n", program, program);
  71. del_pos = i;
  72. ok = 1;
  73. break;
  74. }
  75. }
  76. if (!ok)
  77. return 0;
  78. for (i=0;i<pat->programs_num;i++) {
  79. if (i < del_pos)
  80. continue;
  81. if (i == del_pos) {
  82. FREE(pat->programs[i]);
  83. }
  84. if (i >= del_pos && i+1 < pat->programs_num) {
  85. struct ts_pat_program *next = pat->programs[i+1];
  86. pat->programs[i] = next;
  87. }
  88. }
  89. pat->section_header->version_number++;
  90. pat->section_header->section_length -= 4;
  91. pat->programs_num--;
  92. ts_pat_regenerate_packet_data(pat);
  93. return 1;
  94. }