[FFmpeg-devel] [PATCH 2/4] avcodec/samidec: use ff_htmlmarkup_to_ass()

Yayoi yayoi.ukai at gmail.com
Mon Aug 10 06:39:31 CEST 2015


---
 libavcodec/Makefile     |  2 +-
 libavcodec/samidec.c    | 71 ++++++++++++++++++++++++++++++++-----------------
 tests/ref/fate/sub-sami | 18 ++++++-------
 3 files changed, 56 insertions(+), 35 deletions(-)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 4ca824b..8201aa0 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -447,7 +447,7 @@ OBJS-$(CONFIG_RV20_DECODER)            += rv10.o
 OBJS-$(CONFIG_RV20_ENCODER)            += rv20enc.o
 OBJS-$(CONFIG_RV30_DECODER)            += rv30.o rv34.o rv30dsp.o
 OBJS-$(CONFIG_RV40_DECODER)            += rv40.o rv34.o rv40dsp.o
-OBJS-$(CONFIG_SAMI_DECODER)            += samidec.o ass.o
+OBJS-$(CONFIG_SAMI_DECODER)            += samidec.o ass.o htmlsubtitles.o
 OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
 OBJS-$(CONFIG_S302M_ENCODER)           += s302menc.o
 OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
diff --git a/libavcodec/samidec.c b/libavcodec/samidec.c
index 47850e2..6ed1755 100644
--- a/libavcodec/samidec.c
+++ b/libavcodec/samidec.c
@@ -27,6 +27,7 @@
 #include "ass.h"
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
+#include "htmlsubtitles.h"
 
 typedef struct {
     AVBPrint source;
@@ -41,14 +42,15 @@ static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
     char *tag = NULL;
     char *dupsrc = av_strdup(src);
     char *p = dupsrc;
+    char *pcopy = NULL;
+    int index = 0;
+    int second_paragraph = 0;
 
-    av_bprint_clear(&sami->content);
     for (;;) {
         char *saveptr = NULL;
-        int prev_chr_is_space = 0;
         AVBPrint *dst = &sami->content;
 
-        /* parse & extract paragraph tag */
+        /* extract paragraph tag */
         p = av_stristr(p, "<P");
         if (!p)
             break;
@@ -77,37 +79,56 @@ static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
             goto end;
         }
 
-        /* extract the text, stripping most of the tags */
+        /* check for the additional paragraph
+        *  if there are additional paragraph
+        *  save next paragraph content
+        *  for additional iteration
+        */
+        pcopy = av_strdup(p);
         while (*p) {
             if (*p == '<') {
-                if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
+                if (!av_strncasecmp(p, "<p", 2) && (p[2] == '>' || av_isspace(p[2]))) {
+                    second_paragraph = 1;
                     break;
-                if (!av_strncasecmp(p, "<BR", 3))
-                    av_bprintf(dst, "\\N");
-                p++;
-                while (*p && *p != '>')
-                    p++;
-                if (!*p)
-                    break;
-                if (*p == '>')
-                    p++;
-                continue;
+                }
             }
-            if (!av_isspace(*p))
-                av_bprint_chars(dst, *p, 1);
-            else if (!prev_chr_is_space)
-                av_bprint_chars(dst, ' ', 1);
-            prev_chr_is_space = av_isspace(*p);
             p++;
+            index++;
+        }
+        /* if there are additional paragraph
+        *  remove the additional paragraph contents
+        */
+        p = p - index;
+        if (second_paragraph) {
+            p[index] = 0;
         }
-    }
 
-    av_bprint_clear(&sami->full);
-    if (sami->source.len)
-        av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->source.str);
-    av_bprintf(&sami->full, "%s", sami->content.str);
+        ff_htmlmarkup_to_ass(avctx, dst, p);
+
+        /* add the source if there are any. */
+        av_bprint_clear(&sami->full);
+        if (sami->source.len) {
+            av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->source.str);
+            av_bprintf(&sami->full, "%s", sami->content.str);
+            /* add back stored additional paragraph content.
+            * if there are any and run additional iteration.
+            */
+            if (second_paragraph) {
+                second_paragraph = 0;
+                p = pcopy;
+                p += index;
+                index = 0;
+                continue;
+            }
+        } else {
+            av_bprintf(&sami->full, "%s", sami->content.str);
+        }
+        av_bprint_clear(&sami->content);
+
+    }
 
 end:
