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

Yayoi yayoi.ukai at gmail.com
Sat Aug 8 08:03:29 CEST 2015


---
 libavcodec/samidec.c    | 59 +++++++++++++++++++++++++++++--------------------
 tests/ref/fate/sub-sami | 18 +++++++--------
 2 files changed, 44 insertions(+), 33 deletions(-)

diff --git a/libavcodec/samidec.c b/libavcodec/samidec.c
index 47850e2..41826a7 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,11 +42,12 @@ 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 */
@@ -77,37 +79,46 @@ static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
             goto end;
         }
 
-        /* extract the text, stripping most of the tags */
+        /* check for the second paragrph */
+        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++;
+        }
+        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);
+            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..3c73526 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