transport_packet.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * section and descriptor parser
  3. *
  4. * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #ifndef _UCSI_TRANSPORT_PACKET_H
  21. #define _UCSI_TRANSPORT_PACKET_H 1
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #include <stdint.h>
  27. #include "descriptor.h"
  28. #define TRANSPORT_PACKET_LENGTH 188
  29. #define TRANSPORT_PACKET_SYNC 0x47
  30. #define TRANSPORT_MAX_PIDS 0x2000
  31. #define TRANSPORT_NULL_PID 0x1fff
  32. /**
  33. * Enumeration of adaptation field control values.
  34. */
  35. enum transport_adaptation_field_control {
  36. transport_adaptation_field_control_reserved = 0x00,
  37. transport_adaptation_field_control_payload_only = 0x01,
  38. transport_adaptation_field_control_adaptation_only = 0x02,
  39. transport_adaptation_field_control_adaptation_payload = 0x03,
  40. };
  41. /**
  42. * Enumeration of scrambling control values.
  43. */
  44. enum transport_scrambling_control {
  45. transport_scrambling_control_unscrambled = 0x00,
  46. transport_scrambling_control_user_1 = 0x01,
  47. transport_scrambling_control_user_2 = 0x02,
  48. transport_scrambling_control_user_3 = 0x03,
  49. };
  50. /**
  51. * Enumeration of adaptation flags.
  52. */
  53. enum transport_adaptation_flags {
  54. transport_adaptation_flag_discontinuity = 0x80,
  55. transport_adaptation_flag_random_access = 0x40,
  56. transport_adaptation_flag_es_priority = 0x20,
  57. transport_adaptation_flag_pcr = 0x10,
  58. transport_adaptation_flag_opcr = 0x08,
  59. transport_adaptation_flag_splicing_point = 0x04,
  60. transport_adaptation_flag_private_data = 0x02,
  61. transport_adaptation_flag_extension = 0x01,
  62. };
  63. /**
  64. * Enumeration of adaptation extension flags.
  65. */
  66. enum transport_adaptation_extension_flags {
  67. transport_adaptation_extension_flag_ltw = 0x80,
  68. transport_adaptation_extension_flag_piecewise_rate = 0x40,
  69. transport_adaptation_extension_flag_seamless_splice = 0x20,
  70. };
  71. /**
  72. * Enumeration of flags controlling which values to extract using the
  73. * transport_packet_values_extract() function.
  74. */
  75. enum transport_value {
  76. /* normal adaptation */
  77. transport_value_pcr = 0x0001,
  78. transport_value_opcr = 0x0002,
  79. transport_value_splice_countdown = 0x0004,
  80. transport_value_private_data = 0x0008,
  81. /* extension adaptation */
  82. transport_value_ltw = 0x0100,
  83. transport_value_piecewise_rate = 0x0200,
  84. transport_value_seamless_splice = 0x0400,
  85. };
  86. /**
  87. * Structure describing a transport packet header.
  88. */
  89. struct transport_packet {
  90. uint8_t sync_byte;
  91. EBIT4(uint8_t transport_error_indicator : 1; ,
  92. uint8_t payload_unit_start_indicator : 1; ,
  93. uint8_t transport_priority : 1; ,
  94. uint8_t pid_hi : 5; );
  95. uint8_t pid_lo;
  96. EBIT3(uint8_t transport_scrambling_control : 2; ,
  97. uint8_t adaptation_field_control : 2; ,
  98. uint8_t continuity_counter : 4; );
  99. /* values */
  100. } __ucsi_packed;
  101. /**
  102. * Structure to extract values into using the transport_packet_values_extract()
  103. * function.
  104. */
  105. struct transport_values {
  106. enum transport_adaptation_flags flags; /* always extracted */
  107. uint8_t *payload; /* always extracted */
  108. uint16_t payload_length; /* always extracted */
  109. uint64_t pcr;
  110. uint64_t opcr;
  111. uint8_t splice_countdown;
  112. uint8_t private_data_length;
  113. uint8_t *private_data;
  114. uint16_t ltw_offset;
  115. uint32_t piecewise_rate;
  116. uint8_t splice_type;
  117. uint64_t dts_next_au;
  118. };
  119. /**
  120. * Extract the PID from a transport packet.
  121. *
  122. * @param pkt The packet.
  123. * @return The PID.
  124. */
  125. static inline int transport_packet_pid(struct transport_packet *pkt)
  126. {
  127. return (pkt->pid_hi << 8) | (pkt->pid_lo);
  128. }
  129. /**
  130. * Process a buffer into a transport packet.
  131. *
  132. * @param buf Raw buffer. Note, this function assumes there are 188 bytes available.
  133. * @return transport_packet pointer, or NULL on error.
  134. */
  135. static inline struct transport_packet *transport_packet_init(unsigned char *buf)
  136. {
  137. struct transport_packet *pkt = (struct transport_packet*) buf;
  138. if (pkt->sync_byte != TRANSPORT_PACKET_SYNC)
  139. return NULL;
  140. if (transport_packet_pid(pkt) >= TRANSPORT_MAX_PIDS)
  141. return NULL;
  142. return pkt;
  143. }
  144. /**
  145. * Check the continuity counter for a packet in a PID stream.
  146. *
  147. * @param pkt transport_packet to check.
  148. * @param discontinuity_indicator Set to 1 if the packet's discontinuity_indicator flag is set.
  149. * @param cstate Pointer to a single 8 bit character, used to store state for validating
  150. * continuity. To initialise the state, simply set it to 0 before the first call.
  151. * @return 0 if the continuity was correct, or nonzero on error. cstate will not be updated on error,
  152. * it is up to the caller to clear it to accept the next packet.
  153. */
  154. extern int transport_packet_continuity_check(struct transport_packet *pkt,
  155. int discontinuity_indicator, unsigned char *cstate);
  156. /**
  157. * Extract selected fields from a transport packet.
  158. *
  159. * @param pkt The packet.
  160. * @param out Destination structure for values.
  161. * @param extract Orred bitmask of enum transport_value - tells it what fields
  162. * to extract if they are available.
  163. * @return < 0 => error. Otherwise, an orred bitmask of enum transport_value
  164. * telling you what fields were successfully extracted.
  165. */
  166. extern int transport_packet_values_extract(struct transport_packet *pkt,
  167. struct transport_values *out,
  168. enum transport_value extract);
  169. #ifdef __cplusplus
  170. }
  171. #endif
  172. #endif