[FFmpeg-devel] [PATCH 4/5] lavf/subtitles: subtitles helpers with character encoding support.

Nicolas George nicolas.george at normalesup.org
Thu Aug 8 17:03:34 CEST 2013


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavformat/subtitles.c |   31 +++++++++++++++++++++++++++++++
 libavformat/subtitles.h |   10 ++++++++++
 2 files changed, 41 insertions(+)

diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index 2af0450..f268ffa 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -229,3 +229,34 @@ void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
         n++;
     }
 }
+
+int ff_subtitles_read_chunks(AVFormatContext *avf, AVTextFile *tf)
+{
+    int ret;
+    size_t i, j = 0;
+    char *tail = NULL;
+
+    tf->flags |= AV_TEXT_FLAG_REMOVE_CR | AV_TEXT_FLAG_SPLIT_LINES;
+    if ((ret = av_text_file_read_pb(tf, avf->pb)) < 0) {
+        av_log(avf, AV_LOG_ERROR, "%s\n", tf->error);
+        return ret;
+    }
+    for (i = 0; i < tf->nb_lines; i++) {
+        if (*tf->lines[i]) {
+            if (tail)
+                *tail = '\n';
+            else
+                tf->lines[j] = tf->lines[i];
+            tail = tf->lines[i] + strlen(tf->lines[i]);
+        } else {
+            if (tail)
+                j++;
+            tail = NULL;
+        }
+    }
+    if (tail)
+        j++;
+    tf->nb_lines = j;
+    tf->lines[j] = 0;
+    return 0;
+}
diff --git a/libavformat/subtitles.h b/libavformat/subtitles.h
index 455b374..58128ec 100644
--- a/libavformat/subtitles.h
+++ b/libavformat/subtitles.h
@@ -24,6 +24,7 @@
 #include <stdint.h>
 #include "avformat.h"
 #include "libavutil/bprint.h"
+#include "libavutil/text_file.h"
 
 typedef struct {
     AVPacket *subs;         ///< array of subtitles packets
@@ -96,4 +97,13 @@ const char *ff_smil_get_attr_ptr(const char *s, const char *attr);
  */
 void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf);
 
+/**
+ * Read a text file into subtitles chunks.
+ *
+ * @param avf  format context
+ * @param tf   text file structure; the lines field will be used to return
+ *             the chunks
+ */
+int ff_subtitles_read_chunks(AVFormatContext *avf, AVTextFile *tf);
+
 #endif /* AVFORMAT_SUBTITLES_H */
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list