libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pat_desc.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_init(struct ts_pat *pat, uint16_t transport_stream_id) {
  23. pat->ts_header.pid = 0x00;
  24. pat->ts_header.pusi = 1;
  25. pat->ts_header.payload_field = 1;
  26. pat->ts_header.payload_offset = 4;
  27. pat->section_header->table_id = 0x00;
  28. pat->section_header->version_number = 1;
  29. pat->section_header->current_next_indicator = 1;
  30. pat->section_header->section_syntax_indicator = 1;
  31. pat->section_header->private_indicator = 0;
  32. pat->section_header->section_length = 9; // Empty section (9)
  33. pat->section_header->reserved1 = 3;
  34. pat->section_header->reserved2 = 3;
  35. pat->section_header->ts_id_number = transport_stream_id;
  36. pat->programs_num = 0;
  37. pat->initialized = 1;
  38. ts_pat_regenerate_packet_data(pat);
  39. return pat;
  40. }
  41. struct ts_pat *ts_pat_alloc_init(uint16_t transport_stream_id) {
  42. struct ts_pat *pat = ts_pat_alloc();
  43. if (!pat)
  44. return NULL;
  45. return ts_pat_init(pat, transport_stream_id);
  46. }
  47. int ts_pat_add_program(struct ts_pat *pat, uint16_t program, uint16_t pat_pid) {
  48. int i;
  49. if (pat->programs_max == pat->programs_num)
  50. return 0;
  51. for (i=0;i<pat->programs_num;i++) {
  52. struct ts_pat_program *prg = pat->programs[i];
  53. if (program == prg->program) {
  54. ts_LOGf("!!! Program 0x%04x (%d) already exists in PAT!\n", program, program);
  55. return 0;
  56. }
  57. }
  58. pat->section_header->version_number++;
  59. pat->section_header->section_length += 4;
  60. struct ts_pat_program *pinfo = calloc(1, sizeof(struct ts_pat_program));
  61. pinfo->program = program;
  62. pinfo->reserved = 7; // All three bits are up
  63. pinfo->pid = pat_pid;
  64. pat->programs[pat->programs_num] = pinfo;
  65. pat->programs_num++;
  66. ts_pat_regenerate_packet_data(pat);
  67. return 1;
  68. }
  69. int ts_pat_del_program(struct ts_pat *pat, uint16_t program) {
  70. int i, ok=1, del_pos=0;
  71. if (!pat->programs_num)
  72. return 0;
  73. for (i=0;i<pat->programs_num;i++) {
  74. struct ts_pat_program *prg = pat->programs[i];
  75. if (program == prg->program) {
  76. ts_LOGf("!!! Found program 0x%04x (%d) for deleting.\n", program, program);
  77. del_pos = i;
  78. ok = 1;
  79. break;
  80. }
  81. }
  82. if (!ok)
  83. return 0;
  84. for (i=0;i<pat->programs_num;i++) {
  85. if (i < del_pos)
  86. continue;
  87. if (i == del_pos) {
  88. FREE(pat->programs[i]);
  89. }
  90. if (i >= del_pos && i+1 < pat->programs_num) {
  91. struct ts_pat_program *next = pat->programs[i+1];
  92. pat->programs[i] = next;
  93. }
  94. }
  95. pat->section_header->version_number++;
  96. pat->section_header->section_length -= 4;
  97. pat->programs_num--;
  98. ts_pat_regenerate_packet_data(pat);
  99. return 1;
  100. }