subtitle_asa.c 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*****************************************************************************
 * subtitle_asa.c: Demux for subtitle text files using the asa engine.
 *****************************************************************************
 * Copyright (C) 1999-2007 the VideoLAN team
 * $Id$
 *
 * Authors: David Lamparter <equinox at videolan dot org>
 *
 * Originated from subtitle.c
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Derk-Jan Hartman <hartman 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
 * (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.
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "config.h"
33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_input.h>
36
#include <vlc_memory.h>
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

#include <vlc_demux.h>

#include "asademux.h"

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

#define SUB_DELAY_LONGTEXT \
    N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
#define SUB_FPS_LONGTEXT \
    N_("Override the normal frames per second settings. " \
    "This will only affect frame-based subtitle formats without a fixed value.")
#define SUB_TYPE_LONGTEXT \
    N_("Force the subtiles format. Use \"auto\", the set of supported values varies.")

56 57 58 59 60 61
vlc_module_begin ()
    set_shortname( N_("Subtitles (asa demuxer)"))
    set_description( N_("Text subtitles parser") )
    set_capability( "demux", 50 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
62 63
    add_float( "sub-fps", 0.0, NULL,
               N_("Frames per second"),
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
64
               SUB_FPS_LONGTEXT, true )
65 66
    add_integer( "sub-delay", 0, NULL,
               N_("Subtitles delay"),
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
67
               SUB_DELAY_LONGTEXT, true )
68
    add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
69
                SUB_TYPE_LONGTEXT, true )
70
    set_callbacks( Open, Close )
71

72 73
    add_shortcut( "asademux" )
vlc_module_end ()
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

/*****************************************************************************
 * Prototypes:
 *****************************************************************************/
typedef struct
{
    int64_t i_start;
    int64_t i_stop;

    char    *psz_text;
} subtitle_t;


struct demux_sys_t
{
    int         i_type;
    es_out_id_t *es;

    int64_t     i_next_demux_date;
    int64_t     i_microsecperframe;

    char        *psz_header;
    int         i_subtitle;
    int         i_subtitles;
    int         i_subs_alloc;
    subtitle_t  *subtitle;

    int64_t     i_length;
};

static int Demux( demux_t * );
static int Control( demux_t *, int, va_list );

static void Fix( demux_t * );

static int ProcessLine( demux_t *, void *, int64_t, int64_t,
                         const char *, size_t );

/*****************************************************************************
 * Module initializer
 *****************************************************************************/
static int Open ( vlc_object_t *p_this )
{
    demux_t        *p_demux = (demux_t*)p_this;
    demux_sys_t    *p_sys;
    es_format_t    fmt;
    input_thread_t *p_input;
    float          f_fps;
    char           *psz_type;
    int64_t        i_ssize;
    void           *p_data;
    struct asa_import_detect *p_detect = NULL;

    if( strcmp( p_demux->psz_demux, "asademux" ) )
    {
        return VLC_EGENERIC;
    }

    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
ivoire's avatar
ivoire committed
135 136
    if( !p_sys  )
        return VLC_ENOMEM;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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
    p_sys->psz_header         = NULL;
    p_sys->i_subtitle         = 0;
    p_sys->i_subtitles        = 0;
    p_sys->i_subs_alloc       = 0;
    p_sys->subtitle           = NULL;
    p_sys->i_microsecperframe = 40000;

    /* Get the FPS */
    p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
    if( p_input )
    {
        f_fps = var_GetFloat( p_input, "sub-original-fps" );
        if( f_fps >= 1.0 )
            p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );

        msg_Dbg( p_demux, "Movie fps: %f", f_fps );
        vlc_object_release( p_input );
    }

    /* Check for override of the fps */
    f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
    if( f_fps >= 1.0 )
    {
        p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
        msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
    }

    /* Get or probe the type */
    psz_type = var_CreateGetString( p_demux, "sub-type" );
    if( *psz_type )
    {
        for( p_detect = asa_det_first; p_detect; p_detect = p_detect->next )
        {
            if( !strcmp( p_detect->name, psz_type ) )
            {
                break;
            }
        }
        if( !p_detect )
        {
            msg_Warn( p_demux, "unknown sub-type \"%s\"", psz_type );
        }
    }
    free( psz_type );

    /* Probe if unknown type */
    if( !p_detect )
    {
        int i_size;
        const uint8_t *p;

        msg_Dbg( p_demux, "autodetecting subtitle format" );
        i_size = stream_Peek( p_demux->s, &p, 4096 );

        if( i_size <= 0)
        {
            msg_Warn( p_demux, "cannot process subtitles (no data?)" );
            return VLC_EGENERIC;
        }
        p_detect = asa_imports_detect( p, i_size );

    }
    if( !p_detect )
    {
        msg_Err( p_demux, "failed to recognize subtitle type" );
        free( p_sys );
        return VLC_EGENERIC;
    }
    if( !p_detect->fmt )
    {
207
        msg_Err( p_demux, "detected %s subtitle format, no asa support", p_detect->name );
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        free( p_sys );
        return VLC_EGENERIC;
    }
    msg_Dbg( p_demux, "detected %s subtitle format", p_detect->name );

    stream_Control( p_demux->s, STREAM_GET_SIZE, &i_ssize );
    p_data = malloc( i_ssize );
    if( !p_data )
    {
        free( p_sys );
        return VLC_ENOMEM;
    }
    if( stream_Read( p_demux->s, &p_data, i_ssize ) != i_ssize )
    {
        msg_Err( p_demux, "subtitle stream read error" );
        free( p_data );
        free( p_sys );
        return VLC_EGENERIC;
    }
    asa_import( p_demux, p_data, i_ssize, p_sys->i_microsecperframe, p_detect,
                ProcessLine, NULL );
    free( p_data );

    msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );

    /* Fix subtitle (order and time) *** */
    Fix( p_demux );
    p_sys->i_subtitle = 0;
    p_sys->i_length = 0;
    if( p_sys->i_subtitles > 0 )
    {
        p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
        /* +1 to avoid 0 */
        if( p_sys->i_length <= 0 )
242
            p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start + VLC_TS_0;
243 244 245 246 247
    }

    /* *** add subtitle ES *** */
    if( p_detect->fmt->target == ASAI_TARGET_SSA )
    {
248
        es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
249 250 251
    }
    else
    {
252
        es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    }
    p_sys->es = es_out_Add( p_demux->out, &fmt );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * ProcessLine: Callback for asa_import, fed one line
 * (note: return values are not kept. nonzero signals abort to asa_import)
 *****************************************************************************/
