stl.c 15.2 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

François Cartegnie's avatar
François Cartegnie committed
38 39
#include "substext.h" /* required for font scaling / updater */

Laurent Aimar's avatar
Laurent Aimar committed
40 41 42 43 44 45 46 47 48 49
/*****************************************************************************
 * 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)
50
    set_capability("spu decoder", 10)
Laurent Aimar's avatar
Laurent Aimar committed
51 52 53 54 55 56
    set_callbacks(Open, Close)
vlc_module_end()

/*****************************************************************************
 * Local definitions/prototypes
 *****************************************************************************/
57 58
#define GSI_BLOCK_SIZE 1024

François Cartegnie's avatar
François Cartegnie committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#define STL_GROUPS_MAX 255

#define STL_TEXTFIELD_SIZE     112
#define STL_TTI_HEADER_SIZE    16
#define STL_TTI_SIZE           (STL_TTI_HEADER_SIZE + STL_TEXTFIELD_SIZE)

#define STL_TF_TELETEXT_FIRST     0x00
#define STL_TF_TELETEXT_LAST      0x1f
#define STL_TF_CHARCODE1_FIRST    0x20
#define STL_TF_CHARCODE1_LAST     0x7f
#define STL_TF_ITALICS_ON         0x80
#define STL_TF_ITALICS_OFF        0x81
#define STL_TF_UNDERLINE_ON       0x82
#define STL_TF_UNDERLINE_OFF      0x83
#define STL_TF_BOXING_ON          0x84
#define STL_TF_BOXING_OFF         0x85
#define STL_TF_LINEBREAK          0x8a
#define STL_TF_END_FILL           0x8f
#define STL_TF_CHARCODE2_FIRST    0xa1

79 80 81 82 83 84 85 86
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;

François Cartegnie's avatar
François Cartegnie committed
87 88 89 90 91 92 93 94 95 96 97
typedef struct
{
    uint8_t i_accumulating;
    uint8_t i_justify;
    int64_t i_start;
    int64_t i_end;
    text_style_t *p_style;
    text_segment_t *p_segment;
    text_segment_t **pp_segment_last;
} stl_sg_t;

98 99 100 101 102
typedef struct {
    cct_number_value_t value;
    const char *str;
} cct_number_t;

Laurent Aimar's avatar
Laurent Aimar committed
103
struct decoder_sys_t {
François Cartegnie's avatar
François Cartegnie committed
104
    stl_sg_t groups[STL_GROUPS_MAX + 1];
105
    cct_number_value_t cct;
106
    uint8_t i_fps;
Laurent Aimar's avatar
Laurent Aimar committed
107 108
};

109 110 111 112 113 114
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"} };

François Cartegnie's avatar
François Cartegnie committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128
static text_style_t * CreateGroupStyle()
{
    text_style_t *p_style = text_style_Create(STYLE_NO_DEFAULTS);
    if(p_style)
    {
        p_style->i_features = STYLE_HAS_FLAGS|STYLE_HAS_BACKGROUND_ALPHA|STYLE_HAS_BACKGROUND_COLOR;
        /* Teletext needs default background to black */
        p_style->i_background_alpha = STYLE_ALPHA_OPAQUE;
        p_style->i_background_color = 0x000000;
        p_style->i_font_size = 0;
        p_style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE;
    }
    return p_style;
}
129

François Cartegnie's avatar
François Cartegnie committed
130 131
static void TextBufferFlush(stl_sg_t *p_group, uint8_t *p_buf, uint8_t *pi_buf,
                            const char *psz_charset)
Laurent Aimar's avatar
Laurent Aimar committed
132
{
François Cartegnie's avatar
François Cartegnie committed
133 134
    if(*pi_buf == 0)
        return;
135

François Cartegnie's avatar
François Cartegnie committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    char *psz_utf8 = FromCharset(psz_charset, p_buf, *pi_buf);
    if(psz_utf8)
    {
        *p_group->pp_segment_last = text_segment_New(psz_utf8);
        if(*p_group->pp_segment_last)
        {
            if(p_group->p_style)
                (*p_group->pp_segment_last)->style = text_style_Duplicate(p_group->p_style);
            p_group->pp_segment_last = &((*p_group->pp_segment_last)->p_next);
        }
        free(psz_utf8);
    }

    *pi_buf = 0;
}
151

