eia608.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**********************************************************************************************/
  2. /* The MIT License */
  3. /* */
  4. /* Copyright 2016-2017 Twitch Interactive, Inc. or its affiliates. All Rights Reserved. */
  5. /* */
  6. /* Permission is hereby granted, free of charge, to any person obtaining a copy */
  7. /* of this software and associated documentation files (the "Software"), to deal */
  8. /* in the Software without restriction, including without limitation the rights */
  9. /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
  10. /* copies of the Software, and to permit persons to whom the Software is */
  11. /* furnished to do so, subject to the following conditions: */
  12. /* */
  13. /* The above copyright notice and this permission notice shall be included in */
  14. /* all copies or substantial portions of the Software. */
  15. /* */
  16. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
  17. /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
  18. /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
  19. /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
  20. /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
  21. /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */
  22. /* THE SOFTWARE. */
  23. /**********************************************************************************************/
  24. #ifndef LIBCAPTION_EIA608_H
  25. #define LIBCAPTION_EIA608_H
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include "eia608_charmap.h"
  30. #include "utf8.h"
  31. ////////////////////////////////////////////////////////////////////////////////
  32. // Parity
  33. #define EIA608_BX(B, X) (((B) << (X)) & 0x80)
  34. #define EIA608_BP(B) ((B)&0x7F) | (0x80 ^ EIA608_BX((B), 1) ^ EIA608_BX((B), 2) ^ EIA608_BX((B), 3) ^ EIA608_BX((B), 4) ^ EIA608_BX((B), 5) ^ EIA608_BX((B), 6) ^ EIA608_BX((B), 7))
  35. #define EIA608_B2(B) EIA608_BP((B) + 0), EIA608_BP((B) + 1), EIA608_BP((B) + 2), EIA608_BP((B) + 3), EIA608_BP((B) + 4), EIA608_BP((B) + 5), EIA608_BP((B) + 6), EIA608_BP((B) + 7)
  36. #define EIA608_B1(B) EIA608_B2((B) + 0), EIA608_B2((B) + 8), EIA608_B2((B) + 16), EIA608_B2((B) + 24), EIA608_B2((B) + 32), EIA608_B2((B) + 40), EIA608_B2((B) + 48), EIA608_B2((B) + 56)
  37. static const uint8_t eia608_parity_table[] = { EIA608_B1(0), EIA608_B1(64) };
  38. extern const char* eia608_style_map[];
  39. #ifdef _MSC_VER
  40. #ifndef inline
  41. #define inline __inline
  42. #endif
  43. #endif
  44. /*! \brief
  45. \param
  46. */
  47. static inline uint8_t eia608_parity_byte(uint8_t cc_data) { return eia608_parity_table[0x7F & cc_data]; }
  48. /*! \brief
  49. \param
  50. */
  51. static inline uint16_t eia608_parity_word(uint16_t cc_data) { return (uint16_t)((eia608_parity_byte((uint8_t)(cc_data >> 8)) << 8) | eia608_parity_byte((uint8_t)cc_data)); }
  52. /*! \brief
  53. \param
  54. */
  55. static inline uint16_t eia608_parity(uint16_t cc_data) { return eia608_parity_word(cc_data); }
  56. /*! \brief
  57. \param
  58. */
  59. static inline int eia608_parity_varify(uint16_t cc_data) { return eia608_parity_word(cc_data) == cc_data ? 1 : 0; }
  60. /*! \brief
  61. \param
  62. */
  63. static inline int eia608_parity_strip(uint16_t cc_data) { return cc_data & 0x7F7F; }
  64. /*! \brief
  65. \param
  66. */
  67. static inline int eia608_test_second_channel_bit(uint16_t cc_data) { return (cc_data & 0x0800); }
  68. ////////////////////////////////////////////////////////////////////////////////
  69. // cc_data types
  70. /*! \brief
  71. \param
  72. */
  73. static inline int eia608_is_basicna(uint16_t cc_data) { return 0x0000 != (0x6000 & cc_data); /*&& 0x1F00 < (0x7F00 & cc_data);*/ }
  74. /*! \brief
  75. \param
  76. */
  77. static inline int eia608_is_preamble(uint16_t cc_data) { return 0x1040 == (0x7040 & cc_data); }
  78. /*! \brief
  79. \param
  80. */
  81. static inline int eia608_is_midrowchange(uint16_t cc_data) { return 0x1120 == (0x7770 & cc_data); }
  82. /*! \brief
  83. \param
  84. */
  85. static inline int eia608_is_specialna(uint16_t cc_data) { return 0x1130 == (0x7770 & cc_data); }
  86. /*! \brief
  87. \param
  88. */
  89. static inline int eia608_is_xds(uint16_t cc_data) { return 0x0000 == (0x7070 & cc_data) && 0x0000 != (0x0F0F & cc_data); }
  90. /*! \brief
  91. \param
  92. */
  93. static inline int eia608_is_westeu(uint16_t cc_data) { return 0x1220 == (0x7660 & cc_data); }
  94. /*! \brief
  95. \param
  96. */
  97. static inline int eia608_is_control(uint16_t cc_data) { return 0x1420 == (0x7670 & cc_data) || 0x1720 == (0x7770 & cc_data); }
  98. /*! \brief
  99. \param
  100. */
  101. static inline int eia608_is_norpak(uint16_t cc_data) { return 0x1724 == (0x777C & cc_data) || 0x1728 == (0x777C & cc_data); }
  102. /*! \brief
  103. \param
  104. */
  105. static inline int eia608_is_padding(uint16_t cc_data) { return 0x8080 == cc_data; }
  106. ////////////////////////////////////////////////////////////////////////////////
  107. // preamble
  108. typedef enum {
  109. eia608_style_white = 0,
  110. eia608_style_green = 1,
  111. eia608_style_blue = 2,
  112. eia608_style_cyan = 3,
  113. eia608_style_red = 4,
  114. eia608_style_yellow = 5,
  115. eia608_style_magenta = 6,
  116. eia608_style_italics = 7,
  117. } eia608_style_t;
  118. /*! \brief
  119. \param
  120. */
  121. int eia608_parse_preamble(uint16_t cc_data, int* row, int* col, eia608_style_t* style, int* chan, int* underline);
  122. /*! \brief
  123. \param
  124. */
  125. int eia608_parse_midrowchange(uint16_t cc_data, int* chan, eia608_style_t* style, int* underline);
  126. /*! \brief
  127. \param
  128. */
  129. uint16_t eia608_row_column_pramble(int row, int col, int chan, int underline);
  130. /*! \brief
  131. \param
  132. */
  133. uint16_t eia608_row_style_pramble(int row, int chan, eia608_style_t style, int underline);
  134. /*! \brief
  135. \param
  136. */
  137. uint16_t eia608_midrow_change(int chan, eia608_style_t style, int underline);
  138. ////////////////////////////////////////////////////////////////////////////////
  139. // control command
  140. typedef enum {
  141. eia608_tab_offset_0 = 0x1720,
  142. eia608_tab_offset_1 = 0x1721,
  143. eia608_tab_offset_2 = 0x1722,
  144. eia608_tab_offset_3 = 0x1723,
  145. eia608_control_resume_caption_loading = 0x1420,
  146. eia608_control_backspace = 0x1421,
  147. eia608_control_alarm_off = 0x1422,
  148. eia608_control_alarm_on = 0x1423,
  149. eia608_control_delete_to_end_of_row = 0x1424,
  150. eia608_control_roll_up_2 = 0x1425,
  151. eia608_control_roll_up_3 = 0x1426,
  152. eia608_control_roll_up_4 = 0x1427,
  153. eia608_control_resume_direct_captioning = 0x1429,
  154. eia608_control_text_restart = 0x142A,
  155. eia608_control_text_resume_text_display = 0x142B,
  156. eia608_control_erase_display_memory = 0x142C,
  157. eia608_control_carriage_return = 0x142D,
  158. eia608_control_erase_non_displayed_memory = 0x142E,
  159. eia608_control_end_of_caption = 0x142F,
  160. } eia608_control_t;
  161. /*! \brief
  162. \param
  163. */
  164. uint16_t eia608_control_command(eia608_control_t cmd, int cc);
  165. /*! \brief
  166. \param
  167. */
  168. static inline uint16_t eia608_tab(int size, int cc) { return eia608_control_command((eia608_control_t)(eia608_tab_offset_0 | (size & 0x0F)), cc); }
  169. /*! \brief
  170. \param
  171. */
  172. eia608_control_t eia608_parse_control(uint16_t cc_data, int* cc);
  173. ////////////////////////////////////////////////////////////////////////////////
  174. // text
  175. /*! \brief
  176. \param c
  177. */
  178. uint16_t eia608_from_utf8_1(const utf8_char_t* c, int chan);
  179. /*! \brief
  180. \param
  181. */
  182. uint16_t eia608_from_utf8_2(const utf8_char_t* c1, const utf8_char_t* c2);
  183. /*! \brief
  184. \param
  185. */
  186. uint16_t eia608_from_basicna(uint16_t bna1, uint16_t bna2);
  187. /*! \brief
  188. \param
  189. */
  190. int eia608_to_utf8(uint16_t c, int* chan, utf8_char_t* char1, utf8_char_t* char2);
  191. ////////////////////////////////////////////////////////////////////////////////
  192. /*! \brief
  193. \param
  194. */
  195. void eia608_dump(uint16_t cc_data);
  196. ////////////////////////////////////////////////////////////////////////////////
  197. #ifdef __cplusplus
  198. }
  199. #endif
  200. #endif