caption.h 5.7 KB

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