]> git.sesse.net Git - vlc/blobdiff - modules/codec/stl.c
adjust: stick to single precision
[vlc] / modules / codec / stl.c
index 51138546ed14efa316ba8fdbed5329c0fd13b57a..361e7530523995a2a142d26eb6089bb421345c03 100644 (file)
@@ -6,19 +6,19 @@
  *
  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -33,6 +33,7 @@
 #include <vlc_plugin.h>
 #include <vlc_codec.h>
 #include <vlc_memory.h>
+#include <vlc_charset.h>
 
 /*****************************************************************************
  * Module descriptor
@@ -51,53 +52,57 @@ vlc_module_end()
 /*****************************************************************************
  * Local definitions/prototypes
  *****************************************************************************/
+#define GSI_BLOCK_SIZE 1024
+
+typedef enum {
+    CCT_ISO_6937_2 = 0x3030, CCT_BEGIN = CCT_ISO_6937_2,
+    CCT_ISO_8859_5 = 0x3031,
+    CCT_ISO_8859_6 = 0x3032,
+    CCT_ISO_8859_7 = 0x3033,
+    CCT_ISO_8859_8 = 0x3034, CCT_END = CCT_ISO_8859_8
+} cct_number_value_t;
+
+typedef struct {
+    cct_number_value_t value;
+    const char *str;
+} cct_number_t;
+
 struct decoder_sys_t {
-    int dummy;
+    cct_number_value_t cct;
 };
 
-static char *ParseText(uint8_t *data, int size)
+static cct_number_t cct_nums[] = { {CCT_ISO_6937_2, "ISO_6937-2"},
+                                   {CCT_ISO_8859_5, "ISO_8859-5"},
+                                   {CCT_ISO_8859_6, "ISO_8859-6"},
+                                   {CCT_ISO_8859_7, "ISO_8859-7"},
+                                   {CCT_ISO_8859_8, "ISO_8859-8"} };
+
+
+static char *ParseText(const uint8_t *data, size_t size, const char *charset)
 {
-    char *text = strdup("");
-    int  text_size = 0;
+    char *text = malloc(size);
+    if (text == NULL)
+        return NULL;
 
-    for (int i = 0; i < size; i++) {
+    size_t text_size = 0;
+
+    for (size_t i = 0; i < size; i++) {
         uint8_t code = data[i];
 
         if (code == 0x8f)
             break;
-
-        char tmp[16] = "";
-        char *t = tmp;
-        if (code >= 0x20 && code <= 0x7f)
-            snprintf(tmp, sizeof(tmp), "%c", code);
-#if 0
-        else if (code == 0x80)
-            snprintf(tmp, sizeof(tmp), "<i>");
-        else if (code == 0x81)
-            snprintf(tmp, sizeof(tmp), "</i>");
-        else if (code == 0x82)
-            snprintf(tmp, sizeof(tmp), "<u>");
-        else if (code == 0x83)
-            snprintf(tmp, sizeof(tmp), "</u>");
-#endif
-        else if (code == 0x8a)
-            snprintf(tmp, sizeof(tmp), "\n");
-        else {
-            fprintf(stderr, "--> %2.2x\n", code);
-            t = NULL;
-        }
-
-        if (!t)
-            continue;
-        size_t t_size = strlen(t);
-        text = realloc_or_free(text, t_size + text_size + 1);
-        if (!text)
+        if (code == 0x7f)
             continue;
-        memcpy(&text[text_size], t, t_size);
-        text_size += t_size;
-        text[text_size]   = '\0';
+        /* TODO: italics begin/end 0x80/0x81, underline being/end 0x82/0x83 */
+        if (code & 0x60)
+            text[text_size++] = code;
+        if (code == 0x8a)
+            text[text_size++] = '\n';
     }
-    return text;
+
+    char *u8 = FromCharset(charset, text, text_size);
+    free(text);
+    return u8;
 }
 
 static subpicture_t *Decode(decoder_t *dec, block_t **block)
@@ -117,7 +122,7 @@ static subpicture_t *Decode(decoder_t *dec, block_t **block)
     uint8_t *payload = malloc(payload_size);
     if (!payload)
         goto exit;
-    for (int i = 0; i < b->i_buffer / 128; i++)
+    for (unsigned i = 0; i < b->i_buffer / 128; i++)
         memcpy(&payload[112 * i], &b->p_buffer[128 * i + 16], 112);
 
     sub = decoder_NewSubpicture(dec, NULL);
@@ -138,7 +143,10 @@ static subpicture_t *Decode(decoder_t *dec, block_t **block)
     video_format_Clean(&fmt);
 
     if (sub->p_region) {
-        sub->p_region->psz_text = ParseText(payload, payload_size);
+        sub->p_region->psz_text = ParseText(payload,
+                                            payload_size,
+                                            cct_nums[dec->p_sys->cct - CCT_BEGIN].str);
+        sub->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;
         sub->p_region->psz_html = NULL;
     }
 
@@ -149,6 +157,30 @@ exit:
     return sub;
 }
 
+static int ExtractCCT(const decoder_t *dec, cct_number_value_t *cct_number)
+{
+    uint8_t *header = dec->fmt_in.p_extra;
+    if (!header) {
+        msg_Err(dec, "NULL EBU header (GSI block)\n");
+        return VLC_EGENERIC;
+    }
+
+    if (GSI_BLOCK_SIZE != dec->fmt_in.i_extra) {
+        msg_Err(dec, "EBU header is not in expected size (%d)\n", dec->fmt_in.i_extra);
+        return VLC_EGENERIC;
+    }
+
+    int cct = (header[12] << 8) | header[13];
+    if (CCT_BEGIN > cct || CCT_END < cct) {
+        msg_Err(dec, "EBU header contains illegal CCT (0x%x)\n", cct);
+        return VLC_EGENERIC;
+    }
+
+    *cct_number = cct;
+
+    return VLC_SUCCESS;
+}
+
 static int Open(vlc_object_t *object)
 {
     decoder_t *dec = (decoder_t*)object;
@@ -156,7 +188,18 @@ static int Open(vlc_object_t *object)
     if (dec->fmt_in.i_codec != VLC_CODEC_EBU_STL)
         return VLC_EGENERIC;
 
+    cct_number_value_t cct;
+    int rc = ExtractCCT(dec, &cct);
+    if (VLC_SUCCESS != rc)
+        return rc;
+
+    msg_Dbg(dec, "CCT=0x%x", cct);
+
     decoder_sys_t *sys = malloc(sizeof(*sys));
+    if (!sys)
+        return VLC_ENOMEM;
+
+    sys->cct = cct;
 
     dec->p_sys = sys;
     dec->pf_decode_sub = Decode;
@@ -172,4 +215,3 @@ static void Close(vlc_object_t *object)
 
     free(sys);
 }
-