François Cartegnie's avatar
François Cartegnie committed
152 153 154 155 156
static void GroupParseTeletext(stl_sg_t *p_group, uint8_t code)
{
    if(p_group->p_style == NULL &&
      !(p_group->p_style = CreateGroupStyle()))
        return;
Laurent Aimar's avatar
Laurent Aimar committed
157

François Cartegnie's avatar
François Cartegnie committed
158 159
    /* See ETS 300 706 Table 26 as EBU 3264 does only name values
       and does not explain at all */
Laurent Aimar's avatar
Laurent Aimar committed
160

161
    static const uint32_t colors[] =
François Cartegnie's avatar
François Cartegnie committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    {
        0x000000,
        0xFF0000,
        0x00FF00,
        0xFFFF00,
        0x0000FF,
        0xFF00FF,
        0x00FFFF,
        0xFFFFFF,
    };

    /* Teletext data received, so we need to enable background */
    p_group->p_style->i_style_flags |= STYLE_BACKGROUND;

    switch(code)
    {
        case 0x0c:
            p_group->p_style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE;
            p_group->p_style->i_style_flags &= ~(STYLE_DOUBLEWIDTH|STYLE_HALFWIDTH);
Laurent Aimar's avatar
Laurent Aimar committed
181
            break;
François Cartegnie's avatar
François Cartegnie committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

        case 0x0d: /* double height */
            p_group->p_style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE * 2;
            p_group->p_style->i_style_flags &= ~STYLE_DOUBLEWIDTH;
            p_group->p_style->i_style_flags |= STYLE_HALFWIDTH;
            break;

        case 0x0e: /* double width */
            p_group->p_style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE;
            p_group->p_style->i_style_flags &= ~STYLE_HALFWIDTH;
            p_group->p_style->i_style_flags |= STYLE_DOUBLEWIDTH;
            break;

        case 0x0f: /* double size */
            p_group->p_style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE * 2;
            p_group->p_style->i_style_flags &= ~(STYLE_DOUBLEWIDTH|STYLE_HALFWIDTH);
            break;

        case 0x1d:
            p_group->p_style->i_background_color = p_group->p_style->i_font_color;
            p_group->p_style->i_features &= ~STYLE_HAS_FONT_COLOR;
            break;

        case 0x1c:
            p_group->p_style->i_background_color = colors[0];
            break;

        default:
            if(code < 8)
211
            {
François Cartegnie's avatar
François Cartegnie committed
212 213
                p_group->p_style->i_font_color = colors[code];
                p_group->p_style->i_features |= STYLE_HAS_FONT_COLOR;
214
            }
François Cartegnie's avatar
François Cartegnie committed
215 216 217

            /* Need to handle Mosaic ? Really ? */
            break;
Laurent Aimar's avatar
Laurent Aimar committed
218
    }
219

François Cartegnie's avatar
François Cartegnie committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

static void GroupApplyStyle(stl_sg_t *p_group, uint8_t code)
{
    if(p_group->p_style == NULL &&
      !(p_group->p_style = CreateGroupStyle()))
        return;

    switch(code)
    {
        case STL_TF_ITALICS_ON:
            p_group->p_style->i_style_flags |= STYLE_ITALIC;
            break;
        case STL_TF_ITALICS_OFF:
            p_group->p_style->i_style_flags &= ~STYLE_ITALIC;
            break;
        case STL_TF_UNDERLINE_ON:
            p_group->p_style->i_style_flags |= STYLE_UNDERLINE;
            break;
        case STL_TF_UNDERLINE_OFF:
            p_group->p_style->i_style_flags &= ~STYLE_UNDERLINE;
            break;
        case STL_TF_BOXING_ON:
        case STL_TF_BOXING_OFF:
        default:
            break;
    }
}
248

François Cartegnie's avatar
François Cartegnie committed
249 250 251 252 253 254 255
static int64_t ParseTimeCode(const uint8_t *data, double fps)
{
    return INT64_C(1000000) * (data[0] * 3600 +
                               data[1] *   60 +
                               data[2] *    1 +
                               data[3] /  fps);
}
256

François Cartegnie's avatar
François Cartegnie committed
257 258 259 260 261 262 263 264 265
static void ClearTeletextStyles(stl_sg_t *p_group)
{
    if(p_group->p_style)
    {
        p_group->p_style->i_features &= ~STYLE_HAS_FONT_COLOR;
        p_group->p_style->i_background_color = 0x000000;
        p_group->p_style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE;
        p_group->p_style->i_style_flags &= ~(STYLE_DOUBLEWIDTH|STYLE_HALFWIDTH);
    }
Laurent Aimar's avatar
Laurent Aimar committed
266 267
}

François Cartegnie's avatar
François Cartegnie committed
268
/* Returns true if group is we need to output group */
269
static bool ParseTTI(stl_sg_t *p_group, const uint8_t *p_data, const char *psz_charset, double fps)
Laurent Aimar's avatar
Laurent Aimar committed
270
{
François Cartegnie's avatar
François Cartegnie committed
271 272
    uint8_t p_buffer[STL_TEXTFIELD_SIZE];
    uint8_t i_buffer = 0;
Laurent Aimar's avatar
Laurent Aimar committed
273

François Cartegnie's avatar
François Cartegnie committed
274 275 276 277
    /* Header */
    uint8_t ebn = p_data[3];
    if(ebn > 0xef && ebn != 0xff)
        return false;
Laurent Aimar's avatar
Laurent Aimar committed
278

François Cartegnie's avatar
François Cartegnie committed
279 280
    if(p_data[15] != 0x00) /* comment flag */
        return false;
Laurent Aimar's avatar
Laurent Aimar committed
281

François Cartegnie's avatar
François Cartegnie committed
282 283
    if(p_data[14] > 0x00)
        p_group->i_justify = p_data[14];
284

François Cartegnie's avatar
François Cartegnie committed
285 286 287 288
    /* Accumulating started or continuing.
     * We must not flush current segments on output and continue on next block */
    p_group->i_accumulating = (p_data[4] == 0x01 || p_data[4] == 0x02);

289 290
    p_group->i_start = ParseTimeCode( &p_data[5], fps );
    p_group->i_end = ParseTimeCode( &p_data[9], fps );
François Cartegnie's avatar
François Cartegnie committed
291 292 293

    /* Text Field */
    for (size_t i = STL_TTI_HEADER_SIZE; i < STL_TTI_SIZE; i++)
294
    {
François Cartegnie's avatar
François Cartegnie committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        const uint8_t code = p_data[i];
        switch(code)
        {
            case STL_TF_LINEBREAK:
                p_buffer[i_buffer++] = '\n';
                TextBufferFlush(p_group, p_buffer, &i_buffer, psz_charset);
                /* Clear teletext styles on each new row */
                ClearTeletextStyles(p_group);
                break;

            case STL_TF_END_FILL:
                TextBufferFlush(p_group, p_buffer, &i_buffer, psz_charset);
                ClearTeletextStyles(p_group);
                return true;

            default:
                if(code <= STL_TF_TELETEXT_LAST)
                {
                    TextBufferFlush(p_group, p_buffer, &i_buffer, psz_charset);
                    GroupParseTeletext(p_group, code);
                }
                else if((code >= STL_TF_CHARCODE1_FIRST && code <= STL_TF_CHARCODE1_LAST) ||
                    code >= STL_TF_CHARCODE2_FIRST)
                {
                    p_buffer[i_buffer++] = code;
                }
                else if(code >= STL_TF_ITALICS_ON && code <= STL_TF_BOXING_OFF)
                {
                    TextBufferFlush(p_group, p_buffer, &i_buffer, psz_charset);
                    GroupApplyStyle(p_group, code);
                }
                break;
327 328
        }
    }
Laurent Aimar's avatar
Laurent Aimar committed
329

François Cartegnie's avatar
François Cartegnie committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    TextBufferFlush(p_group, p_buffer, &i_buffer, psz_charset);

    return false;
}

static void FillSubpictureUpdater(stl_sg_t *p_group, subpicture_updater_sys_t *p_spu_sys)
{
    if(p_group->i_accumulating)
    {
        p_spu_sys->region.p_segments = text_segment_Copy(p_group->p_segment);
    }
    else
    {
        p_spu_sys->region.p_segments = p_group->p_segment;
        p_group->p_segment = NULL;
        p_group->pp_segment_last = &p_group->p_segment;
    }

    p_spu_sys->region.align = SUBPICTURE_ALIGN_BOTTOM;
    if(p_group->i_justify == 0x01)
        p_spu_sys->region.inner_align = SUBPICTURE_ALIGN_LEFT;
    else if(p_group->i_justify == 0x03)
        p_spu_sys->region.inner_align = SUBPICTURE_ALIGN_RIGHT;
}

static void ResetGroups(decoder_sys_t *p_sys)
{
    for(size_t i=0; i<=STL_GROUPS_MAX; i++)
    {
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        stl_sg_t *p_group = &p_sys->groups[i];
        if(p_group->p_segment)
        {
            text_segment_ChainDelete(p_group->p_segment);
            p_group->p_segment = NULL;
            p_group->pp_segment_last = &p_group->p_segment;
        }

        if(p_group->p_style)
        {
            text_style_Delete(p_group->p_style);
            p_group->p_style = NULL;
        }

        p_group->i_accumulating = false;
        p_group->i_end = 0;
        p_group->i_start = 0;
        p_group->i_justify = 0;
Laurent Aimar's avatar
Laurent Aimar committed
377
    }
François Cartegnie's avatar
François Cartegnie committed
378 379
}

380
static int Decode(decoder_t *p_dec, block_t *p_block)
François Cartegnie's avatar
François Cartegnie committed
381
{
382 383
    if (p_block == NULL) /* No Drain */
        return VLCDEC_SUCCESS;
François Cartegnie's avatar
François Cartegnie committed
384 385 386 387 388 389 390 391 392 393 394

    if(p_block->i_buffer < STL_TTI_SIZE)
        p_block->i_flags |= BLOCK_FLAG_CORRUPTED;

    if(p_block->i_flags & (BLOCK_FLAG_CORRUPTED|BLOCK_FLAG_DISCONTINUITY))
    {
        ResetGroups(p_dec->p_sys);

        if(p_block->i_flags & BLOCK_FLAG_CORRUPTED)
        {
            block_Release(p_block);
395
            return VLCDEC_SUCCESS;
François Cartegnie's avatar
François Cartegnie committed
396
        }
Laurent Aimar's avatar
Laurent Aimar committed
397 398
    }

François Cartegnie's avatar
François Cartegnie committed
399 400 401 402
    const char *psz_charset = cct_nums[p_dec->p_sys->cct - CCT_BEGIN].str;
    for (size_t i = 0; i < p_block->i_buffer / STL_TTI_SIZE; i++)
    {
        stl_sg_t *p_group = &p_dec->p_sys->groups[p_block->p_buffer[0]];
403
        if(ParseTTI(p_group, &p_block->p_buffer[i * STL_TTI_SIZE], psz_charset, p_dec->p_sys->i_fps) &&
François Cartegnie's avatar
François Cartegnie committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
           p_group->p_segment != NULL )
        {
            /* output */
            subpicture_t *p_sub = decoder_NewSubpictureText(p_dec);
            if( p_sub )
            {
                FillSubpictureUpdater(p_group, p_sub->updater.p_sys );

                p_sub->b_absolute = false;

                if(p_group->i_end && p_group->i_start >= p_block->i_dts)
                {
                    p_sub->i_start = VLC_TS_0 + p_group->i_start;
                    p_sub->i_stop =  VLC_TS_0 + p_group->i_end;
                }
                else
                {
                    p_sub->i_start    = p_block->i_pts;
                    p_sub->i_stop     = p_block->i_pts + p_block->i_length;
                    p_sub->b_ephemer  = (p_block->i_length == 0);
                }
425
                decoder_QueueSub(p_dec, p_sub);
François Cartegnie's avatar
François Cartegnie committed
426 427 428
            }
        }
    }
Laurent Aimar's avatar
Laurent Aimar committed
429

430 431
    ResetGroups(p_dec->p_sys);

François Cartegnie's avatar
François Cartegnie committed
432
    block_Release(p_block);
433
    return VLCDEC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
434 435
}

Stefan Pöschel's avatar
Stefan Pöschel committed
436
static int ParseGSI(const decoder_t *dec, decoder_sys_t *p_sys)
437 438 439 440 441 442 443 444 445 446 447 448
{
    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;
    }

449 450 451 452 453 454 455
    char dfc_fps_str[] = { header[6], header[7], '\0' };
    int fps = strtol(dfc_fps_str, NULL, 10);
    if (1 > fps || 60 < fps) {
        msg_Warn(dec, "EBU header contains unsupported DFC fps ('%s'); falling back to 25\n", dfc_fps_str);
        fps = 25;
    }

456 457 458 459 460
    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;
    }
