caption.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_H
  25. #define LIBCAPTION_H
  26. #include "utf8.h"
  27. #include "xds.h"
  28. #include "eia608.h"
  29. // ssize_t is POSIX and does not exist on Windows
  30. #if defined(_MSC_VER)
  31. #if defined(_WIN64)
  32. typedef signed long ssize_t;
  33. #else
  34. typedef signed int ssize_t;
  35. #endif
  36. #endif
  37. typedef enum {
  38. LIBCAPTION_OK = 1,
  39. LIBCAPTION_ERROR = 0,
  40. LIBCAPTION_READY = 2
  41. } libcaption_stauts_t;
  42. /*! \brief
  43. \param
  44. */
  45. static inline libcaption_stauts_t libcaption_status_update (libcaption_stauts_t old_stat, libcaption_stauts_t new_stat) { return (LIBCAPTION_ERROR == old_stat || LIBCAPTION_ERROR == new_stat) ? LIBCAPTION_ERROR : (LIBCAPTION_READY == old_stat) ? LIBCAPTION_READY : new_stat; }
  46. #define SCREEN_ROWS 15
  47. #define SCREEN_COLS 32
  48. typedef struct {
  49. unsigned int uln : 1; //< underline
  50. unsigned int sty : 3; //< style
  51. utf8_char_t data[5]; //< 4 byte utf8 values plus null term
  52. } caption_frame_cell_t;
  53. typedef struct {
  54. caption_frame_cell_t cell[SCREEN_ROWS][SCREEN_COLS];
  55. } caption_frame_buffer_t;
  56. typedef struct {
  57. unsigned int uln : 1; //< underline
  58. unsigned int sty : 3; //< style
  59. unsigned int mod : 3; //< current mode
  60. unsigned int rup : 2; //< roll-up line count minus 1
  61. uint16_t row, col, cc_data;
  62. } caption_frame_state_t;
  63. // timestamp and duration are in seconds
  64. typedef struct {
  65. double timestamp;
  66. double duration;
  67. xds_t xds;
  68. caption_frame_state_t state;
  69. caption_frame_buffer_t front;
  70. caption_frame_buffer_t back;
  71. } caption_frame_t;
  72. // typedef enum {
  73. // eia608_paint_on = 0,
  74. // eia608_pop_on = 1,
  75. // eia608_rollup_2 = 2,
  76. // eia608_rollup_3 = 3,
  77. // eia608_rollup_4 = 4,
  78. // } eia608_display_mode_t;
  79. // eia608_display_mode_t caption_frame_mode (caption_frame_t* frame);
  80. /*!
  81. \brief Initializes an allocated caption_frame_t instance
  82. \param frame Pointer to prealocated caption_frame_t object
  83. */
  84. void caption_frame_init (caption_frame_t* frame);
  85. /*! \brief Writes a single charcter to a caption_frame_t object
  86. \param frame A pointer to an allocted and initialized caption_frame_t object
  87. \param row Row position to write charcter, must be between 0 and SCREEN_ROWS-1
  88. \param col Column position to write charcter, must be between 0 and SCREEN_ROWS-1
  89. \param style Style to apply to charcter
  90. \param underline Set underline attribute, 0 = off any other value = on
  91. \param c pointer to a single valid utf8 charcter. Bytes are automatically determined, and a NULL terminator is not required
  92. */
  93. int caption_frame_write_char (caption_frame_t* frame, int row, int col, eia608_style_t style, int underline, const utf8_char_t* c);
  94. /*! \brief
  95. \param
  96. */
  97. const utf8_char_t* caption_frame_read_char (caption_frame_t* frame, int row, int col, eia608_style_t* style, int* underline);
  98. /*! \brief
  99. \param
  100. */
  101. /*! \brief
  102. \param
  103. */
  104. libcaption_stauts_t caption_frame_decode (caption_frame_t* frame, uint16_t cc_data, double timestamp);
  105. /*! \brief
  106. \param
  107. */
  108. int caption_frame_from_text (caption_frame_t* frame, const utf8_char_t* data);
  109. /*! \brief
  110. \param
  111. */
  112. #define CAPTION_FRAME_TEXT_BYTES (((2+SCREEN_ROWS)*SCREEN_COLS*4)+1)
  113. void caption_frame_to_text (caption_frame_t* frame, utf8_char_t* data);
  114. /*! \brief
  115. \param
  116. */
  117. #define CAPTION_FRAME_DUMP_BUF_SIZE 4096
  118. size_t caption_frame_dump_buffer (caption_frame_t* frame, utf8_char_t* buf);
  119. void caption_frame_dump (caption_frame_t* frame);
  120. /*! \brief
  121. \param
  122. */
  123. #define CAPTION_FRAME_JSON_BUF_SIZE 32768
  124. size_t caption_frame_json (caption_frame_t* frame, utf8_char_t* buf);
  125. #endif