Exemplo n.º 1
0
static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    const char *node;
    ssa_style_t *p_ssa_style = NULL;
    int i_style_level = 0;
    int i_metadata_level = 0;
    int type;

    while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
    {
        switch( type )
        {
            case XML_READER_ENDELEM:
                switch (i_style_level)
                {
                    case 0:
                        if( !strcasecmp( "metadata", node ) && (i_metadata_level == 1) )
                            i_metadata_level--;
                        break;
                    case 1:
                        if( !strcasecmp( "styles", node ) )
                            i_style_level--;
                        break;
                    case 2:
                        if( !strcasecmp( "style", node ) )
                        {
                            TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_ssa_style );

                            p_ssa_style = NULL;
                            i_style_level--;
                        }
                        break;
                }
                break;

            case XML_READER_STARTELEM:
                if( !strcasecmp( "metadata", node ) && (i_style_level == 0) )
                    i_metadata_level++;
                else if( !strcasecmp( "resolution", node ) &&
                         ( i_metadata_level == 1) )
                {
                    const char *attr, *val;
                    while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) )
                    {
                        if( !strcasecmp( "x", attr ) )
                            p_sys->i_original_width = atoi( val );
                        else if( !strcasecmp( "y", attr ) )
                            p_sys->i_original_height = atoi( val );
                    }
                }
                else if( !strcasecmp( "styles", node ) && (i_style_level == 0) )
                {
                    i_style_level++;
                }
                else if( !strcasecmp( "style", node ) && (i_style_level == 1) )
                {
                    i_style_level++;

                    p_ssa_style = calloc( 1, sizeof(ssa_style_t) );
                    if( unlikely(!p_ssa_style) )
                        return;
                    p_ssa_style->p_style = text_style_Create( STYLE_NO_DEFAULTS );
                    if( unlikely(!p_ssa_style->p_style) )
                    {
                        free(p_ssa_style);
                        return;
                    }
                    /* All styles are supposed to default to Default, and then
                     * one or more settings are over-ridden.
                     * At the moment this only effects styles defined AFTER
                     * Default in the XML
                     */
                    for( int i = 0; i < p_sys->i_ssa_styles; i++ )
                    {
                        if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
                        {
                            ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i];

                            memcpy( p_ssa_style, p_default_style, sizeof( ssa_style_t ) );
                            //FIXME: Make font_style a pointer. Actually we double copy some data here,
                            //   we use text_style_Copy to avoid copying psz_fontname, though .
                            text_style_Copy( p_ssa_style->p_style, p_default_style->p_style );
                            p_ssa_style->psz_stylename = NULL;
                        }
                    }

                    const char *attr, *val;
                    while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) )
                    {
                        if( !strcasecmp( "name", attr ) )
                        {
                            free( p_ssa_style->psz_stylename );
                            p_ssa_style->psz_stylename = strdup( val );
                        }
                    }
                }
                else if( !strcasecmp( "fontstyle", node ) && (i_style_level == 2) )
                {
                    const char *attr, *val;
                    while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) )
                    {
                        if( !strcasecmp( "face", attr ) )
                        {
                            free( p_ssa_style->p_style->psz_fontname );
                            p_ssa_style->p_style->psz_fontname = strdup( val );
                        }
                        else if( !strcasecmp( "size", attr ) )
                        {
                            if( ( *val == '+' ) || ( *val == '-' ) )
                            {
                                int i_value = atoi( val );

                                if( ( i_value >= -5 ) && ( i_value <= 5 ) )
                                    p_ssa_style->p_style->i_font_size  +=
                                       ( i_value * p_ssa_style->p_style->i_font_size ) / 10;
                                else if( i_value < -5 )
                                    p_ssa_style->p_style->i_font_size  = - i_value;
                                else if( i_value > 5 )
                                    p_ssa_style->p_style->i_font_size  = i_value;
                            }
                            else
                                p_ssa_style->p_style->i_font_size  = atoi( val );
                        }
                        else if( !strcasecmp( "italic", attr ) )
                        {
                            if( !strcasecmp( "yes", val ))
                                p_ssa_style->p_style->i_style_flags |= STYLE_ITALIC;
                            else
                                p_ssa_style->p_style->i_style_flags &= ~STYLE_ITALIC;
                            p_ssa_style->p_style->i_features |= STYLE_HAS_FLAGS;
                        }
                        else if( !strcasecmp( "weight", attr ) )
                        {
                            if( !strcasecmp( "bold", val ))
                                p_ssa_style->p_style->i_style_flags |= STYLE_BOLD;
                            else
                                p_ssa_style->p_style->i_style_flags &= ~STYLE_BOLD;
                            p_ssa_style->p_style->i_features |= STYLE_HAS_FLAGS;
                        }
                        else if( !strcasecmp( "underline", attr ) )
                        {
                            if( !strcasecmp( "yes", val ))
                                p_ssa_style->p_style->i_style_flags |= STYLE_UNDERLINE;
                            else
                                p_ssa_style->p_style->i_style_flags &= ~STYLE_UNDERLINE;
                            p_ssa_style->p_style->i_features |= STYLE_HAS_FLAGS;
                        }
                        else if( !strcasecmp( "color", attr ) )
                        {
                            if( *val == '#' )
                            {
                                unsigned long col = strtol(val+1, NULL, 16);
                                 p_ssa_style->p_style->i_font_color = (col & 0x00ffffff);
                                 p_ssa_style->p_style->i_font_alpha = (col >> 24) & 0xff;
                                 p_ssa_style->p_style->i_features |= STYLE_HAS_FONT_COLOR
                                                                   | STYLE_HAS_FONT_ALPHA;
                            }
                        }
                        else if( !strcasecmp( "outline-color", attr ) )
                        {
                            if( *val == '#' )
                            {
                                unsigned long col = strtol(val+1, NULL, 16);
                                p_ssa_style->p_style->i_outline_color = (col & 0x00ffffff);
                                p_ssa_style->p_style->i_outline_alpha = (col >> 24) & 0xff;
                                p_ssa_style->p_style->i_features |= STYLE_HAS_OUTLINE_COLOR
                                                                  | STYLE_HAS_OUTLINE_ALPHA;
                            }
                        }
                        else if( !strcasecmp( "outline-level", attr ) )
                        {
                            p_ssa_style->p_style->i_outline_width = atoi( val );
                        }
                        else if( !strcasecmp( "shadow-color", attr ) )
                        {
                            if( *val == '#' )
                            {
                                unsigned long col = strtol(val+1, NULL, 16);
                                p_ssa_style->p_style->i_shadow_color = (col & 0x00ffffff);
                                p_ssa_style->p_style->i_shadow_alpha = (col >> 24) & 0xff;
                                p_ssa_style->p_style->i_features |= STYLE_HAS_SHADOW_COLOR
                                                                  | STYLE_HAS_SHADOW_ALPHA;
                            }
                        }
Exemplo n.º 2
0
static text_style_t* CurrentStyle( style_stack_t* p_stack )
{
    if ( p_stack == NULL )
        return text_style_Create( STYLE_NO_DEFAULTS );
    return text_style_Duplicate( p_stack->p_style->font_style );
}
Exemplo n.º 3
0
text_style_t *text_style_New( void )
{
    return text_style_Create( STYLE_FULLY_SET );
}
Exemplo n.º 4
0
Arquivo: zvbi.c Projeto: BossKing/vlc
/*****************************************************************************
 * Decode:
 *****************************************************************************/
static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t   *p_sys = p_dec->p_sys;
    block_t         *p_block;
    subpicture_t    *p_spu = NULL;
    video_format_t  fmt;
    bool            b_cached = false;
    vbi_page        p_page;

    if( (pp_block == NULL) || (*pp_block == NULL) )
        return NULL;

    p_block = *pp_block;
    *pp_block = NULL;

    if( p_block->i_buffer > 0 &&
        ( ( p_block->p_buffer[0] >= 0x10 && p_block->p_buffer[0] <= 0x1f ) ||
          ( p_block->p_buffer[0] >= 0x99 && p_block->p_buffer[0] <= 0x9b ) ) )
    {
        vbi_sliced   *p_sliced = p_sys->p_vbi_sliced;
        unsigned int i_lines = 0;

        p_block->i_buffer--;
        p_block->p_buffer++;
        while( p_block->i_buffer >= 2 )
        {
            int      i_id   = p_block->p_buffer[0];
            unsigned i_size = p_block->p_buffer[1];

            if( 2 + i_size > p_block->i_buffer )
                break;

            if( ( i_id == 0x02 || i_id == 0x03 ) && i_size >= 44 && i_lines < MAX_SLICES )
            {
                if(p_block->p_buffer[3] == 0xE4 )    /* framing_code */
                {
                    unsigned line_offset  = p_block->p_buffer[2] & 0x1f;
                    unsigned field_parity = p_block->p_buffer[2] & 0x20;

                    p_sliced[i_lines].id = VBI_SLICED_TELETEXT_B;
                    if( line_offset > 0 )
                        p_sliced[i_lines].line = line_offset + (field_parity ? 0 : 313);
                    else
                        p_sliced[i_lines].line = 0;
                    for( int i = 0; i < 42; i++ )
                        p_sliced[i_lines].data[i] = vbi_rev8( p_block->p_buffer[4 + i] );
                    i_lines++;
                }
            }

            p_block->i_buffer -= 2 + i_size;
            p_block->p_buffer += 2 + i_size;
        }

        if( i_lines > 0 )
            vbi_decode( p_sys->p_vbi_dec, p_sliced, i_lines, 0 );
    }

    /* */
    vlc_mutex_lock( &p_sys->lock );
    const int i_align = p_sys->i_align;
    const unsigned int i_wanted_page = p_sys->i_wanted_page;
    const unsigned int i_wanted_subpage = p_sys->i_wanted_subpage;
    const bool b_opaque = p_sys->b_opaque;
    const unsigned int i_level = p_sys->i_level > 3 ? 3 : p_sys->i_level;
    vlc_mutex_unlock( &p_sys->lock );

    /* Try to see if the page we want is in the cache yet */
    memset( &p_page, 0, sizeof(vbi_page) );
    b_cached = vbi_fetch_vt_page( p_sys->p_vbi_dec, &p_page,
                                  vbi_dec2bcd( i_wanted_page ),
                                  i_wanted_subpage, level_zvbi_values[i_level],
                                  25, true );

    if( i_wanted_page == p_sys->i_last_page && !p_sys->b_update )
        goto error;

    if( !b_cached )
    {
        if( p_sys->b_text && p_sys->i_last_page != i_wanted_page )
        {
            /* We need to reset the subtitle */
            p_spu = Subpicture( p_dec, &fmt, true,
                                p_page.columns, p_page.rows,
                                i_align, p_block->i_pts );
            if( !p_spu )
                goto error;
            subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
            p_spu_sys->p_segments = text_segment_New("");

            p_sys->b_update = true;
            p_sys->i_last_page = i_wanted_page;
            goto exit;
        }
        goto error;
    }

    p_sys->b_update = false;
    p_sys->i_last_page = i_wanted_page;
#ifdef ZVBI_DEBUG
    msg_Dbg( p_dec, "we now have page: %d ready for display",
             i_wanted_page );
#endif

    /* Ignore transparent rows at the beginning and end */
    int i_first_row = get_first_visible_row( p_page.text, p_page.rows, p_page.columns );
    int i_num_rows;
    if ( i_first_row < 0 ) {
        i_first_row = p_page.rows - 1;
        i_num_rows = 0;
    } else {
        i_num_rows = get_last_visible_row( p_page.text, p_page.rows, p_page.columns ) - i_first_row + 1;
    }
#ifdef ZVBI_DEBUG
    msg_Dbg( p_dec, "After top and tail of page we have rows %i-%i of %i",
             i_first_row + 1, i_first_row + i_num_rows, p_page.rows );
#endif

    /* If there is a page or sub to render, then we do that here */
    /* Create the subpicture unit */
    p_spu = Subpicture( p_dec, &fmt, p_sys->b_text,
                        p_page.columns, i_num_rows,
                        i_align, p_block->i_pts );
    if( !p_spu )
        goto error;

    if( p_sys->b_text )
    {
        unsigned int i_textsize = 7000;
        int i_total,offset;
        char p_text[i_textsize+1];

        i_total = vbi_print_page_region( &p_page, p_text, i_textsize,
                        "UTF-8", 0, 0, 0, i_first_row, p_page.columns, i_num_rows );

        for( offset=1; offset<i_total && isspace( p_text[i_total-offset ] ); offset++)
           p_text[i_total-offset] = '\0';

        i_total -= offset;

        offset=0;
        while( offset < i_total && isspace( p_text[offset] ) )
           offset++;

        subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
        p_spu_sys->p_segments = text_segment_New( &p_text[offset] );
        if( p_spu_sys->p_segments && b_opaque )
        {
            p_spu_sys->p_segments->style = text_style_Create( STYLE_NO_DEFAULTS );
            if( p_spu_sys->p_segments->style )
            {
                /* Set text background */
                p_spu_sys->p_segments->style->i_style_flags = STYLE_BACKGROUND;
                p_spu_sys->p_segments->style->i_features |= STYLE_HAS_FLAGS;
            }
        }

        p_spu_sys->align = i_align;
        p_spu_sys->noregionbg = true;

#ifdef ZVBI_DEBUG
        msg_Info( p_dec, "page %x-%x(%d)\n\"%s\"", p_page.pgno, p_page.subno, i_total, &p_text[offset] );
#endif
    }
    else
    {
        picture_t *p_pic = p_spu->p_region->p_picture;

        /* ZVBI is stupid enough to assume pitch == width */
        p_pic->p->i_pitch = 4 * fmt.i_width;

        /* Maintain subtitle postion */
        p_spu->p_region->i_y = i_first_row*10;
        p_spu->i_original_picture_width = p_page.columns*12;
        p_spu->i_original_picture_height = p_page.rows*10;

        vbi_draw_vt_page_region( &p_page, ZVBI_PIXFMT_RGBA32,
                          p_spu->p_region->p_picture->p->p_pixels, -1,
                          0, i_first_row, p_page.columns, i_num_rows,
                          1, 1);

        vlc_mutex_lock( &p_sys->lock );
        memcpy( p_sys->nav_link, &p_page.nav_link, sizeof( p_sys->nav_link )) ;
        vlc_mutex_unlock( &p_sys->lock );

        OpaquePage( p_pic, &p_page, fmt, b_opaque, i_first_row * p_page.columns );
    }

exit:
    vbi_unref_page( &p_page );
    block_Release( p_block );
    return p_spu;

error:
    vbi_unref_page( &p_page );
    block_Release( p_block );
    return NULL;
}
Exemplo n.º 5
0
static text_segment_t* NewTextSegmentPopStyle( text_segment_t* p_segment, style_stack_t** pp_stack )
{
    text_segment_t* p_new = text_segment_New( NULL );
    if ( unlikely( p_new == NULL ) )
        return NULL;
    // We shouldn't have an empty stack since this happens when closing a tag,
    // but better be safe than sorry if (/when) we encounter a broken subtitle file.
    PopStyle( pp_stack );
    text_style_t* p_dup = ( *pp_stack ) ? text_style_Duplicate( (*pp_stack)->p_style ) : text_style_Create( STYLE_NO_DEFAULTS );
    p_new->style = p_dup;
    p_segment->p_next = p_new;
    return p_new;
}
Exemplo n.º 6
0
static text_style_t* DuplicateAndPushStyle(style_stack_t** pp_stack)
{
    text_style_t* p_dup = ( *pp_stack ) ? text_style_Duplicate( (*pp_stack)->p_style ) : text_style_Create( STYLE_NO_DEFAULTS );
    if ( unlikely( !p_dup ) )
        return NULL;
    style_stack_t* p_entry = malloc( sizeof( *p_entry ) );
    if ( unlikely( !p_entry ) )
    {
        free( p_dup );
        return NULL;
    }
    // Give the style ownership to the segment.
    p_entry->p_style = p_dup;
    p_entry->p_next = *pp_stack;
    *pp_stack = p_entry;
    return p_dup;
}