static int ProcessLine( demux_t *p_demux, void *p_arg,
                         int64_t i_start, int64_t i_stop,
                         const char *p_buffer, size_t i_buffer_length )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    subtitle_t *p_subtitle;
    char *psz_text;

271 272
    VLC_UNUSED(p_arg);

273 274 275
    if( p_sys->i_subtitles >= p_sys->i_subs_alloc )
    {
        p_sys->i_subs_alloc += 500;
276 277
        if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
                                sizeof(subtitle_t) * p_sys->i_subs_alloc ) ) )
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
        {
            return VLC_ENOMEM;
        }
    }
    p_subtitle = &p_sys->subtitle[p_sys->i_subtitles];

    psz_text = malloc( i_buffer_length + 1 );
    if( !psz_text )
        return VLC_ENOMEM;
    memcpy( psz_text, p_buffer, i_buffer_length );
    psz_text[i_buffer_length] = '\0';

    p_subtitle->i_start = i_start;
    p_subtitle->i_stop  = i_stop;
    p_subtitle->psz_text = psz_text;

    p_sys->i_subtitles++;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: Close subtitle demux
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
    int i;

    for( i = 0; i < p_sys->i_subtitles; i++ )
308 309
        free( p_sys->subtitle[i].psz_text );
    free( p_sys->subtitle );
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

    free( p_sys );
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    int64_t *pi64, i64;
    double *pf, f;

    switch( i_query )
    {
        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            *pi64 = p_sys->i_length;
            return VLC_SUCCESS;

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->i_subtitle < p_sys->i_subtitles )
            {
                *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_SET_TIME:
            i64 = (int64_t)va_arg( args, int64_t );
            p_sys->i_subtitle = 0;
            while( p_sys->i_subtitle < p_sys->i_subtitles &&
                   p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
            {
                p_sys->i_subtitle++;
            }

            if( p_sys->i_subtitle >= p_sys->i_subtitles )
                return VLC_EGENERIC;
            return VLC_SUCCESS;

        case DEMUX_GET_POSITION:
            pf = (double*)va_arg( args, double * );
            if( p_sys->i_subtitle >= p_sys->i_subtitles )
            {
                *pf = 1.0;
            }
            else if( p_sys->i_subtitles > 0 )
            {
                *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
                      (double)p_sys->i_length;
            }
            else
            {
                *pf = 0.0;
            }
            return VLC_SUCCESS;

        case DEMUX_SET_POSITION:
            f = (double)va_arg( args, double );
            i64 = f * p_sys->i_length;

            p_sys->i_subtitle = 0;
            while( p_sys->i_subtitle < p_sys->i_subtitles &&
                   p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
            {
                p_sys->i_subtitle++;
            }
            if( p_sys->i_subtitle >= p_sys->i_subtitles )
                return VLC_EGENERIC;
            return VLC_SUCCESS;

        case DEMUX_SET_NEXT_DEMUX_TIME:
            p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
            return VLC_SUCCESS;

        case DEMUX_GET_FPS:
        case DEMUX_GET_META:
        case DEMUX_GET_ATTACHMENTS:
        case DEMUX_GET_TITLE_INFO:
            return VLC_EGENERIC;

        default:
            msg_Err( p_demux, "unknown query in subtitle control" );
            return VLC_EGENERIC;
    }
}

/*****************************************************************************
 * Demux: Send subtitle to decoder
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    int64_t i_maxdate;

    if( p_sys->i_subtitle >= p_sys->i_subtitles )
        return 0;

    i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
    if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
    {
        /* Should not happen */
        i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
    }

    while( p_sys->i_subtitle < p_sys->i_subtitles &&
           p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
    {
        block_t *p_block;
        int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;

        if( i_len <= 1 )
        {
            /* empty subtitle */
            p_sys->i_subtitle++;
            continue;
        }

        if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
        {
            p_sys->i_subtitle++;
            continue;
        }

        if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
        {
            p_sys->i_subtitle++;
            continue;
        }

442 443 444
        p_block->i_pts = VLC_TS_0 + p_sys->subtitle[p_sys->i_subtitle].i_start;
        p_block->i_dts = VLC_TS_0 + p_block->i_pts;
        if( p_sys->subtitle[p_sys->i_subtitle].i_stop >= 0 )
445 446 447 448 449 450 451
        {
            p_block->i_length =
                p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
        }

        memcpy( p_block->p_buffer,
                p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
452
        if( p_block->i_pts > VLC_TS_INVALID )
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
        {
            es_out_Send( p_demux->out, p_sys->es, p_block );
        }
        else
        {
            block_Release( p_block );
        }
        p_sys->i_subtitle++;
    }

    /* */
    p_sys->i_next_demux_date = 0;

    return 1;
}

/*****************************************************************************
 * Fix: fix time stamp and order of subtitle
 *****************************************************************************/
static void Fix( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
475
    bool b_done;
476 477 478 479 480 481 482 483
    int     i_index;

    /* *** fix order (to be sure...) *** */
    /* We suppose that there are near in order and this durty bubble sort
     * wont take too much time
     */
    do
    {
484
        b_done = true;
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
        for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
        {
            if( p_sys->subtitle[i_index].i_start <
                    p_sys->subtitle[i_index - 1].i_start )
            {
                subtitle_t sub_xch;
                memcpy( &sub_xch,
                        p_sys->subtitle + i_index - 1,
                        sizeof( subtitle_t ) );
                memcpy( p_sys->subtitle + i_index - 1,
                        p_sys->subtitle + i_index,
                        sizeof( subtitle_t ) );
                memcpy( p_sys->subtitle + i_index,
                        &sub_xch,
                        sizeof( subtitle_t ) );
500
                b_done = false;
501 502 503 504 505
            }
        }
    } while( !b_done );
}