+    av_free(pcopy);
     av_free(dupsrc);
     return ret;
 }
diff --git a/tests/ref/fate/sub-sami b/tests/ref/fate/sub-sami
index caa85a2..9eabbd7 100644
--- a/tests/ref/fate/sub-sami
+++ b/tests/ref/fate/sub-sami
@@ -10,12 +10,12 @@ Style: Default,Arial,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,2,10
 
 [Events]
 Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
-Dialogue: 0,0:00:00.00,0:00:00.01,Default,,0,0,0,,{\i1}Pres. John F. Kennedy {\i0}\N
-Dialogue: 0,0:00:00.01,0:00:08.80,Default,,0,0,0,,{\i1}Pres. John F. Kennedy {\i0}\NLet the word go forth, from this time and place to friend and foe alike that the torch 
-Dialogue: 0,0:00:08.80,0:00:19.50,Default,,0,0,0,,{\i1}Pres. John F. Kennedy {\i0}\Nhas been passed to a new generation of Americans, born in this century, tempered by war, 
-Dialogue: 0,0:00:19.50,0:00:28.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy {\i0}\Ndisciplined by a hard and bitter peace, proud of our ancient heritage, and unwilling to witness 
-Dialogue: 0,0:00:28.00,0:00:38.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy {\i0}\Nor permit the slow undoing of those human rights to which this nation has always 
-Dialogue: 0,0:00:38.00,0:00:46.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy {\i0}\Nbeen committed and to which we are committed today at home and around the world. 
-Dialogue: 0,0:00:46.00,0:01:01.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy {\i0}\NLet every nation know, whether it wishes us well or ill, that we shall pay any price, bear any burden, 
-Dialogue: 0,0:01:01.00,0:01:13.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy {\i0}\Nmeet any hardship, support any friend, oppose any foe, to ensure the survival and success of liberty. 
-Dialogue: 0,0:01:13.00,9:59:59.99,Default,,0,0,0,,{\i1}End of: {\i0}\NPresident John F. Kennedy Speech 
+Dialogue: 0,0:00:00.00,0:00:00.01,Default,,0,0,0,,{\i1}Pres. John F. Kennedy{\i0}\N
+Dialogue: 0,0:00:00.01,0:00:08.80,Default,,0,0,0,,{\i1}Pres. John F. Kennedy{\i0}\NLet the word go forth,\Nfrom this time and place to friend and foe\Nalike that the torch
+Dialogue: 0,0:00:08.80,0:00:19.50,Default,,0,0,0,,{\i1}Pres. John F. Kennedy{\i0}\Nhas been passed to a new generation of Americans,\Nborn in this century, tempered by war,
+Dialogue: 0,0:00:19.50,0:00:28.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy{\i0}\Ndisciplined by a hard and bitter peace,\Nproud of our ancient heritage, and unwilling to witness
+Dialogue: 0,0:00:28.00,0:00:38.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy{\i0}\Nor permit the slow undoing of those human rights\Nto which this nation has always
+Dialogue: 0,0:00:38.00,0:00:46.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy{\i0}\Nbeen committed and to which we are\Ncommitted today at home and around the world.
+Dialogue: 0,0:00:46.00,0:01:01.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy{\i0}\NLet every nation know,\Nwhether it wishes us well or ill,\Nthat we shall pay any price, bear any burden,
+Dialogue: 0,0:01:01.00,0:01:13.00,Default,,0,0,0,,{\i1}Pres. John F. Kennedy{\i0}\Nmeet any hardship, support any friend,\Noppose any foe, to ensure the survival and\Nsuccess of liberty.
+Dialogue: 0,0:01:13.00,9:59:59.99,Default,,0,0,0,,{\i1}End of:{\i0}\NPresident John F. Kennedy Speech
-- 
1.8.5.2 (Apple Git-48)



More information about the ffmpeg-devel mailing list