461 462 463

    msg_Dbg(dec, "DFC fps=%d, CCT=0x%x", fps, cct);
    p_sys->i_fps = fps;
Stefan Pöschel's avatar
Stefan Pöschel committed
464
    p_sys->cct = cct;
465 466 467 468

    return VLC_SUCCESS;
}

Laurent Aimar's avatar
Laurent Aimar committed
469 470 471 472 473 474 475
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;

François Cartegnie's avatar
François Cartegnie committed
476
    decoder_sys_t *sys = calloc(1, sizeof(*sys));
477 478 479
    if (!sys)
        return VLC_ENOMEM;

Stefan Pöschel's avatar
Stefan Pöschel committed
480 481 482 483
    int rc = ParseGSI(dec, sys);
    if (VLC_SUCCESS != rc)
        return rc;

François Cartegnie's avatar
François Cartegnie committed
484 485
    for(size_t i=0; i<=STL_GROUPS_MAX; i++)
        sys->groups[i].pp_segment_last = &sys->groups[i].p_segment;
Laurent Aimar's avatar
Laurent Aimar committed
486 487

    dec->p_sys = sys;
488
    dec->pf_decode = Decode;
Laurent Aimar's avatar
Laurent Aimar committed
489 490 491 492 493 494 495
    dec->fmt_out.i_codec = 0;
    return VLC_SUCCESS;
}

static void Close(vlc_object_t *object)
{
    decoder_t *dec = (decoder_t*)object;
François Cartegnie's avatar
François Cartegnie committed
496
    decoder_sys_t *p_sys = dec->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
497

François Cartegnie's avatar
François Cartegnie committed
498 499
    ResetGroups(p_sys);
    free(p_sys);
Laurent Aimar's avatar
Laurent Aimar committed
500
}