summaryrefslogtreecommitdiff
path: root/libavformat/subtitles.c
diff options
context:
space:
mode:
authorClément Bœsch <ubitux@gmail.com>2012-12-28 01:15:01 +0100
committerClément Bœsch <ubitux@gmail.com>2012-12-30 22:58:58 +0100
commitd9ac8d296725fef605989b4c9d297fb65c40e2e5 (patch)
treefd6a6663d37da958ffe7484964972ae72ce57672 /libavformat/subtitles.c
parent67286fa98b1ebbf005de784b47852f8d429d03c8 (diff)
lavf: move srtdec:read_chunk() to subtitles utils.
This function can be useful for various other subtitles formats.
Diffstat (limited to 'libavformat/subtitles.c')
-rw-r--r--libavformat/subtitles.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index b264ec5e05..4088cf344d 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -192,3 +192,49 @@ const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
}
return NULL;
}
+
+static inline int is_eol(char c)
+{
+ return c == '\r' || c == '\n';
+}
+
+void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
+{
+ char eol_buf[5];
+ int n = 0, i = 0, nb_eol = 0;
+
+ av_bprint_clear(buf);
+
+ for (;;) {
+ char c = avio_r8(pb);
+
+ if (!c)
+ break;
+
+ /* ignore all initial line breaks */
+ if (n == 0 && is_eol(c))
+ continue;
+
+ /* line break buffering: we don't want to add the trailing \r\n */
+ if (is_eol(c)) {
+ nb_eol += c == '\n';
+ if (nb_eol == 2)
+ break;
+ eol_buf[i++] = c;
+ if (i == sizeof(eol_buf) - 1)
+ break;
+ continue;
+ }
+
+ /* only one line break followed by data: we flush the line breaks
+ * buffer */
+ if (i) {
+ eol_buf[i] = 0;
+ av_bprintf(buf, "%s", eol_buf);
+ i = nb_eol = 0;
+ }
+
+ av_bprint_chars(buf, c, 1);
+ n++;
+ }
+}