mpeg_pes.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_PES_H
  21. #define _MPEG_PES_H
  22. /**
  23. * @file mpeg_pes.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://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  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_PES
  42. * @brief MPEG Packetized Elementary Stream magic
  43. * @ingroup dvb_table
  44. * @def DVB_MPEG_PES_AUDIO
  45. * @brief PES Audio
  46. * @ingroup dvb_table
  47. * @def DVB_MPEG_PES_VIDEO
  48. * @brief PES Video
  49. * @ingroup dvb_table
  50. * @def DVB_MPEG_STREAM_MAP
  51. * @brief PES Stream map
  52. * @ingroup dvb_table
  53. * @def DVB_MPEG_STREAM_PADDING
  54. * @brief PES padding
  55. * @ingroup dvb_table
  56. * @def DVB_MPEG_STREAM_PRIVATE_2
  57. * @brief PES private
  58. * @ingroup dvb_table
  59. * @def DVB_MPEG_STREAM_ECM
  60. * @brief PES ECM Stream
  61. * @ingroup dvb_table
  62. * @def DVB_MPEG_STREAM_EMM
  63. * @brief PES EMM Stream
  64. * @ingroup dvb_table
  65. * @def DVB_MPEG_STREAM_DIRECTORY
  66. * @brief PES Stream directory
  67. * @ingroup dvb_table
  68. * @def DVB_MPEG_STREAM_DSMCC
  69. * @brief PES DSMCC
  70. * @ingroup dvb_table
  71. * @def DVB_MPEG_STREAM_H222E
  72. * @brief PES H.222.1 type E
  73. * @ingroup dvb_table
  74. */
  75. #define DVB_MPEG_PES 0x00001
  76. #define DVB_MPEG_PES_AUDIO 0xc0 ... 0xcf
  77. #define DVB_MPEG_PES_VIDEO 0xe0 ... 0xef
  78. #define DVB_MPEG_STREAM_MAP 0xBC
  79. #define DVB_MPEG_STREAM_PADDING 0xBE
  80. #define DVB_MPEG_STREAM_PRIVATE_2 0x5F
  81. #define DVB_MPEG_STREAM_ECM 0x70
  82. #define DVB_MPEG_STREAM_EMM 0x71
  83. #define DVB_MPEG_STREAM_DIRECTORY 0xFF
  84. #define DVB_MPEG_STREAM_DSMCC 0x7A
  85. #define DVB_MPEG_STREAM_H222E 0xF8
  86. /**
  87. * @struct ts_t
  88. * @brief MPEG PES timestamp structure, used for dts and pts
  89. * @ingroup dvb_table
  90. *
  91. * @param tag 4 bits Should be 0010 for PTS and 0011 for DTS
  92. * @param bits30 3 bits Timestamp bits 30-32
  93. * @param one 1 bit Sould be 1
  94. * @param bits15 15 bits Timestamp bits 15-29
  95. * @param one1 1 bit Should be 1
  96. * @param bits00 15 Bits Timestamp bits 0-14
  97. * @param one2 1 bit Should be 1
  98. */
  99. struct ts_t {
  100. uint8_t one:1;
  101. uint8_t bits30:3;
  102. uint8_t tag:4;
  103. union {
  104. uint16_t bitfield;
  105. struct {
  106. uint16_t one1:1;
  107. uint16_t bits15:15;
  108. } __attribute__((packed));
  109. } __attribute__((packed));
  110. union {
  111. uint16_t bitfield2;
  112. struct {
  113. uint16_t one2:1;
  114. uint16_t bits00:15;
  115. } __attribute__((packed));
  116. } __attribute__((packed));
  117. } __attribute__((packed));
  118. /**
  119. * @struct dvb_mpeg_pes_optional
  120. * @brief MPEG PES optional header
  121. * @ingroup dvb_table
  122. *
  123. * @param two 2 bits Should be 10
  124. * @param PES_scrambling_control 2 bits PES Scrambling Control (Not Scrambled=00, otherwise scrambled)
  125. * @param PES_priority 1 bit PES Priority
  126. * @param data_alignment_indicator 1 bit PES data alignment
  127. * @param copyright 1 bit PES content protected by copyright
  128. * @param original_or_copy 1 bit PES content is original (=1) or copied (=0)
  129. * @param PTS_DTS 2 bit PES header contains PTS (=10, =11) and/or DTS (=01, =11)
  130. * @param ESCR 1 bit PES header contains ESCR fields
  131. * @param ES_rate 1 bit PES header contains ES_rate field
  132. * @param DSM_trick_mode 1 bit PES header contains DSM_trick_mode field
  133. * @param additional_copy_info 1 bit PES header contains additional_copy_info field
  134. * @param PES_CRC 1 bit PES header contains CRC field
  135. * @param PES_extension 1 bit PES header contains extension field
  136. * @param length 8 bit PES header data length
  137. * @param pts 64 bit PES PTS timestamp
  138. * @param dts 64 bit PES DTS timestamp
  139. */
  140. struct dvb_mpeg_pes_optional {
  141. union {
  142. uint16_t bitfield;
  143. struct {
  144. uint16_t PES_extension:1;
  145. uint16_t PES_CRC:1;
  146. uint16_t additional_copy_info:1;
  147. uint16_t DSM_trick_mode:1;
  148. uint16_t ES_rate:1;
  149. uint16_t ESCR:1;
  150. uint16_t PTS_DTS:2;
  151. uint16_t original_or_copy:1;
  152. uint16_t copyright:1;
  153. uint16_t data_alignment_indicator:1;
  154. uint16_t PES_priority:1;
  155. uint16_t PES_scrambling_control:2;
  156. uint16_t two:2;
  157. } __attribute__((packed));
  158. } __attribute__((packed));
  159. uint8_t length;
  160. uint64_t pts;
  161. uint64_t dts;
  162. } __attribute__((packed));
  163. /**
  164. * @struct dvb_mpeg_pes
  165. * @brief MPEG PES data structure
  166. * @ingroup dvb_table
  167. *
  168. * @param sync 24 bits DVB_MPEG_PES
  169. * @param stream_id 8 bits PES Stream ID
  170. * @param length 16 bits PES packet length
  171. * @param optional Pointer to optional PES header
  172. */
  173. struct dvb_mpeg_pes {
  174. union {
  175. uint32_t bitfield;
  176. struct {
  177. uint32_t stream_id:8;
  178. uint32_t sync:24;
  179. } __attribute__((packed));
  180. } __attribute__((packed));
  181. uint16_t length;
  182. struct dvb_mpeg_pes_optional optional[];
  183. } __attribute__((packed));
  184. struct dvb_v5_fe_parms;
  185. #ifdef __cplusplus
  186. extern "C" {
  187. #endif
  188. /**
  189. * @brief Initialize a struct dvb_mpeg_pes from buffer
  190. * @ingroup dvb_table
  191. *
  192. * @param parms struct dvb_v5_fe_parms for log functions
  193. * @param buf Buffer
  194. * @param buflen Length of buffer
  195. * @param table Pointer to allocated struct dvb_mpeg_pes
  196. *
  197. * @return Length of data in table
  198. *
  199. * This function copies the length of struct dvb_mpeg_pes
  200. * to table and fixes endianness. The pointer table has to be
  201. * allocated on stack or dynamically.
  202. */
  203. ssize_t dvb_mpeg_pes_init(struct dvb_v5_fe_parms *parms, const uint8_t *buf, ssize_t buflen,
  204. uint8_t *table);
  205. /**
  206. * @brief Deallocate memory associated with a struct dvb_mpeg_pes
  207. * @ingroup dvb_table
  208. *
  209. * @param pes struct dvb_mpeg_pes to be deallocated
  210. *
  211. * If the pointer pes was allocated dynamically, this function
  212. * can be used to free the memory.
  213. */
  214. void dvb_mpeg_pes_free(struct dvb_mpeg_pes *pes);
  215. /**
  216. * @brief Print details of struct dvb_mpeg_pes
  217. * @ingroup dvb_table
  218. *
  219. * @param parms struct dvb_v5_fe_parms for log functions
  220. * @param pes Pointer to struct dvb_mpeg_pes to print
  221. *
  222. * This function prints the fields of struct dvb_mpeg_pes
  223. */
  224. void dvb_mpeg_pes_print (struct dvb_v5_fe_parms *parms, struct dvb_mpeg_pes *pes);
  225. #ifdef __cplusplus
  226. }
  227. #endif
  228. #endif