⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subtitle.c

📁 video linux conference
💻 C
📖 第 1 页 / 共 3 页
字号:
    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++ )    {        if( p_sys->subtitle[i].psz_text )            free( p_sys->subtitle[i].psz_text );    }    if( p_sys->subtitle )        free( p_sys->subtitle );    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_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;        }        p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;        p_block->i_dts = p_block->i_pts;        if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )        {            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 );        if( p_block->i_pts > 0 )        {            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;    vlc_bool_t b_done;    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    {        b_done = VLC_TRUE;        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 ) );                b_done = VLC_FALSE;            }        }    } while( !b_done );}static int TextLoad( text_t *txt, stream_t *s ){    int   i_line_max;    /* init txt */    i_line_max          = 500;    txt->i_line_count   = 0;    txt->i_line         = 0;    txt->line           = calloc( i_line_max, sizeof( char * ) );    /* load the complete file */    for( ;; )    {        char *psz = stream_ReadLine( s );        if( psz == NULL )            break;        txt->line[txt->i_line_count++] = psz;        if( txt->i_line_count >= i_line_max )        {            i_line_max += 100;            txt->line = realloc( txt->line, i_line_max * sizeof( char*) );        }    }    if( txt->i_line_count <= 0 )    {        free( txt->line );        return VLC_EGENERIC;    }    return VLC_SUCCESS;}static void TextUnload( text_t *txt ){    int i;    for( i = 0; i < txt->i_line_count; i++ )    {        free( txt->line[i] );    }    free( txt->line );    txt->i_line       = 0;    txt->i_line_count = 0;}static char *TextGetLine( text_t *txt ){    if( txt->i_line >= txt->i_line_count )        return( NULL );    return txt->line[txt->i_line++];}static void TextPreviousLine( text_t *txt ){    if( txt->i_line > 0 )        txt->i_line--;}/***************************************************************************** * Specific Subtitle function *****************************************************************************/#define MAX_LINE 8192static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle ){    demux_sys_t *p_sys = p_demux->p_sys;    text_t      *txt = &p_sys->txt;    /*     * each line:     *  {n1}{n2}Line1|Line2|Line3....     * where n1 and n2 are the video frame number...     * {n2} can also be {}     */    char *s;    char buffer_text[MAX_LINE + 1];    int    i_start;    int    i_stop;    unsigned int i;    int i_microsecperframe = 40000; /* default to 25 fps */    if( p_sys->i_microsecperframe > 0 )         i_microsecperframe = p_sys->i_microsecperframe;        p_subtitle->i_start = 0;    p_subtitle->i_stop  = 0;    p_subtitle->psz_text = NULL;    for( ;; )    {        if( ( s = TextGetLine( txt ) ) == NULL )        {            return( VLC_EGENERIC );        }        i_start = 0;        i_stop  = 0;        memset( buffer_text, '\0', MAX_LINE );        if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||            sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)        {            break;        }    }    /* replace | by \n */    for( i = 0; i < strlen( buffer_text ); i++ )    {        if( buffer_text[i] == '|' )        {            buffer_text[i] = '\n';        }    }    p_subtitle->i_start = (int64_t)i_start * i_microsecperframe;    p_subtitle->i_stop  = (int64_t)i_stop  * i_microsecperframe;    p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );    return( 0 );}static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle ){    demux_sys_t *p_sys = p_demux->p_sys;    text_t      *txt = &p_sys->txt;    /*     * n     * h1:m1:s1,d1 --> h2:m2:s2,d2     * Line1     * Line2     * ...     * [empty line]     *     */    char *s;    char buffer_text[ 10 * MAX_LINE];    int  i_buffer_text;    int64_t     i_start;    int64_t     i_stop;    p_subtitle->i_start = 0;    p_subtitle->i_stop  = 0;    p_subtitle->psz_text = NULL;    for( ;; )    {        int h1, m1, s1, d1, h2, m2, s2, d2;        if( ( s = TextGetLine( txt ) ) == NULL )        {            return( VLC_EGENERIC );        }        if( sscanf( s,                    "%d:%d:%d,%d --> %d:%d:%d,%d",                    &h1, &m1, &s1, &d1,                    &h2, &m2, &s2, &d2 ) == 8 )        {            i_start = ( (int64_t)h1 * 3600*1000 +                        (int64_t)m1 * 60*1000 +                        (int64_t)s1 * 1000 +                        (int64_t)d1 ) * 1000;            i_stop  = ( (int64_t)h2 * 3600*1000 +                        (int64_t)m2 * 60*1000 +                        (int64_t)s2 * 1000 +                        (int64_t)d2 ) * 1000;            /* Now read text until an empty line */            for( i_buffer_text = 0;; )            {                int i_len;                if( ( s = TextGetLine( txt ) ) == NULL )                {                    return( VLC_EGENERIC );                }                i_len = strlen( s );                if( i_len <= 0 )                {                    /* empty line -> end of this subtitle */                    buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';                    p_subtitle->i_start = i_start;                    p_subtitle->i_stop = i_stop;                    p_subtitle->psz_text = strdup( buffer_text );                    /* If framerate is available, use sub-fps */                    if( p_sys->i_microsecperframe != 0 &&                        p_sys->i_original_mspf != 0)                    {                        p_subtitle->i_start = (int64_t)i_start *                                              p_sys->i_microsecperframe/                                              p_sys->i_original_mspf;                        p_subtitle->i_stop  = (int64_t)i_stop  *                                              p_sys->i_microsecperframe /                                              p_sys->i_original_mspf;                    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -