vbi_data_descriptor.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_DVB_VBI_DATA_DESCRIPTOR
  22. #define _UCSI_DVB_VBI_DATA_DESCRIPTOR 1
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <libucsi/descriptor.h>
  28. #include <libucsi/endianops.h>
  29. /**
  30. * Possible values for the data_service_id field.
  31. */
  32. enum {
  33. DVB_VBI_DATA_SERVICE_ID_EBU = 0x01,
  34. DVB_VBI_DATA_SERVICE_ID_INVERTED = 0x02,
  35. DVB_VBI_DATA_SERVICE_ID_VPS = 0x04,
  36. DVB_VBI_DATA_SERVICE_ID_WSS = 0x05,
  37. DVB_VBI_DATA_SERVICE_ID_CC = 0x06,
  38. DVB_VBI_DATA_SERVICE_ID_MONO_422 = 0x07,
  39. };
  40. /**
  41. * dvb_vbi_data_descriptor structure
  42. */
  43. struct dvb_vbi_data_descriptor {
  44. struct descriptor d;
  45. /* struct dvb_vbi_data_entry entries[] */
  46. } __ucsi_packed;
  47. /**
  48. * An entry in the dvb_vbi_data_descriptor entries field.
  49. */
  50. struct dvb_vbi_data_entry {
  51. uint8_t data_service_id;
  52. uint8_t data_length;
  53. /* uint8_t data[] */
  54. } __ucsi_packed;
  55. /**
  56. * Format of the dvb_vbi_data_entry data field, if data_service_id == 1,2,4,5,6,7.
  57. */
  58. struct dvb_vbi_data_x {
  59. EBIT3(uint8_t reserved : 2; ,
  60. uint8_t field_parity : 1; ,
  61. uint8_t line_offset : 5; );
  62. } __ucsi_packed;
  63. /**
  64. * Process a dvb_vbi_data_descriptor.
  65. *
  66. * @param d Generic descriptor structure.
  67. * @return dvb_vbi_data_descriptor pointer, or NULL on error.
  68. */
  69. static inline struct dvb_vbi_data_descriptor*
  70. dvb_vbi_data_descriptor_codec(struct descriptor* d)
  71. {
  72. uint8_t* p = (uint8_t*) d + 2;
  73. uint32_t pos = 0;
  74. uint32_t len = d->len;
  75. while(pos < len) {
  76. struct dvb_vbi_data_entry *e =
  77. (struct dvb_vbi_data_entry*) (p+pos);
  78. pos += sizeof(struct dvb_vbi_data_entry);
  79. if (pos > len)
  80. return NULL;
  81. pos += e->data_length;
  82. if (pos > len)
  83. return NULL;
  84. }
  85. return (struct dvb_vbi_data_descriptor*) d;
  86. }
  87. /**
  88. * Iterator for entries field in a dvb_vbi_data_descriptor structure.
  89. *
  90. * @param d Pointer to dvb_vbi_data_descriptor structure.
  91. * @param pos Variable holding pointer to the current dvb_vbi_data_entry structure.
  92. */
  93. #define dvb_vbi_data_descriptor_entries_for_each(d, pos) \
  94. for ((pos) = dvb_vbi_data_descriptor_entries_first(d); \
  95. (pos); \
  96. (pos) = dvb_vbi_data_descriptor_entries_next(d, pos))
  97. /**
  98. * Get a pointer to the data field of a dvb_vbi_data_entry.
  99. *
  100. * @param d dvb_vbi_data_entry structure.
  101. * @return Pointer to the data field.
  102. */
  103. static inline uint8_t *
  104. dvb_vbi_data_entry_data(struct dvb_vbi_data_entry *d)
  105. {
  106. return (uint8_t *) d + sizeof(struct dvb_vbi_data_entry);
  107. }
  108. /**
  109. * Get a pointer to the data field of a dvb_vbi_data_x for id 1,2,4,5,6,7.
  110. *
  111. * @param d dvb_vbi_data_entry structure.
  112. * @return Pointer to the data field, or NULL if invalid
  113. */
  114. static inline struct dvb_vbi_data_x*
  115. dvb_vbi_data_entry_data_x(struct dvb_vbi_data_entry *d)
  116. {
  117. switch(d->data_service_id) {
  118. case 1:
  119. case 2:
  120. case 4:
  121. case 5:
  122. case 6:
  123. case 7:
  124. return (struct dvb_vbi_data_x*) ((uint8_t *) d + sizeof(struct dvb_vbi_data_entry));
  125. }
  126. return NULL;
  127. }
  128. /******************************** PRIVATE CODE ********************************/
  129. static inline struct dvb_vbi_data_entry*
  130. dvb_vbi_data_descriptor_entries_first(struct dvb_vbi_data_descriptor *d)
  131. {
  132. if (d->d.len == 0)
  133. return NULL;
  134. return (struct dvb_vbi_data_entry *)
  135. ((uint8_t*) d + sizeof(struct dvb_vbi_data_descriptor));
  136. }
  137. static inline struct dvb_vbi_data_entry*
  138. dvb_vbi_data_descriptor_entries_next(struct dvb_vbi_data_descriptor *d,
  139. struct dvb_vbi_data_entry *pos)
  140. {
  141. uint8_t *end = (uint8_t*) d + 2 + d->d.len;
  142. uint8_t *next = (uint8_t *) pos + sizeof(struct dvb_vbi_data_entry) +
  143. pos->data_length;
  144. if (next >= end)
  145. return NULL;
  146. return (struct dvb_vbi_data_entry *) next;
  147. }
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #endif