[FFmpeg-devel] [PATCH 5/5] lavf/srtdec: handle character encodings.

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


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavformat/srtdec.c |   23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)


Note: the other formats that use ff_subtitles_read_chunks should be easy to
convert the same way.


diff --git a/libavformat/srtdec.c b/libavformat/srtdec.c
index dbf1866..ca0a054 100644
--- a/libavformat/srtdec.c
+++ b/libavformat/srtdec.c
@@ -76,32 +76,33 @@ static int64_t get_pts(const char **buf, int *duration,
 static int srt_read_header(AVFormatContext *s)
 {
     SRTContext *srt = s->priv_data;
-    AVBPrint buf;
     AVStream *st = avformat_new_stream(s, NULL);
+    AVTextFile tf = { AV_TEXT_FILE_DEFAULT };
     int res = 0;
+    size_t i;
 
     if (!st)
         return AVERROR(ENOMEM);
     avpriv_set_pts_info(st, 64, 1, 1000);
     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
     st->codec->codec_id   = AV_CODEC_ID_SUBRIP;
+    st->codec->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
 
-    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+    if ((res = ff_subtitles_read_chunks(s, &tf)) < 0)
+        goto end;
+    av_log(s, AV_LOG_VERBOSE, "Detected encoding: %s\n", tf.encoding);
 
-    while (!url_feof(s->pb)) {
-        ff_subtitles_read_chunk(s->pb, &buf);
-
-        if (buf.len) {
-            int64_t pos = avio_tell(s->pb);
+    for (i = 0; i < tf.nb_lines; i++) {
+        /* TODO reindent */
             int64_t pts;
             int duration;
-            const char *ptr = buf.str;
+            const char *ptr = tf.lines[i];
             int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
             AVPacket *sub;
 
             pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
             if (pts != AV_NOPTS_VALUE) {
-                int len = buf.len - (ptr - buf.str);
+                int len = strlen(ptr);
                 if (len <= 0)
                     continue;
                 sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
@@ -109,7 +110,6 @@ static int srt_read_header(AVFormatContext *s)
                     res = AVERROR(ENOMEM);
                     goto end;
                 }
-                sub->pos = pos;
                 sub->pts = pts;
                 sub->duration = duration;
                 if (x1 != -1) {
@@ -122,13 +122,12 @@ static int srt_read_header(AVFormatContext *s)
                     }
                 }
             }
-        }
     }
 
     ff_subtitles_queue_finalize(&srt->q);
 
 end:
-    av_bprint_finalize(&buf, NULL);
+    av_text_file_free(&tf);
     return res;
 }
 
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list