vtt.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_VTT_H
  25. #define LIBCAPTION_VTT_H
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include "caption.h"
  30. #include "eia608.h"
  31. enum VTT_BLOCK_TYPE {
  32. VTT_REGION = 0,
  33. VTT_STYLE = 1,
  34. VTT_NOTE = 2,
  35. VTT_CUE = 3
  36. };
  37. // CUE represents a block of caption text
  38. typedef struct _vtt_block_t {
  39. struct _vtt_block_t* next;
  40. enum VTT_BLOCK_TYPE type;
  41. // CUE-Only
  42. double timestamp;
  43. double duration; // -1.0 for no duration
  44. char* cue_settings;
  45. char* cue_id;
  46. // Standard block data
  47. size_t text_size;
  48. char* block_text;
  49. } vtt_block_t;
  50. // VTT files are a collection of REGION, STYLE and CUE blocks.
  51. // XXX: Comments (NOTE blocks) are ignored
  52. typedef struct _vtt_t {
  53. vtt_block_t* region_head;
  54. vtt_block_t* region_tail;
  55. vtt_block_t* style_head;
  56. vtt_block_t* style_tail;
  57. vtt_block_t* cue_head;
  58. vtt_block_t* cue_tail;
  59. } vtt_t;
  60. /*! \brief
  61. \param
  62. */
  63. vtt_t* vtt_new();
  64. /*! \brief
  65. \param
  66. */
  67. void vtt_free(vtt_t* vtt);
  68. /*! \brief
  69. \param
  70. */
  71. vtt_block_t* vtt_block_new(vtt_t* vtt, const utf8_char_t* data, size_t size, enum VTT_BLOCK_TYPE type);
  72. /*! \brief
  73. \param
  74. */
  75. void vtt_cue_free_head(vtt_t* vtt);
  76. /*! \brief
  77. \param
  78. */
  79. void vtt_style_free_head(vtt_t* vtt);
  80. /*! \brief
  81. \param
  82. */
  83. void vtt_region_free_head(vtt_t* vtt);
  84. // returns a vtt_t, containing linked lists of blocks. must be freed when done
  85. /*! \brief
  86. \param
  87. */
  88. vtt_t* vtt_parse(const utf8_char_t* data, size_t size);
  89. /*! \brief
  90. \param
  91. */
  92. vtt_t* _vtt_parse(const utf8_char_t* data, size_t size, int srt_mode);
  93. /*! \brief
  94. \param
  95. */
  96. static inline vtt_block_t* vtt_cue_next(vtt_block_t* block) { return block->next; }
  97. /*! \brief
  98. \param
  99. */
  100. static inline utf8_char_t* vtt_block_data(vtt_block_t* block) { return (utf8_char_t*)(block) + sizeof(vtt_block_t); }
  101. /*! \brief
  102. \param
  103. */
  104. static inline void vtt_crack_time(double tt, int* hh, int* mm, int* ss, int* ms)
  105. {
  106. (*ms) = (int)((int64_t)(tt * 1000) % 1000);
  107. (*ss) = (int)((int64_t)(tt) % 60);
  108. (*mm) = (int)((int64_t)(tt / (60)) % 60);
  109. (*hh) = (int)((int64_t)(tt / (60 * 60)));
  110. }
  111. // This only converts the current CUE, it does not walk the list
  112. /*! \brief
  113. \param
  114. */
  115. int vtt_cue_to_caption_frame(vtt_block_t* cue, caption_frame_t* frame);
  116. // returns the new cue
  117. /*! \brief
  118. \param
  119. */
  120. vtt_block_t* vtt_cue_from_caption_frame(caption_frame_t* frame, vtt_t* vtt);
  121. /*! \brief
  122. \param
  123. */
  124. void vtt_dump(vtt_t* vtt);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif