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

pat_desc.c 2.8KB

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