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

📄 real.c

📁 video linux conference
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************** * real.c: Real demuxer. ***************************************************************************** * Copyright (C) 2004 VideoLAN * $Id: real.c 10150 2005-03-05 17:54:19Z gbazin $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <stdlib.h>                                      /* malloc(), free() */#include <vlc/vlc.h>#include <vlc/input.h>/***************************************************************************** * Module descriptor *****************************************************************************/static int  Open    ( vlc_object_t * );static void Close  ( vlc_object_t * );vlc_module_begin();    set_description( _("Real demuxer" ) );    set_capability( "demux2", 15 );    set_category( CAT_INPUT );    set_subcategory( SUBCAT_INPUT_DEMUX );    set_callbacks( Open, Close );    add_shortcut( "real" );    add_shortcut( "rm" );vlc_module_end();/***************************************************************************** * Local prototypes *****************************************************************************/typedef struct{    int         i_id;    es_format_t fmt;    es_out_id_t *p_es;    int         i_frame;    block_t     *p_frame;} real_track_t;struct demux_sys_t{    int64_t  i_data_offset;    int64_t  i_data_size;    uint32_t i_data_packets_count;    uint32_t i_data_packets;    int64_t  i_data_offset_next;    int          i_track;    real_track_t **track;    uint8_t buffer[65536];    int64_t     i_pcr;};static int Demux( demux_t *p_demux );static int Control( demux_t *p_demux, int i_query, va_list args );static int HeaderRead( demux_t *p_demux );/***************************************************************************** * Open *****************************************************************************/static int Open( vlc_object_t *p_this ){    demux_t     *p_demux = (demux_t*)p_this;    demux_sys_t *p_sys;    uint8_t     *p_peek;    if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;    if( strncmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;    /* Fill p_demux field */    p_demux->pf_demux = Demux;    p_demux->pf_control = Control;    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );    p_sys->i_data_offset = 0;    p_sys->i_track = 0;    p_sys->track   = NULL;    p_sys->i_pcr   = 1;    /* Parse the headers */    if( HeaderRead( p_demux ) )    {        int i;        msg_Err( p_demux, "invalid header" );        for( i = 0; i < p_sys->i_track; i++ )        {            real_track_t *tk = p_sys->track[i];            if( tk->p_es )            {                es_out_Del( p_demux->out, tk->p_es );            }            free( tk );        }        if( p_sys->i_track > 0 )        {            free( p_sys->track );        }        free( p_sys );        return VLC_EGENERIC;    }    return VLC_SUCCESS;}/***************************************************************************** * Close *****************************************************************************/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_track; i++ )    {        real_track_t *tk = p_sys->track[i];        if( tk->p_frame )        {            block_Release( tk->p_frame );        }        free( tk );    }    if( p_sys->i_track > 0 )    {        free( p_sys->track );    }    free( p_sys );}/***************************************************************************** * Demux: *****************************************************************************/static int Demux( demux_t *p_demux ){    demux_sys_t *p_sys = p_demux->p_sys;    uint8_t     header[18];    int         i_size;    int         i_id;    int64_t     i_pts;    int         i;    real_track_t *tk = NULL;    vlc_bool_t  b_selected;    if( p_sys->i_data_packets >= p_sys->i_data_packets_count )    {        if( stream_Read( p_demux->s, header, 18 ) < 18 )        {            return 0;        }        if( strncmp( header, "DATA", 4 ) )        {            return 0;        }        p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;        p_sys->i_data_size   = GetDWBE( &header[4] );        p_sys->i_data_packets_count = GetDWBE( &header[10] );        p_sys->i_data_packets = 0;        p_sys->i_data_offset_next = GetDWBE( &header[14] );        msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",                 p_sys->i_data_packets_count,                 (uint32_t)p_sys->i_data_offset_next );    }    if( stream_Read( p_demux->s, header, 12 ) < 12 )    {        return 0;    }    i_size = GetWBE( &header[2] ) - 12;    i_id   = GetWBE( &header[4] );    i_pts  = 1000 * GetDWBE( &header[6] );    /* header[11] -> flags 0x02 -> keyframe */    msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",             p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );    p_sys->i_data_packets++;    stream_Read( p_demux->s, p_sys->buffer, i_size );    for( i = 0; i < p_sys->i_track; i++ )    {        if( p_sys->track[i]->i_id == i_id )        {            tk = p_sys->track[i];        }    }    if( tk == NULL )    {        msg_Warn( p_demux, "unknown track id(0x%x)", i_id );        return 1;    }    es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );    if( tk->fmt.i_cat == VIDEO_ES && b_selected )    {        uint8_t     *p = p_sys->buffer;        while( p < &p_sys->buffer[i_size - 2] )        {            uint8_t h = *p++;            int     i_len = 0;            int     i_copy;            int     i_subseq = 0;            int     i_seqnum = 0;            int     i_offset = 0;            if( (h&0xc0) == 0x40 )            {                /* Short header */                p++;                i_len = &p_sys->buffer[i_size] - p;            }            else            {                if( (h&0x40) == 0 )                {                    i_subseq = (*p++)&0x7f;                }                i_len = (p[0] << 8)|p[1]; p += 2;                if( (i_len&0xc000) == 0 )                {                    i_len <<= 16;                    i_len |= (p[0] << 8)|p[1]; p += 2;                    i_len &= 0x3fffffff;                }                else                {                    i_len &= 0x3fff;                }                i_offset = (p[0] << 8)|p[1]; p += 2;                if( (i_offset&0xc000) == 0 )                {                    i_offset <<= 16;                    i_offset |= (p[0] << 8)|p[1]; p += 2;                    i_offset &= 0x3fffffff;                }                else                {                    i_offset &= 0x3fff;                }                i_seqnum = *p++;            }            i_copy = i_len - i_offset;            if( i_copy > &p_sys->buffer[i_size] - p )            {                i_copy = &p_sys->buffer[i_size] - p;            }            else if( i_copy < 0 )            {                break;            }            msg_Dbg( p_demux, "    - len=%d offset=%d size=%d subseq=%d seqnum=%d",                     i_len, i_offset, i_copy, i_subseq, i_seqnum );            if( (h&0xc0) == 0x80 )            {                /* last fragment -> fixes */                i_copy = i_offset;                i_offset = i_len - i_copy;                msg_Dbg( p_demux, "last fixing copy=%d offset=%d", i_copy, i_offset );            }            if( tk->p_frame &&                ( tk->p_frame->i_dts != i_pts ||                  tk->i_frame != i_len ) )            {                msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );                if( p_sys->i_pcr < tk->p_frame->i_dts )                {                    p_sys->i_pcr = tk->p_frame->i_dts;                    es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );                }                es_out_Send( p_demux->out, tk->p_es, tk->p_frame );                tk->i_frame = 0;                tk->p_frame = NULL;            }            if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && tk->p_frame == NULL )            {                /* no fragment */                i_len = i_copy;                i_offset = 0;            }            if( tk->p_frame == NULL )            {                msg_Dbg( p_demux, "new frame size=%d", i_len );                tk->i_frame = i_len;                if( ( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) == NULL )                {                    return -1;                }                memset( &tk->p_frame->p_buffer[8], 0, i_len );                tk->p_frame->i_dts = i_pts;                tk->p_frame->i_pts = i_pts;                ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */                ((uint32_t*)tk->p_frame->p_buffer)[1] = 0;      /* chunk counts */            }            if( i_offset < tk->i_frame)            {                int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;                msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d", i_ck, i_offset, i_copy );                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;                memcpy( &tk->p_frame->p_buffer[i_offset + 8],                        p, i_copy );            }            p += i_copy;            if( (h&0xc0) != 0x80 )            {                break;            }#if 0            if( tk->p_frame )            {                /* append data */                int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;                if( (h&0xc0) == 0x80 )                {                    /* last fragment */                    i_copy = i_offset;                    i_offset = i_len - i_offset;                    ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;                    memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );                    p += i_copy;                    if( p_sys->i_pcr < tk->p_frame->i_dts )                    {                        p_sys->i_pcr = tk->p_frame->i_dts;                        es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );                    }                    es_out_Send( p_demux->out, tk->p_es, tk->p_frame );                    tk->i_frame = 0;                    tk->p_frame = NULL;                    continue;                }                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;                memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );                break;            }            if( (h&0xc0) != 0x00 )            {                block_t *p_frame;                /* not fragmented */                if( ( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) == NULL )                {                    return -1;                }                p_frame->i_dts = i_pts;                p_frame->i_pts = i_pts;                ((uint32_t*)p_frame->p_buffer)[0] = i_copy;                ((uint32_t*)p_frame->p_buffer)[1] = 1;                ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;                memcpy( &p_frame->p_buffer[8], p, i_copy );                p += i_copy;                if( p_sys->i_pcr < p_frame->i_dts )                {                    p_sys->i_pcr = p_frame->i_dts;                    es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );                }                es_out_Send( p_demux->out, tk->p_es, p_frame );            }            else            {                /* First fragment */                tk->i_frame = i_len;                if( ( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) == NULL )                {                    return -1;                }                memset( &tk->p_frame->p_buffer[8], 0, i_len );                tk->p_frame->i_dts = i_pts;                tk->p_frame->i_pts = i_pts;                ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len;  /* len */                ((uint32_t*)tk->p_frame->p_buffer)[1] = 1;      /* chunk counts */                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;                memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );                break;            }#endif        }    }    else if( tk->fmt.i_cat == AUDIO_ES && b_selected )    {        /* Set PCR */        if( p_sys->i_pcr < i_pts )        {            p_sys->i_pcr = i_pts;            es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr );        }        if( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', '4', 'a' ) )        {            int     i_sub = (p_sys->buffer[1] >> 4)&0x0f;            uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];

⌨️ 快捷键说明

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