mpeg_ts.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2013-2014 - Andre Roth <neolynx@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation version 2
  7. * of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  18. *
  19. */
  20. #ifndef _MPEG_TS_H
  21. #define _MPEG_TS_H
  22. /**
  23. * @file mpeg_ts.h
  24. * @ingroup dvb_table
  25. * @brief Provides the table parser for the MPEG-PES Elementary Stream
  26. * @copyright GNU General Public License version 2 (GPLv2)
  27. * @author Andre Roth
  28. *
  29. * @par Relevant specs
  30. * The table described herein is defined in ISO 13818-1
  31. *
  32. * @see
  33. * http://en.wikipedia.org/wiki/MPEG_transport_stream
  34. *
  35. * @par Bug Report
  36. * Please submit bug reports and patches to linux-media@vger.kernel.org
  37. */
  38. #include <stdint.h>
  39. #include <unistd.h> /* ssize_t */
  40. /**
  41. * @def DVB_MPEG_TS
  42. * @brief MPEG Transport Stream magic
  43. * @ingroup dvb_table
  44. * @def DVB_MPEG_TS_PACKET_SIZE
  45. * @brief Size of an MPEG packet
  46. * @ingroup dvb_table
  47. */
  48. #define DVB_MPEG_TS 0x47
  49. #define DVB_MPEG_TS_PACKET_SIZE 188
  50. /**
  51. * @struct dvb_mpeg_ts_adaption
  52. * @brief MPEG TS header adaption field
  53. * @ingroup dvb_table
  54. *
  55. * @param type DVB_MPEG_ES_SEQ_START
  56. * @param length 1 bit Adaptation Field Length
  57. * @param discontinued 1 bit Discontinuity indicator
  58. * @param random_access 1 bit Random Access indicator
  59. * @param priority 1 bit Elementary stream priority indicator
  60. * @param PCR 1 bit PCR flag
  61. * @param OPCR 1 bit OPCR flag
  62. * @param splicing_point 1 bit Splicing point flag
  63. * @param private_data 1 bit Transport private data flag
  64. * @param extension 1 bit Adaptation field extension flag
  65. * @param data Pointer to data
  66. */
  67. struct dvb_mpeg_ts_adaption {
  68. uint8_t length;
  69. struct {
  70. uint8_t extension:1;
  71. uint8_t private_data:1;
  72. uint8_t splicing_point:1;
  73. uint8_t OPCR:1;
  74. uint8_t PCR:1;
  75. uint8_t priority:1;
  76. uint8_t random_access:1;
  77. uint8_t discontinued:1;
  78. } __attribute__((packed));
  79. uint8_t data[];
  80. } __attribute__((packed));
  81. /**
  82. * @struct dvb_mpeg_ts
  83. * @brief MPEG TS header
  84. * @ingroup dvb_table
  85. *
  86. * @param sync_byte DVB_MPEG_TS
  87. * @param tei 1 bit Transport Error Indicator
  88. * @param payload_start 1 bit Payload Unit Start Indicator
  89. * @param priority 1 bit Transport Priority
  90. * @param pid 13 bits Packet Identifier
  91. * @param scrambling 2 bits Scrambling control
  92. * @param adaptation_field 1 bit Adaptation field exist
  93. * @param payload 1 bit Contains payload
  94. * @param continuity_counter 4 bits Continuity counter
  95. * @param adaption Pointer to optional adaption fiels (struct dvb_mpeg_ts_adaption)
  96. */
  97. struct dvb_mpeg_ts {
  98. uint8_t sync_byte;
  99. union {
  100. uint16_t bitfield;
  101. struct {
  102. uint16_t pid:13;
  103. uint16_t priority:1;
  104. uint16_t payload_start:1;
  105. uint16_t tei:1;
  106. } __attribute__((packed));
  107. } __attribute__((packed));
  108. struct {
  109. uint8_t continuity_counter:4;
  110. uint8_t payload:1;
  111. uint8_t adaptation_field:1;
  112. uint8_t scrambling:2;
  113. } __attribute__((packed));
  114. struct dvb_mpeg_ts_adaption adaption[];
  115. } __attribute__((packed));
  116. struct dvb_v5_fe_parms;
  117. #ifdef __cplusplus
  118. extern "C" {
  119. #endif
  120. /**
  121. * @brief Initialize a struct dvb_mpeg_ts from buffer
  122. * @ingroup dvb_table
  123. *
  124. * @param parms struct dvb_v5_fe_parms for log functions
  125. * @param buf Buffer
  126. * @param buflen Length of buffer
  127. * @param table Pointer to allocated struct dvb_mpeg_ts
  128. * @param table_length Pointer to size_t where length will be written to
  129. *
  130. * @return Length of data in table
  131. *
  132. * This function copies the length of struct dvb_mpeg_ts
  133. * to table and fixes endianness. The pointer table has to be allocated
  134. * on stack or dynamically.
  135. */
  136. ssize_t dvb_mpeg_ts_init (struct dvb_v5_fe_parms *parms, const uint8_t *buf, ssize_t buflen,
  137. uint8_t *table, ssize_t *table_length);
  138. /**
  139. * @brief Deallocate memory associated with a struct dvb_mpeg_ts
  140. * @ingroup dvb_table
  141. *
  142. * @param ts struct dvb_mpeg_ts to be deallocated
  143. *
  144. * If ts was allocated dynamically, this function
  145. * can be used to free the memory.
  146. */
  147. void dvb_mpeg_ts_free(struct dvb_mpeg_ts *ts);
  148. /**
  149. * @brief Print details of struct dvb_mpeg_ts
  150. * @ingroup dvb_table
  151. *
  152. * @param parms struct dvb_v5_fe_parms for log functions
  153. * @param ts Pointer to struct dvb_mpeg_ts to print
  154. *
  155. * This function prints the fields of struct dvb_mpeg_ts
  156. */
  157. void dvb_mpeg_ts_print(struct dvb_v5_fe_parms *parms, struct dvb_mpeg_ts *ts);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #endif