You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.0KB

  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 "avformat.h"
  24. #include "libavutil/bprint.h"
  25. enum sub_sort {
  26. SUB_SORT_TS_POS = 0, ///< sort by timestamps, then position
  27. SUB_SORT_POS_TS, ///< sort by position, then timestamps
  28. };
  29. typedef struct {
  30. AVPacket *subs; ///< array of subtitles packets
  31. int nb_subs; ///< number of subtitles packets
  32. int allocated_size; ///< allocated size for subs
  33. int current_sub_idx; ///< current position for the read packet callback
  34. enum sub_sort sort; ///< sort method to use when finalizing subtitles
  35. } FFDemuxSubtitlesQueue;
  36. /**
  37. * Insert a new subtitle event.
  38. *
  39. * @param event the subtitle line, may not be zero terminated
  40. * @param len the length of the event (in strlen() sense, so without '\0')
  41. * @param merge set to 1 if the current event should be concatenated with the
  42. * previous one instead of adding a new entry, 0 otherwise
  43. */
  44. AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
  45. const uint8_t *event, int len, int merge);
  46. /**
  47. * Set missing durations and sort subtitles by PTS, and then byte position.
  48. */
  49. void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q);
  50. /**
  51. * Generic read_packet() callback for subtitles demuxers using this queue
  52. * system.
  53. */
  54. int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt);
  55. /**
  56. * Update current_sub_idx to emulate a seek. Except the first parameter, it
  57. * matches AVInputFormat->read_seek2 prototypes.
  58. */
  59. int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
  60. int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
  61. /**
  62. * Remove and destroy all the subtitles packets.
  63. */
  64. void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q);
  65. /**
  66. * SMIL helper to load next chunk ("<...>" or untagged content) in buf.
  67. *
  68. * @param c cached character, to avoid a backward seek
  69. */
  70. int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c);
  71. /**
  72. * SMIL helper to point on the value of an attribute in the given tag.
  73. *
  74. * @param s SMIL tag ("<...>")
  75. * @param attr the attribute to look for
  76. */
  77. const char *ff_smil_get_attr_ptr(const char *s, const char *attr);
  78. /**
  79. * @brief Read a subtitles chunk.
  80. *
  81. * A chunk is defined by a multiline "event", ending with a second line break.
  82. * The trailing line breaks are trimmed. CRLF are supported.
  83. * Example: "foo\r\nbar\r\n\r\nnext" will print "foo\r\nbar" into buf, and pb
  84. * will focus on the 'n' of the "next" string.
  85. *
  86. * @param pb I/O context
  87. * @param buf an initialized buf where the chunk is written
  88. *
  89. * @note buf is cleared before writing into it.
  90. */
  91. void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf);
  92. /**
  93. * Get the number of characters to increment to jump to the next line, or to
  94. * the end of the string.
  95. * The function handles the following line breaks schemes:
  96. * LF, CRLF (MS), or standalone CR (old MacOS).
  97. */
  98. static av_always_inline int ff_subtitles_next_line(const char *ptr)
  99. {
  100. int n = strcspn(ptr, "\r\n");
  101. ptr += n;
  102. if (*ptr == '\r') {
  103. ptr++;
  104. n++;
  105. }
  106. if (*ptr == '\n')
  107. n++;
  108. return n;
  109. }
  110. #endif /* AVFORMAT_SUBTITLES_H */