[vlc-devel] [patch] add subtitles autodetect

Savin Alex alexo.veto at gmail.com
Tue Jun 29 20:09:40 CEST 2010


В сообщении от 28 июня 2010 17:59:59 вы написали:
> On Monday 28 June 2010 15:27:23 Savin Alex, you wrote:
> > add subtitles autodetect,
> 
> You can't do that here. stream_ReadLine() is used not only by the subtitle
> parsers. You will confuse everything else.
thanks for answer,
modules/demux/subtitle.c b/modules/demux/subtitle.c static int TextLoad( text_t *txt, stream_t *s ) 
- is it a good place for my patch?

also:
(my previous patch don't touch "UTF-16LE", "UTF-16BE" and "UTF-8" stream, and work only at begin of 
stream - 
is it really broken?)

> > it also required enca library,
> > should work, but I don't know how add enca library to vlc build config,
> > so can't test
> 
> You would need to patch configure.ac
now I read autoconf documenation..., I will send final(tested) version later


diff --git a/modules/demux/subtitle.c b/modules/demux/subtitle.c
index b8021d8..aa73e97 100644
--- a/modules/demux/subtitle.c
+++ b/modules/demux/subtitle.c
@@ -37,6 +37,10 @@
 #include <vlc_memory.h>
 
 #include <ctype.h>
+#include <string.h>
+#include <enca.h>
+#include <iconv.h>
+#include <stdlib.h>
 
 #include <vlc_demux.h>
 #include <vlc_charset.h>
@@ -754,6 +758,16 @@ static int TextLoad( text_t *txt, stream_t *s )
     if( !txt->line )
         return VLC_ENOMEM;
 
+    const int bufferlen=16384;
+    char buffer[bufferlen];
+    int bufferpos=0, bufferposnext=0;
+    char lang[3];
+    if (getenv("LANG")){
+        strncpy(lang, getenv("LANG"), 2);
+        lang[2] = 0;
+    }else
+        lang[0]=0;
+        
     /* load the complete file */
     for( ;; )
     {
@@ -761,7 +775,18 @@ static int TextLoad( text_t *txt, stream_t *s )
 
         if( psz == NULL )
             break;
-
+            
+        if (lang[0] != 0 && bufferpos != bufferlen){
+            if ((bufferposnext+=strlen(psz)) < bufferlen ){
+                strcpy((buffer+bufferpos), psz);
+                bufferpos = bufferposnext;
+            }else{
+                strncpy((buffer+bufferpos), psz, (strlen(psz)-(bufferposnext-bufferlen)));
+                bufferpos=bufferlen;
+                buffer[bufferlen-1] = 0;
+            }
+        }
+        
         txt->line[txt->i_line_count++] = psz;
         if( txt->i_line_count >= i_line_max )
         {
@@ -778,6 +803,34 @@ static int TextLoad( text_t *txt, stream_t *s )
         return VLC_EGENERIC;
     }
 
+    if (lang[0] != 0){
+        EncaAnalyser analyser;
+        EncaEncoding encoding;
+        const char *psz_encoding = NULL;
+        
+        analyser = enca_analyser_alloc((const char *)lang);
+        if (analyser){
+            encoding = enca_analyse(analyser, (unsigned char *)buffer, (size_t)bufferlen);
+            if (encoding.charset != ENCA_CS_UNKNOWN){
+                psz_encoding = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
+                iconv_t icnv = iconv_open("UTF-8", psz_encoding);
+                if (icnv != (iconv_t)-1){
+                    int linelen;
+                    //char **ptr_line = NULL;
+                    for (int i=0; i <= i_line_count; i++){
+                        linelen = strlen(txt->line[i]);
+                        realloc (txt->line[i], linelen * 2 * sizeof( char ) );
+                        if( !txt->line[i] )
+                            return VLC_ENOMEM;
+                        iconv(icnv, &txt->line[i], linelen, &txt->line[i], linelen*2);
+                    }
+                    iconv_close(icnv); 
+                }
+            }
+            enca_analyser_free (analyser);
+        }
+    }
+
     return VLC_SUCCESS;
 }
 static void TextUnload( text_t *txt )

-- 
Best regards, Александр
                        mailto:alexo.veto at gmail.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vlc_sub_enc_autodetect2.patch
Type: text/x-patch
Size: 3824 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20100629/43811a95/attachment.bin>


More information about the vlc-devel mailing list