eia608.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**********************************************************************************************/
  2. /* The MIT License */
  3. /* */
  4. /* Copyright 2016-2016 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. #include "utf8.h"
  27. #include "eia608_charmap.h"
  28. ////////////////////////////////////////////////////////////////////////////////
  29. // Parity
  30. #define EIA608_BX(B,X) (((B)>>X)&0x01)
  31. #define EIA608_BP(B) ((B)&0x7F) | ((EIA608_BX((B),0)^EIA608_BX(B,1)^EIA608_BX((B),2)^EIA608_BX((B),3)^EIA608_BX((B),4)^EIA608_BX((B),5)^EIA608_BX((B),6)^(0x01))<<7)
  32. #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)
  33. #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)
  34. static const uint8_t eia608_parity_table[] = { EIA608_B1 (0), EIA608_B1 (64) };
  35. extern const char* eia608_mode_map[];
  36. extern const char* eia608_style_map[];
  37. #ifdef _MSC_VER
  38. #ifndef inline
  39. #define inline __inline
  40. #endif
  41. #endif
  42. /*! \brief
  43. \param
  44. */
  45. static inline uint8_t eia608_parity_byte (uint8_t cc_data) { return eia608_parity_table[0x7F & cc_data]; }
  46. /*! \brief
  47. \param
  48. */
  49. 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)); }
  50. /*! \brief
  51. \param
  52. */
  53. static inline uint16_t eia608_parity (uint16_t cc_data) { return eia608_parity_word (cc_data); }
  54. /*! \brief
  55. \param
  56. */
  57. static inline int eia608_parity_varify (uint16_t cc_data) { return eia608_parity_word (cc_data) == cc_data ? 1 : 0; }
  58. /*! \brief
  59. \param
  60. */
  61. static inline int eia608_parity_strip (uint16_t cc_data) { return cc_data & 0x7F7F; }
  62. /*! \brief
  63. \param
  64. */
  65. static inline int eia608_test_second_channel_bit (uint16_t cc_data) { return (cc_data & 0x0800); }
  66. ////////////////////////////////////////////////////////////////////////////////
  67. // cc_data types
  68. /*! \brief
  69. \param
  70. */
  71. static inline int eia608_is_basicna (uint16_t cc_data) { return 0x0000 != (0x6000 & cc_data); /*&& 0x1F00 < (0x7F00 & cc_data);*/ }
  72. /*! \brief
  73. \param
  74. */
  75. static inline int eia608_is_preamble (uint16_t cc_data) { return 0x1040 == (0x7040 & cc_data); }
  76. /*! \brief
  77. \param
  78. */
  79. static inline int eia608_is_midrowchange (uint16_t cc_data) { return 0x1120 == (0x7770 & cc_data); }
  80. /*! \brief
  81. \param
  82. */
  83. static inline int eia608_is_specialna (uint16_t cc_data) { return 0x1130 == (0x7770 & cc_data); }
  84. /*! \brief
  85. \param
  86. */
  87. static inline int eia608_is_xds (uint16_t cc_data) { return 0x0000 == (0x7070 & cc_data) && 0x0000 != (0x0F0F & cc_data); }
  88. /*! \brief
  89. \param
  90. */
  91. static inline int eia608_is_westeu (uint16_t cc_data) { return 0x1220 == (0x7660 & cc_data); }
  92. /*! \brief
  93. \param
  94. */
  95. static inline int eia608_is_control (uint16_t cc_data) { return 0x1420 == (0x7670 & cc_data) || 0x1720 == (0x7770 & cc_data); }
  96. /*! \brief
  97. \param
  98. */
  99. static inline int eia608_is_norpak (uint16_t cc_data) { return 0x1724 == (0x777C & cc_data) || 0x1728 == (0x777C & cc_data); }
  100. /*! \brief
  101. \param
  102. */
  103. static inline int eia608_is_padding (uint16_t cc_data) { return 0x8080 == cc_data; }
  104. ////////////////////////////////////////////////////////////////////////////////
  105. // preamble
  106. typedef enum {
  107. eia608_style_white = 0,
  108. eia608_style_green = 1,
  109. eia608_style_blue = 2,
  110. eia608_style_cyan = 3,
  111. eia608_style_red = 4,
  112. eia608_style_yellow = 5,
  113. eia608_style_magenta = 6,
  114. eia608_style_italics = 7,
  115. } eia608_style_t;
  116. /*! \brief
  117. \param
  118. */
  119. int eia608_parse_preamble (uint16_t cc_data, int* row, int* col, eia608_style_t* style, int* chan, int* underline);
  120. /*! \brief
  121. \param
  122. */
  123. int eia608_parse_midrowchange (uint16_t cc_data, int* chan, eia608_style_t* style, int* underline);
  124. /*! \brief
  125. \param
  126. */
  127. uint16_t eia608_row_column_pramble (int row, int col, int chan, int underline);
  128. /*! \brief
  129. \param
  130. */
  131. uint16_t eia608_row_style_pramble (int row, eia608_style_t style, int chan, int underline);
  132. ////////////////////////////////////////////////////////////////////////////////
  133. // control command
  134. typedef enum { // yes, no?
  135. eia608_tab_offset_0 = 0x1720,
  136. eia608_tab_offset_1 = 0x1721,
  137. eia608_tab_offset_2 = 0x1722,
  138. eia608_tab_offset_3 = 0x1723,
  139. eia608_control_resume_caption_loading = 0x1420,
  140. eia608_control_backspace = 0x1421,
  141. eia608_control_alarm_off = 0x1422,
  142. eia608_control_alarm_on = 0x1423,
  143. eia608_control_delete_to_end_of_row = 0x1424,
  144. eia608_control_roll_up_2 = 0x1425,
  145. eia608_control_roll_up_3 = 0x1426,
  146. eia608_control_roll_up_4 = 0x1427,
  147. eia608_control_resume_direct_captioning = 0x1429,
  148. eia608_control_text_restart = 0x142A,
  149. eia608_control_text_resume_text_display = 0x142B,
  150. eia608_control_erase_display_memory = 0x142C,
  151. eia608_control_carriage_return = 0x142D,
  152. eia608_control_erase_non_displayed_memory = 0x142E,
  153. eia608_control_end_of_caption = 0x142F,
  154. } eia608_control_t;
  155. #define eia608_control_popon eia608_control_resume_caption_loading
  156. #define eia608_control_painton eia608_control_resume_direct_captioning
  157. /*! \brief
  158. \param
  159. */
  160. uint16_t eia608_control_command (eia608_control_t cmd, int cc);
  161. /*! \brief
  162. \param
  163. */
  164. static inline uint16_t eia608_tab (int size, int cc) { return eia608_control_command ( (eia608_control_t) (eia608_tab_offset_0 | (size&0x0F)),cc); }
  165. /*! \brief
  166. \param
  167. */
  168. eia608_control_t eia608_parse_control (uint16_t cc_data, int* cc);
  169. ////////////////////////////////////////////////////////////////////////////////
  170. // text
  171. /*! \brief
  172. \param c
  173. */
  174. uint16_t eia608_from_utf8_1 (const utf8_char_t* c, int chan);
  175. /*! \brief
  176. \param
  177. */
  178. uint16_t eia608_from_utf8_2 (const utf8_char_t* c1, const utf8_char_t* c2);
  179. /*! \brief
  180. \param
  181. */
  182. uint16_t eia608_from_basicna (uint16_t bna1, uint16_t bna2);
  183. /*! \brief
  184. \param
  185. */
  186. int eia608_to_utf8 (uint16_t c, int* chan, utf8_char_t* char1, utf8_char_t* char2);
  187. ////////////////////////////////////////////////////////////////////////////////
  188. /*! \brief
  189. \param
  190. */
  191. void eia608_dump (uint16_t cc_data);
  192. ////////////////////////////////////////////////////////////////////////////////
  193. #endif