stl.c 7.45 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3 4 5 6 7 8
/*****************************************************************************
 * stl.c: EBU STL decoder
 *****************************************************************************
 * Copyright (C) 2010 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * 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
Laurent Aimar's avatar
Laurent Aimar committed
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Laurent Aimar's avatar
Laurent Aimar committed
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * 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.
Laurent Aimar's avatar
Laurent Aimar committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>
#include <vlc_memory.h>
36
#include <vlc_charset.h>
Laurent Aimar's avatar
Laurent Aimar committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

vlc_module_begin()
    set_description(N_("EBU STL subtitles decoder"))
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_SCODEC)
    set_capability("decoder", 10)
    set_callbacks(Open, Close)
vlc_module_end()

/*****************************************************************************
 * Local definitions/prototypes
 *****************************************************************************/
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#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;

Laurent Aimar's avatar
Laurent Aimar committed
70
struct decoder_sys_t {
71
    cct_number_value_t cct;
Laurent Aimar's avatar
Laurent Aimar committed
72 73
};

74 75 76 77 78 79 80
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"} };


81
static text_segment_t *ParseText(const uint8_t *data, size_t size, const char *charset)
Laurent Aimar's avatar
Laurent Aimar committed
82
{
83
    text_style_t *style = NULL;
84 85 86 87
    char *text = malloc(size);
    if (text == NULL)
        return NULL;

88 89 90 91
    text_segment_t *segment = text_segment_New( NULL );
    if (segment == NULL)
        return NULL;

92
    size_t text_size = 0;
Laurent Aimar's avatar
Laurent Aimar committed
93

94
    for (size_t i = 0; i < size; i++) {
Laurent Aimar's avatar
Laurent Aimar committed
95 96 97 98
        uint8_t code = data[i];

        if (code == 0x8f)
            break;
99
        if (code == 0x7f)
Laurent Aimar's avatar
Laurent Aimar committed
100
            continue;
101 102
        if (code & 0x60)
            text[text_size++] = code;
103
        /* italics begin/end 0x80/0x81, underline being/end 0x82/0x83 */
104
        if (code >= 0x80 && code <= 0x85 )
105
        {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            /* Style Change, we do a new segment */
            if( text_size != 0 )
            {
                segment->p_next = ParseText( &data[i], (size - i), charset);
                break;
            }
            else
            {
                style = text_style_Create( STYLE_NO_DEFAULTS );
                if (code == 0x80)
                    style->i_style_flags |= STYLE_ITALIC;
                if (code == 0x81)
                    style->i_style_flags &= STYLE_ITALIC;
                if (code == 0x82)
                    style->i_style_flags |= STYLE_UNDERLINE;
                if (code == 0x83)
                    style->i_style_flags &= STYLE_UNDERLINE;
123 124 125 126
                if (code == 0x84)
                    style->i_style_flags |= STYLE_BACKGROUND;
                if (code == 0x85)
                    style->i_style_flags &= STYLE_BACKGROUND;
127 128
                style->i_features |= STYLE_HAS_FLAGS;
            }
129
        }
130 131
        if (code == 0x8a)
            text[text_size++] = '\n';
Laurent Aimar's avatar
Laurent Aimar committed
132
    }
133

134
    segment->psz_text = FromCharset(charset, text, text_size);
135
    free(text);
136 137 138

    if( style )
        segment->style = style;
139

140
    return segment;
Laurent Aimar's avatar
Laurent Aimar committed
141 142 143 144 145 146 147 148 149
}

static subpicture_t *Decode(decoder_t *dec, block_t **block)
{
    if (block == NULL || *block == NULL)
        return NULL;

    subpicture_t *sub = NULL;

150 151
    block_t *b = *block;
    *block = NULL;
152
    if (b->i_flags & (BLOCK_FLAG_CORRUPTED))
Laurent Aimar's avatar
Laurent Aimar committed
153 154 155 156
        goto exit;
    if (b->i_buffer < 128)
        goto exit;

157 158
    int     payload_size = 0;
    uint8_t *payload = malloc((b->i_buffer / 128) * 112);
Laurent Aimar's avatar
Laurent Aimar committed
159 160
    if (!payload)
        goto exit;
161 162 163

    /* */
    int j = 0;
164
    for (unsigned i = 0; i < b->i_buffer / 128; i++)
165 166 167 168 169 170
    {
        if( b->p_buffer[128 * i + 3] != 0xfe ) {
            memcpy(&payload[112 * j++], &b->p_buffer[128 * i + 16], 112);
            payload_size += 112;
        }
    }
Laurent Aimar's avatar
Laurent Aimar committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    sub = decoder_NewSubpicture(dec, NULL);
    if (!sub) {
        free(payload);
        goto exit;
    }
    sub->i_start    = b->i_pts;
    sub->i_stop     = b->i_pts + b->i_length;
    sub->b_ephemer  = b->i_length == 0;
    sub->b_absolute = false;
    //sub->i_original_picture_width  = 0;
    //sub->i_original_picture_height = 0;

    video_format_t fmt;
    video_format_Init(&fmt, VLC_CODEC_TEXT);
    sub->p_region = subpicture_region_New(&fmt);
    video_format_Clean(&fmt);

    if (sub->p_region) {
190 191 192
        sub->p_region->p_text = ParseText(payload,
                                         payload_size,
                                         cct_nums[dec->p_sys->cct - CCT_BEGIN].str);
193
        sub->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;
Laurent Aimar's avatar
Laurent Aimar committed
194 195 196 197 198 199 200 201 202
    }

    free(payload);

exit:
    block_Release(b);
    return sub;
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
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;
}

Laurent Aimar's avatar
Laurent Aimar committed
227 228 229 230 231 232 233
static int Open(vlc_object_t *object)
{
    decoder_t *dec = (decoder_t*)object;

    if (dec->fmt_in.i_codec != VLC_CODEC_EBU_STL)
        return VLC_EGENERIC;

234 235 236 237 238 239 240
    cct_number_value_t cct;
    int rc = ExtractCCT(dec, &cct);
    if (VLC_SUCCESS != rc)
        return rc;

    msg_Dbg(dec, "CCT=0x%x", cct);

Laurent Aimar's avatar
Laurent Aimar committed
241
    decoder_sys_t *sys = malloc(sizeof(*sys));
242 243 244 245
    if (!sys)
        return VLC_ENOMEM;

    sys->cct = cct;
Laurent Aimar's avatar
Laurent Aimar committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

    dec->p_sys = sys;
    dec->pf_decode_sub = Decode;
    dec->fmt_out.i_cat = SPU_ES;
    dec->fmt_out.i_codec = 0;
    return VLC_SUCCESS;
}

static void Close(vlc_object_t *object)
{
    decoder_t *dec = (decoder_t*)object;
    decoder_sys_t *sys = dec->p_sys;

    free(sys);
}