subtitles.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2012 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVFORMAT_SUBTITLES_H
  21. #define AVFORMAT_SUBTITLES_H
  22. #include <stdint.h>
  23. #include <stddef.h>
  24. #include "avformat.h"
  25. #include "libavutil/bprint.h"
  26. enum sub_sort {
  27. SUB_SORT_TS_POS = 0, ///< sort by timestamps, then position
  28. SUB_SORT_POS_TS, ///< sort by position, then timestamps
  29. };
  30. enum ff_utf_type {
  31. FF_UTF_8, // or other 8 bit encodings
  32. FF_UTF16LE,
  33. FF_UTF16BE,
  34. };
  35. typedef struct {
  36. int type;
  37. AVIOContext *pb;
  38. unsigned char buf[8];
  39. int buf_pos, buf_len;
  40. AVIOContext buf_pb;
  41. } FFTextReader;
  42. /**
  43. * Initialize the FFTextReader from the given AVIOContext. This function will
  44. * read some bytes from pb, and test for UTF-8 or UTF-16 BOMs. Further accesses
  45. * to FFTextReader will read more data from pb.
  46. * If s is not NULL, the user will be warned if a UTF-16 conversion takes place.
  47. *
  48. * The purpose of FFTextReader is to transparently convert read data to UTF-8
  49. * if the stream had a UTF-16 BOM.
  50. *
  51. * @param s Pointer to provide av_log context
  52. * @param r object which will be initialized
  53. * @param pb stream to read from (referenced as long as FFTextReader is in use)
  54. */
  55. void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb);
  56. /**
  57. * Similar to ff_text_init_avio(), but sets it up to read from a bounded buffer.
  58. *
  59. * @param r object which will be initialized
  60. * @param buf buffer to read from (referenced as long as FFTextReader is in use)
  61. * @param size size of buf
  62. */
  63. void ff_text_init_buf(FFTextReader *r, void *buf, size_t size);
  64. /**
  65. * Return the byte position of the next byte returned by ff_text_r8(). For
  66. * UTF-16 source streams, this will return the original position, but it will
  67. * be incorrect if a codepoint was only partially read with ff_text_r8().
  68. */
  69. int64_t ff_text_pos(FFTextReader *r);
  70. /**
  71. * Return the next byte. The return value is always 0 - 255. Returns 0 on EOF.
  72. * If the source stream is UTF-16, this reads from the stream converted to
  73. * UTF-8. On invalid UTF-16, 0 is returned.
  74. */
  75. int ff_text_r8(FFTextReader *r);
  76. /**
  77. * Return non-zero if EOF was reached.
  78. */
  79. int ff_text_eof(FFTextReader *r);
  80. /**
  81. * Like ff_text_r8(), but don't remove the byte from the buffer.
  82. */
  83. int ff_text_peek_r8(FFTextReader *r);
  84. /**
  85. * Read the given number of bytes (in UTF-8). On error or EOF, \0 bytes are
  86. * written.
  87. */
  88. void ff_text_read(FFTextReader *r, char *buf, size_t size);
  89. typedef struct {
  90. AVPacket *subs; ///< array of subtitles packets
  91. int nb_subs; ///< number of subtitles packets
  92. int allocated_size; ///< allocated size for subs
  93. int current_sub_idx; ///< current position for the read packet callback
  94. enum sub_sort sort; ///< sort method to use when finalizing subtitles
  95. int keep_duplicates; ///< set to 1 to keep duplicated subtitle events
  96. } FFDemuxSubtitlesQueue;
  97. /**
  98. * Insert a new subtitle event.
  99. *
  100. * @param event the subtitle line, may not be zero terminated
  101. * @param len the length of the event (in strlen() sense, so without '\0')
  102. * @param merge set to 1 if the current event should be concatenated with the
  103. * previous one instead of adding a new entry, 0 otherwise
  104. */
  105. AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
  106. const uint8_t *event, size_t len, int merge);
  107. /**
  108. * Set missing durations, sort subtitles by PTS (and then byte position), and
  109. * drop duplicated events.
  110. */
  111. void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q);
  112. /**
  113. * Generic read_packet() callback for subtitles demuxers using this queue
  114. * system.
  115. */
  116. int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt);
  117. /**
  118. * Update current_sub_idx to emulate a seek. Except the first parameter, it
  119. * matches AVInputFormat->read_seek2 prototypes.
  120. */
  121. int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
  122. int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
  123. /**
  124. * Remove and destroy all the subtitles packets.
  125. */
  126. void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q);
  127. /**
  128. * SMIL helper to load next chunk ("<...>" or untagged content) in buf.
  129. *
  130. * @param c cached character, to avoid a backward seek
  131. */
  132. int ff_smil_extract_next_text_chunk(FFTextReader *tr, AVBPrint *buf, char *c);
  133. /**
  134. * SMIL helper to point on the value of an attribute in the given tag.
  135. *
  136. * @param s SMIL tag ("<...>")
  137. * @param attr the attribute to look for
  138. */
  139. const char *ff_smil_get_attr_ptr(const char *s, const char *attr);
  140. /**
  141. * @brief Same as ff_subtitles_read_text_chunk(), but read from an AVIOContext.
  142. */
  143. void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf);
  144. /**
  145. * @brief Read a subtitles chunk from FFTextReader.
  146. *
  147. * A chunk is defined by a multiline "event", ending with a second line break.
  148. * The trailing line breaks are trimmed. CRLF are supported.
  149. * Example: "foo\r\nbar\r\n\r\nnext" will print "foo\r\nbar" into buf, and pb
  150. * will focus on the 'n' of the "next" string.
  151. *
  152. * @param tr I/O context
  153. * @param buf an initialized buf where the chunk is written
  154. *
  155. * @note buf is cleared before writing into it.
  156. */
  157. void ff_subtitles_read_text_chunk(FFTextReader *tr, AVBPrint *buf);
  158. /**
  159. * Get the number of characters to increment to jump to the next line, or to
  160. * the end of the string.
  161. * The function handles the following line breaks schemes:
  162. * LF, CRLF (MS), or standalone CR (old MacOS).
  163. */
  164. static av_always_inline int ff_subtitles_next_line(const char *ptr)
  165. {
  166. int n = strcspn(ptr, "\r\n");
  167. ptr += n;
  168. while (*ptr == '\r') {
  169. ptr++;
  170. n++;
  171. }
  172. if (*ptr == '\n')
  173. n++;
  174. return n;
  175. }
  176. /**
  177. * Read a line of text. Discards line ending characters.
  178. * The function handles the following line breaks schemes:
  179. * LF, CRLF (MS), or standalone CR (old MacOS).
  180. *
  181. * Returns the number of bytes written to buf. Always writes a terminating 0,
  182. * similar as with snprintf.
  183. *
  184. * @note returns a negative error code if a \0 byte is found
  185. */
  186. ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size);
  187. #endif /* AVFORMAT_SUBTITLES_H */