pmt_section.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * section and descriptor parser
  3. *
  4. * Copyright (C) 2005 Kenneth Aafloy (kenneth@linuxtv.org)
  5. * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #ifndef _UCSI_MPEG_PMT_SECTION_H
  22. #define _UCSI_MPEG_PMT_SECTION_H 1
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <libucsi/section.h>
  28. /**
  29. * mpeg_pmt_section structure.
  30. */
  31. struct mpeg_pmt_section {
  32. struct section_ext head;
  33. EBIT2(uint16_t reserved_1 : 3; ,
  34. uint16_t pcr_pid :13; );
  35. EBIT2(uint16_t reserved_2 : 4; ,
  36. uint16_t program_info_length :12; );
  37. /* struct descriptor descriptors[] */
  38. /* struct mpeg_pmt_stream streams[] */
  39. } __ucsi_packed;
  40. /**
  41. * A stream within an mpeg_pmt_section.
  42. */
  43. struct mpeg_pmt_stream {
  44. uint8_t stream_type;
  45. EBIT2(uint16_t reserved_1 : 3; ,
  46. uint16_t pid :13; );
  47. EBIT2(uint16_t reserved_2 : 4; ,
  48. uint16_t es_info_length :12; );
  49. /* struct descriptor descriptors[] */
  50. } __ucsi_packed;
  51. /**
  52. * Process an mpeg_pmt_section section.
  53. *
  54. * @param section Pointer to the generic section header.
  55. * @return Pointer to the mpeg_pmt_section structure, or NULL on error.
  56. */
  57. extern struct mpeg_pmt_section *mpeg_pmt_section_codec(struct section_ext *section);
  58. /**
  59. * Accessor for program_number field of a PMT.
  60. *
  61. * @param pmt PMT pointer.
  62. * @return The program_number.
  63. */
  64. static inline uint16_t mpeg_pmt_section_program_number(struct mpeg_pmt_section *pmt)
  65. {
  66. return pmt->head.table_id_ext;
  67. }
  68. /**
  69. * Convenience iterator for the descriptors field of the mpeg_pmt_section structure.
  70. *
  71. * @param pmt Pointer to the mpeg_pmt_section structure.
  72. * @param pos Variable holding a pointer to the current descriptor.
  73. */
  74. #define mpeg_pmt_section_descriptors_for_each(pmt, pos) \
  75. for ((pos) = mpeg_pmt_section_descriptors_first(pmt); \
  76. (pos); \
  77. (pos) = mpeg_pmt_section_descriptors_next(pmt, pos))
  78. /**
  79. * Convenience iterator for the streams field of the mpeg_pmt_section structure.
  80. *
  81. * @param pmt Pointer to the mpeg_pmt_section structure.
  82. * @param pos Variable holding a pointer to the current mpeg_pmt_stream.
  83. */
  84. #define mpeg_pmt_section_streams_for_each(pmt, pos) \
  85. for ((pos) = mpeg_pmt_section_streams_first(pmt); \
  86. (pos); \
  87. (pos) = mpeg_pmt_section_streams_next(pmt, pos))
  88. /**
  89. * Convenience iterator for the descriptors field of an mpeg_pmt_stream structure.
  90. *
  91. * @param stream Pointer to the mpeg_pmt_stream structure.
  92. * @param pos Variable holding a pointer to the current descriptor.
  93. */
  94. #define mpeg_pmt_stream_descriptors_for_each(stream, pos) \
  95. for ((pos) = mpeg_pmt_stream_descriptors_first(stream); \
  96. (pos); \
  97. (pos) = mpeg_pmt_stream_descriptors_next(stream, pos))
  98. /******************************** PRIVATE CODE ********************************/
  99. static inline struct descriptor *
  100. mpeg_pmt_section_descriptors_first(struct mpeg_pmt_section * pmt)
  101. {
  102. if (pmt->program_info_length == 0)
  103. return NULL;
  104. return (struct descriptor *)
  105. ((uint8_t *) pmt + sizeof(struct mpeg_pmt_section));
  106. }
  107. static inline struct descriptor *
  108. mpeg_pmt_section_descriptors_next(struct mpeg_pmt_section *pmt,
  109. struct descriptor* pos)
  110. {
  111. return next_descriptor((uint8_t *) pmt + sizeof(struct mpeg_pmt_section),
  112. pmt->program_info_length,
  113. pos);
  114. }
  115. static inline struct mpeg_pmt_stream *
  116. mpeg_pmt_section_streams_first(struct mpeg_pmt_section * pmt)
  117. {
  118. size_t pos = sizeof(struct mpeg_pmt_section) + pmt->program_info_length;
  119. if (pos >= section_ext_length(&pmt->head))
  120. return NULL;
  121. return (struct mpeg_pmt_stream *)((uint8_t *)pmt + pos);
  122. }
  123. static inline struct mpeg_pmt_stream *
  124. mpeg_pmt_section_streams_next(struct mpeg_pmt_section * pmt,
  125. struct mpeg_pmt_stream * pos)
  126. {
  127. uint8_t *end = (uint8_t*) pmt + section_ext_length(&pmt->head);
  128. uint8_t *next = (uint8_t *) pos + sizeof(struct mpeg_pmt_stream) +
  129. pos->es_info_length;
  130. if (next >= end)
  131. return NULL;
  132. return (struct mpeg_pmt_stream *) next;
  133. }
  134. static inline struct descriptor *
  135. mpeg_pmt_stream_descriptors_first(struct mpeg_pmt_stream *stream)
  136. {
  137. if (stream->es_info_length == 0)
  138. return NULL;
  139. return (struct descriptor *)
  140. ((uint8_t*) stream + sizeof(struct mpeg_pmt_stream));
  141. }
  142. static inline struct descriptor *
  143. mpeg_pmt_stream_descriptors_next(struct mpeg_pmt_stream *stream,
  144. struct descriptor* pos)
  145. {
  146. return next_descriptor((uint8_t *) stream + sizeof(struct mpeg_pmt_stream),
  147. stream->es_info_length,
  148. pos);
  149. }
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153. #endif