[vlc-devel] [PATCH] RealVideo Support

Wang Bo silencewang at msn.com
Fri May 23 04:59:47 CEST 2008


---
 configure.ac                       |    7 +-
 modules/access/rtsp/access.c       |  135 ++++++++--
 modules/access/rtsp/real.c         |  108 ++++----
 modules/access/rtsp/real_sdpplin.c |   86 +++---
 modules/access/rtsp/rtsp.c         |   58 +++--
 modules/codec/Modules.am           |    1 +
 modules/codec/realvideo.c          |  529 ++++++++++++++++++++++++++++++++++++
 modules/demux/real.c               |  416 ++++++++++++++++++++++++----
 8 files changed, 1152 insertions(+), 188 deletions(-)
 create mode 100755 modules/codec/realvideo.c

diff --git a/configure.ac b/configure.ac
index a8f6bef..57d3892 100644
--- a/configure.ac
+++ b/configure.ac
@@ -483,7 +483,7 @@ dnl Check for system libs needed
 need_libc=false
 
 dnl Check for usual libc functions
-AC_CHECK_FUNCS([gettimeofday strtod strtol strtof strtoll strtoull strsep isatty vasprintf asprintf swab sigrelse getpwuid_r memalign posix_memalign if_nametoindex atoll getenv putenv setenv gmtime_r ctime_r localtime_r lrintf daemon scandir fork bsearch lstat strlcpy strdup strndup strnlen atof lldiv posix_fadvise posix_madvise uselocale])
+AC_CHECK_FUNCS([gettimeofday strtod strtol strtof strtoll strtoull strsep isatty vasprintf asprintf swab sigrelse getpwuid_r memalign posix_memalign if_nametoindex atoll getenv putenv setenv gmtime_r ctime_r localtime_r lrintf daemon scandir fork bsearch lstat strlcpy strdup strndup strnlen atof lldiv posix_fadvise posix_madvise])
 AC_CHECK_FUNCS(strcasecmp,,[AC_CHECK_FUNCS(stricmp)])
 AC_CHECK_FUNCS(strncasecmp,,[AC_CHECK_FUNCS(strnicmp)])
 AC_CHECK_FUNCS(strcasestr,,[AC_CHECK_FUNCS(stristr)])
@@ -645,7 +645,7 @@ AC_CHECK_LIB(m,cos,[
   VLC_ADD_LIBS([adjust wave ripple psychedelic gradient a52tofloat32 dtstofloat32 x264 goom visual panoramix rotate noise grain],[-lm])
 ])
 AC_CHECK_LIB(m,pow,[
-  VLC_ADD_LIBS([ffmpeg ffmpegaltivec stream_out_transrate i420_rgb faad twolame equalizer spatializer param_eq libvlc vorbis freetype mod mpc dmo quicktime realaudio galaktos opengl],[-lm])
+  VLC_ADD_LIBS([ffmpeg ffmpegaltivec stream_out_transrate i420_rgb faad twolame equalizer spatializer param_eq libvlc vorbis freetype mod mpc dmo quicktime realaudio realvideo galaktos opengl],[-lm])
 ])
 AC_CHECK_LIB(m,sqrt,[
   VLC_ADD_LIBS([headphone_channel_mixer normvol speex mono colorthres extract],[-lm])
@@ -3439,6 +3439,7 @@ AC_ARG_ENABLE(real,
   [  --enable-real           Real audio module (default disabled)])
 if test "${enable_real}" = "yes"; then
   VLC_ADD_PLUGIN([realaudio])
+  VLC_ADD_PLUGIN([realvideo])
 fi
 
 dnl
@@ -5909,6 +5910,8 @@ AS_IF([test "${enable_loader}" = "yes"],
     VLC_ADD_LIBS([quicktime],[../../libs/loader/libloader.la -lpthread])
     VLC_ADD_CPPFLAGS([realaudio],[-I../../@top_srcdir@/libs/loader -DLOADER])
     VLC_ADD_LIBS([realaudio],[../../libs/loader/libloader.la])
+    VLC_ADD_CPPFLAGS([realvideo],[-I../../@top_srcdir@/libs/loader -DLOADER])
+    VLC_ADD_LIBS([realvideo],[../../libs/loader/libloader.la])
   ])
 
 AC_ARG_WITH(,[Components:])
diff --git a/modules/access/rtsp/access.c b/modules/access/rtsp/access.c
index b8b42b1..81c84ff 100644
--- a/modules/access/rtsp/access.c
+++ b/modules/access/rtsp/access.c
@@ -79,6 +79,7 @@ struct access_sys_t
 
     int fd;
 
+    int64_t  i_seekto;
     block_t *p_header;
 };
 
@@ -120,6 +121,26 @@ static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
     return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, true );
 }
 
+static int RtspReadAndSkip( void * p_userdata )
+{
+    access_t *p_access = (access_t *)p_userdata;
+    access_sys_t *p_sys = p_access->p_sys;
+    char buf[10240];
+    mtime_t time_wait = 500000;
+    int size, total=0;
+
+    /*msg_Dbg( p_access, "Try RtspReadAndSkip ");*/
+    do {
+        size = net_ReadNonBlock( p_access, p_sys->fd, 0, buf, 10240,  time_wait );
+        /*if ( size > 0 ) buf[size] = 0;
+        msg_Dbg( p_access, "skip %d byte [0]%c [0] %d %s", size, buf[0], buf[0], buf);*/
+        total += size;
+    }while( size > 0 );
+
+    msg_Dbg( p_access, "RtspReadAndSkip skip %d bytes ", total);
+    return 0;
+}
+
 static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
 {
     access_t *p_access = (access_t *)p_userdata;
@@ -127,12 +148,21 @@ static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
 
     char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, 0 );
 
-    //fprintf(stderr, "ReadLine: %s\n", psz);
-
-    if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
+    if ( psz )
+    {
+        if( strlen( psz ) > i_buffer - 1 )
+        {
+            msg_Warn( p_access, "Real RtspReadLine read a too long line");
+            strncpy( (char *)p_buffer, psz + strlen(psz) - i_buffer + 10, i_buffer - 1 );
+        }
+        else
+            strncpy( (char *)p_buffer, psz, i_buffer - 1 );
+    }
     else *p_buffer = 0;
 
-    free( psz );
+    p_buffer[ i_buffer -1 ] = '\0';
+
+    if( psz ) free( psz );
     return 0;
 }
 
@@ -158,6 +188,7 @@ static int Open( vlc_object_t *p_this )
     char *psz_server = 0;
     int i_result;
 
+    msg_Dbg( p_access, "Rtsp Real: open %s" , p_access->psz_access );
     if( !p_access->psz_access || (
         strncmp( p_access->psz_access, "rtsp", 4 ) &&
         strncmp( p_access->psz_access, "pnm", 3 )  &&
@@ -179,6 +210,7 @@ static int Open( vlc_object_t *p_this )
     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
     p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );
 
+    p_sys->i_seekto = 0;
     p_sys->p_header = 0;
     p_sys->p_rtsp->p_userdata = p_access;
     p_sys->p_rtsp->pf_connect = RtspConnect;
@@ -187,6 +219,7 @@ static int Open( vlc_object_t *p_this )
     p_sys->p_rtsp->pf_read_line = RtspReadLine;
     p_sys->p_rtsp->pf_write = RtspWrite;
 
+    msg_Dbg( p_access, "Rtsp Real: rtsp_connect %s" , p_access->psz_path );
     i_result = rtsp_connect( p_sys->p_rtsp, p_access->psz_path, 0 );
     if( i_result )
     {
@@ -209,7 +242,7 @@ static int Open( vlc_object_t *p_this )
             psz_server = strdup("unknown");
     }
 
-    if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
+    if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" )|| strstr( psz_server, "Vatata" ) )
     {
         uint32_t bandwidth = 10485800;
         rmff_header_t *h;
@@ -229,14 +262,16 @@ static int Open( vlc_object_t *p_this )
 
 
             msg_Err( p_access, "rtsp session can not be established" );
-            intf_UserFatal( p_access, false, _("Session failed"),
-                    _("The requested RTSP session could not be established.") );
             goto error;
         }
 
         p_sys->p_header = block_New( p_access, 4096 );
         p_sys->p_header->i_buffer =
-            rmff_dump_header( h, (char *)p_sys->p_header->p_buffer, 1024 );
+            rmff_dump_header( h, p_sys->p_header->p_buffer, 1024 );
+
+        p_sys->b_seekable = true;
+        p_sys->b_pace_control = true;
+
         rmff_free_header( h );
     }
     else
@@ -249,11 +284,11 @@ static int Open( vlc_object_t *p_this )
     var_Create( p_access, "realrtsp-caching",
                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
 
-    free( psz_server );
+    if( psz_server ) free( psz_server );
     return VLC_SUCCESS;
 
  error:
-    free( psz_server );
+    if( psz_server ) free( psz_server );
     Close( p_this );
     return VLC_EGENERIC;
 }
@@ -267,7 +302,7 @@ static void Close( vlc_object_t * p_this )
     access_sys_t *p_sys = p_access->p_sys;
 
     if( p_sys->p_rtsp ) rtsp_close( p_sys->p_rtsp );
-    free( p_sys->p_rtsp );
+    if( p_sys->p_rtsp ) free( p_sys->p_rtsp );
     free( p_sys );
 }
 
@@ -288,6 +323,26 @@ static block_t *BlockRead( access_t *p_access )
         return p_block;
     }
 
+    if ( p_sys->i_seekto != 0 )
+    {
+        char buf[100];
+
+        sprintf(buf, "Range: npt=%.6f-", (double) p_sys->i_seekto/1000000 );
+        msg_Dbg( p_access, "Seek to %"PRId64" %s ", p_sys->i_seekto , buf );
+
+        /*RtspReadAndSkip( p_access );*/
+        /*pause at first*/
+        rtsp_request_pause(  p_sys->p_rtsp, NULL );
+        RtspReadAndSkip( p_access );
+
+        /* replay*/
+        rtsp_schedule_field(p_sys->p_rtsp, buf );
+        rtsp_request_play(p_sys->p_rtsp,NULL);
+        p_sys->i_seekto = 0;
+
+    }
+
+    /*msg_Dbg( p_access, "real_get_rdt_chunk_header " );*/
     i_size = real_get_rdt_chunk_header( p_access->p_sys->p_rtsp, &pheader );
     if( i_size <= 0 ) return 0;
 
@@ -295,6 +350,8 @@ static block_t *BlockRead( access_t *p_access )
     p_block->i_buffer = real_get_rdt_chunk( p_access->p_sys->p_rtsp, &pheader,
                                             &p_block->p_buffer );
 
+    /*msg_Dbg( p_access, "real_get_rdt_chunk ok " );*/
+    /*need update p_sys->i_pos ? maybe i_pos is response to stream_Tell!*/
     return p_block;
 }
 
@@ -303,7 +360,28 @@ static block_t *BlockRead( access_t *p_access )
  *****************************************************************************/
 static int Seek( access_t *p_access, int64_t i_pos )
 {
+    access_sys_t *p_sys = p_access->p_sys;
+
+    p_sys->i_seekto = i_pos ;
     return VLC_SUCCESS;
+
+#if 0
+    /* ÕâÀï»áÓÐËÀËøÎÊÌâ?  maybe real_get_rdt_chunk_header will read the rtsp's answer in another thread? */
+    int64_t i_time = i_pos / 1000 ;
+    char buf[100];
+    sprintf(buf, "Range: npt=%.6f-", (double) i_time/1000 );
+
+    msg_Dbg( p_access, "Seek to "I64Fd" %s ", i_time, buf );
+
+    // pause at first
+    rtsp_request_pause(  p_sys->p_rtsp, NULL );
+
+    // replay
+    rtsp_schedule_field(p_sys->p_rtsp, buf );
+    rtsp_request_play(p_sys->p_rtsp,NULL);
+
+    return VLC_SUCCESS;
+#endif
 }
 
 /*****************************************************************************
@@ -311,7 +389,8 @@ static int Seek( access_t *p_access, int64_t i_pos )
  *****************************************************************************/
 static int Control( access_t *p_access, int i_query, va_list args )
 {
-    bool   *pb_bool;
+    access_sys_t *p_sys = p_access->p_sys;
+    bool         *pb_bool;
     int          *pi_int;
     int64_t      *pi_64;
 
@@ -320,15 +399,14 @@ static int Control( access_t *p_access, int i_query, va_list args )
         /* */
         case ACCESS_CAN_SEEK:
         case ACCESS_CAN_FASTSEEK:
-            pb_bool = (bool*)va_arg( args, bool* );
-            *pb_bool = false;//p_sys->b_seekable;
+            pb_bool = (bool *)va_arg( args, bool * );
+            *pb_bool = p_sys->b_seekable; // false;//p_sys->b_seekable;
             break;
 
         case ACCESS_CAN_PAUSE:
-            pb_bool = (bool*)va_arg( args, bool* );
+            pb_bool = (bool*)va_arg( args, bool * );
             *pb_bool = false;
             break;
-
         case ACCESS_CAN_CONTROL_PACE:
             pb_bool = (bool*)va_arg( args, bool* );
             *pb_bool = true;//p_sys->b_pace_control;
@@ -347,7 +425,29 @@ static int Control( access_t *p_access, int i_query, va_list args )
 
         /* */
         case ACCESS_SET_PAUSE_STATE:
-            /* Nothing to do */
+            pb_bool = (bool*)va_arg( args, bool* ); 
+//            break;
+
+            /* use this will be make sound bad */
+            if ( pb_bool )
+            {
+                rtsp_request_pause(  p_sys->p_rtsp, NULL );
+                RtspReadAndSkip( p_access );
+            }
+            else
+            {
+                //int64_t i_time = p_sys->i_pcr / 1000 ;
+                //char buf[100];
+                //sprintf(buf, "Range: npt=%.6f-", (double) i_time/1000 );
+
+                //msg_Dbg( p_access, "playing %s ", buf );
+
+                //rtsp_schedule_field(p_sys->p_rtsp, buf );
+                //msg_Dbg( p_access, "playing" );
+                RtspReadAndSkip( p_access );
+                rtsp_request_play(p_sys->p_rtsp,NULL);
+            }
+
             break;
 
         case ACCESS_GET_TITLE_INFO:
@@ -355,7 +455,6 @@ static int Control( access_t *p_access, int i_query, va_list args )
         case ACCESS_SET_SEEKPOINT:
         case ACCESS_SET_PRIVATE_ID_STATE:
         case ACCESS_GET_META:
-        case ACCESS_GET_CONTENT_TYPE:
             return VLC_EGENERIC;
 
         default:
diff --git a/modules/access/rtsp/real.c b/modules/access/rtsp/real.c
index da499ec..2de7c0f 100644
--- a/modules/access/rtsp/real.c
+++ b/modules/access/rtsp/real.c
@@ -22,10 +22,8 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include <stdio.h>
+#include <string.h>
 
 #include <vlc/vlc.h>
 
@@ -162,37 +160,37 @@ static void hash(char *field, char *param)
   b = ((b << 0x17) | (b >> 0x09)) + c;
 
   a = ((~d | b) ^ c)  + LE_32((param+0x00)) + a - 0x0BD6DDBC;
-  a = ((a << 0x06) | (a >> 0x1a)) + b;
+  a = ((a << 0x06) | (a >> 0x1a)) + b; 
   d = ((~c | a) ^ b)  + LE_32((param+0x1c)) + d + 0x432AFF97;
-  d = ((d << 0x0a) | (d >> 0x16)) + a;
+  d = ((d << 0x0a) | (d >> 0x16)) + a; 
   c = ((~b | d) ^ a)  + LE_32((param+0x38)) + c - 0x546BDC59;
-  c = ((c << 0x0f) | (c >> 0x11)) + d;
+  c = ((c << 0x0f) | (c >> 0x11)) + d; 
   b = ((~a | c) ^ d)  + LE_32((param+0x14)) + b - 0x036C5FC7;
-  b = ((b << 0x15) | (b >> 0x0b)) + c;
+  b = ((b << 0x15) | (b >> 0x0b)) + c; 
   a = ((~d | b) ^ c)  + LE_32((param+0x30)) + a + 0x655B59C3;
-  a = ((a << 0x06) | (a >> 0x1a)) + b;
+  a = ((a << 0x06) | (a >> 0x1a)) + b; 
   d = ((~c | a) ^ b)  + LE_32((param+0x0C)) + d - 0x70F3336E;
-  d = ((d << 0x0a) | (d >> 0x16)) + a;
+  d = ((d << 0x0a) | (d >> 0x16)) + a; 
   c = ((~b | d) ^ a)  + LE_32((param+0x28)) + c - 0x00100B83;
-  c = ((c << 0x0f) | (c >> 0x11)) + d;
+  c = ((c << 0x0f) | (c >> 0x11)) + d; 
   b = ((~a | c) ^ d)  + LE_32((param+0x04)) + b - 0x7A7BA22F;
-  b = ((b << 0x15) | (b >> 0x0b)) + c;
+  b = ((b << 0x15) | (b >> 0x0b)) + c; 
   a = ((~d | b) ^ c)  + LE_32((param+0x20)) + a + 0x6FA87E4F;
-  a = ((a << 0x06) | (a >> 0x1a)) + b;
+  a = ((a << 0x06) | (a >> 0x1a)) + b; 
   d = ((~c | a) ^ b)  + LE_32((param+0x3c)) + d - 0x01D31920;
-  d = ((d << 0x0a) | (d >> 0x16)) + a;
+  d = ((d << 0x0a) | (d >> 0x16)) + a; 
   c = ((~b | d) ^ a)  + LE_32((param+0x18)) + c - 0x5CFEBCEC;
-  c = ((c << 0x0f) | (c >> 0x11)) + d;
+  c = ((c << 0x0f) | (c >> 0x11)) + d; 
   b = ((~a | c) ^ d)  + LE_32((param+0x34)) + b + 0x4E0811A1;
-  b = ((b << 0x15) | (b >> 0x0b)) + c;
+  b = ((b << 0x15) | (b >> 0x0b)) + c; 
   a = ((~d | b) ^ c)  + LE_32((param+0x10)) + a - 0x08AC817E;
-  a = ((a << 0x06) | (a >> 0x1a)) + b;
+  a = ((a << 0x06) | (a >> 0x1a)) + b; 
   d = ((~c | a) ^ b)  + LE_32((param+0x2c)) + d - 0x42C50DCB;
-  d = ((d << 0x0a) | (d >> 0x16)) + a;
+  d = ((d << 0x0a) | (d >> 0x16)) + a; 
   c = ((~b | d) ^ a)  + LE_32((param+0x08)) + c + 0x2AD7D2BB;
-  c = ((c << 0x0f) | (c >> 0x11)) + d;
+  c = ((c << 0x0f) | (c >> 0x11)) + d; 
   b = ((~a | c) ^ d)  + LE_32((param+0x24)) + b - 0x14792C6F;
-  b = ((b << 0x15) | (b >> 0x0b)) + c;
+  b = ((b << 0x15) | (b >> 0x0b)) + c; 
 
   lprintf("hash output: %x %x %x %x\n", a, b, c, d);
 
@@ -212,8 +210,8 @@ static void call_hash (char *key, char *challenge, int len) {
   uint8_t *ptr1, *ptr2;
   uint32_t a, b, c, d, tmp;
 
-  ptr1=(uint8_t*)(key+16);
-  ptr2=(uint8_t*)(key+20);
+  ptr1=(key+16);
+  ptr2=(key+20);
 
   a = LE_32(ptr1);
   b = (a >> 3) & 0x3f;
@@ -340,7 +338,7 @@ void real_calc_response_and_checksum (char *response, char *chksum, char *challe
 
   if (xor_table != NULL)
   {
-    table_len = strlen((char *)xor_table);
+    table_len = strlen(xor_table);
 
     if (table_len > 56) table_len=56;
 
@@ -435,7 +433,6 @@ rmff_header_t *real_parse_sdp(char *data, char **stream_rules, uint32_t bandwidt
   int duration=0;
 
   if( !data ) return NULL;
-
   desc=sdpplin_parse(data);
   if( !desc ) return NULL;
 
@@ -463,12 +460,16 @@ rmff_header_t *real_parse_sdp(char *data, char **stream_rules, uint32_t bandwidt
   lprintf("number of streams: %u\n", desc->stream_count);
 
   for (i=0; i<desc->stream_count; i++) {
-
     int j=0;
     int n;
     char b[64];
     int rulematches[16];
 
+	if ( ! desc->stream[i]->asm_rule_book )
+	{
+		goto error;
+	}
+
     lprintf("calling asmrp_match with:\n%s\n%u\n", desc->stream[i]->asm_rule_book, bandwidth);
 
     n=asmrp_match(desc->stream[i]->asm_rule_book, bandwidth, rulematches);
@@ -480,7 +481,7 @@ rmff_header_t *real_parse_sdp(char *data, char **stream_rules, uint32_t bandwidt
 
     if (!desc->stream[i]->mlti_data) {
       len = 0;
-      free( buf );
+      if( buf ) free( buf );
       buf = NULL;
     } else
       len=select_mlti_data(desc->stream[i]->mlti_data,
@@ -531,13 +532,13 @@ rmff_header_t *real_parse_sdp(char *data, char **stream_rules, uint32_t bandwidt
   rmff_fix_header(header);
 
   if( desc ) sdpplin_free( desc );
-  free( buf );
+  if( buf ) free(buf);
   return header;
 
 error:
   if( desc ) sdpplin_free( desc );
   if( header ) rmff_free_header( header );
-  free( buf );
+  if( buf ) free( buf );
   return NULL;
 }
 
@@ -591,7 +592,14 @@ int real_get_rdt_chunk_header(rtsp_client_t *rtsp_session, rmff_pheader_t *ph) {
   ph->stream_number=(flags1>>1)&1;
   ph->timestamp=ts;
   ph->reserved=0;
-  ph->flags=0;      /* TODO: determine keyframe flag and insert here? */
+
+  if ( header[7] == 0xc0 )
+	ph->flags=2;
+  else
+	ph->flags=0;      
+
+	  /*fprintf(stderr, "real_get_rdt_chunk_header: is key frame? %x timestamp %d\n",  header[7], ts );*/
+
   return size;
 }
 
@@ -599,8 +607,8 @@ int real_get_rdt_chunk(rtsp_client_t *rtsp_session, rmff_pheader_t *ph,
                        unsigned char **buffer) {
 
   int n;
-  rmff_dump_pheader(ph, (char*)*buffer);
-  n=rtsp_read_data(rtsp_session, (uint8_t*)(*buffer + 12), ph->length - 12);
+  rmff_dump_pheader(ph, *buffer);
+  n=rtsp_read_data(rtsp_session, *buffer + 12, ph->length - 12);
   return (n <= 0) ? 0 : n+12;
 }
 
@@ -620,6 +628,7 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
   unsigned int size;
   int status;
 
+
   /* get challenge */
   challenge1=strdup(rtsp_search_answers(rtsp_session,"RealChallenge1"));
   lprintf("Challenge1: %s\n", challenge1);
@@ -635,17 +644,20 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
   rtsp_schedule_field(rtsp_session, "Language: en-US");
   rtsp_schedule_field(rtsp_session, "Require: com.real.retain-entity-for-setup");
 
+
   status=rtsp_request_describe(rtsp_session,NULL);
+
+
   if ( status<200 || status>299 ) {
     char *alert=rtsp_search_answers(rtsp_session,"Alert");
     if (alert) {
         lprintf("real: got message from server:\n%s\n", alert);
     }
     printf( "bou\n");
-    rtsp_send_ok( rtsp_session );
-    free( challenge1 );
-    free( alert );
-    free( buf );
+    rtsp_send_ok(rtsp_session);
+    if( challenge1 ) free(challenge1);
+    if( alert ) free(alert);
+    if( buf ) free(buf);
     return NULL;
   }
 
@@ -672,10 +684,10 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
   description = (char*)malloc(sizeof(char)*(size+1));
   if( !description )
     goto error;
-  if( rtsp_read_data(rtsp_session, (uint8_t*)description, size) <= 0)
+  if( rtsp_read_data(rtsp_session, description, size) <= 0)
     goto error;
   description[size]=0;
-  //fprintf(stderr, "%s", description);
+//  fprintf(stderr, "descript: %s", description);
 
   /* parse sdp (sdpplin) and create a header and a subscribe string */
   subscribe = (char *) malloc(sizeof(char)*256);
@@ -690,7 +702,7 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
   rmff_fix_header(h);
 
 #if 0
-  fprintf("Title: %s\nCopyright: %s\nAuthor: %s\nStreams: %i\n",
+  fprintf(stderr, "Title: %s\nCopyright: %s\nAuthor: %s\nStreams: %i\n",
       h->cont->title, h->cont->copyright, h->cont->author, h->prop->num_streams);
 #endif
 
@@ -724,19 +736,19 @@ rmff_header_t  *real_setup_and_get_header(rtsp_client_t *rtsp_session, int bandw
   rtsp_schedule_field(rtsp_session, "Range: npt=0-");
   rtsp_request_play(rtsp_session,NULL);
 
-  free( challenge1 );
-  free( session_id );
-  free( description );
-  free( subscribe );
-  free( buf );
+  if( challenge1 ) free( challenge1 );
+  if( session_id ) free( session_id );
+  if( description ) free(description);
+  if( subscribe ) free(subscribe);
+  if( buf ) free(buf);
   return h;
 
 error:
   if( h ) rmff_free_header( h );
-  free( challenge1 );
-  free( session_id );
-  free( description );
-  free( subscribe );
-  free( buf );
+  if( challenge1 ) free( challenge1 );
+  if( session_id ) free( session_id );
+  if( description ) free(description);
+  if( subscribe ) free(subscribe);
+  if( buf ) free(buf);
   return NULL;
 }
diff --git a/modules/access/rtsp/real_sdpplin.c b/modules/access/rtsp/real_sdpplin.c
index 229501c..e419b47 100644
--- a/modules/access/rtsp/real_sdpplin.c
+++ b/modules/access/rtsp/real_sdpplin.c
@@ -96,7 +96,7 @@ static int filter(const char *in, const char *filter, char **out, size_t outlen)
 
   if (!in) return 0;
 
-  len = (strchr(in,'\n')) ? (size_t)(strchr(in,'\n')-in) : strlen(in);
+  len = (strchr(in,'\n')) ? strchr(in,'\n')-in : strlen(in);
   if (!strncmp(in,filter,flen)) {
     if(in[flen]=='"') flen++;
     if(in[len-1]==13) len--;
@@ -146,8 +146,8 @@ static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
             lprintf("stream id out of bound: %lu\n", tmp);
         else
             desc->stream_id=tmp;
-        handled=1;
-        *data=nl(*data);
+      handled=1;
+      *data=nl(*data);
     }
     if(filter(*data,"a=MaxBitRate:integer;",&buf, BUFLEN)) {
       desc->max_bit_rate=atoi(buf);
@@ -216,14 +216,14 @@ static sdpplin_stream_t *sdpplin_parse_stream(char **data) {
       *data=nl(*data);
     }
   }
-  free( buf );
-  free( decoded) ;
+  if( buf ) free(buf);
+  if( decoded )free(decoded);
   return desc;
 
 error:
-  free( decoded );
-  free( desc );
-  free( buf );
+  if( decoded ) free(decoded);
+  if( desc ) free( desc );
+  if( buf ) free( buf );
   return NULL;
 }
 
@@ -246,7 +246,6 @@ sdpplin_t *sdpplin_parse(char *data) {
     free( desc );
     return NULL;
   }
-
   desc->stream = NULL;
 
   memset(desc, 0, sizeof(sdpplin_t));
@@ -260,6 +259,13 @@ sdpplin_t *sdpplin_parse(char *data) {
             continue;
         }
         stream=sdpplin_parse_stream(&data);
+		if ( stream->stream_id < 0 || stream->stream_id > 2 )
+		{
+			fprintf(stderr, "fatal error, sdp's streamid is wrong : %d !",stream->stream_id );
+			free( buf );
+			free( desc );
+			return NULL;
+		}
         lprintf("got data for stream id %u\n", stream->stream_id);
         if ( stream->stream_id >= desc->stream_count )
             lprintf("stream id %u is greater than stream count %u\n", stream->stream_id, desc->stream_count);
@@ -308,9 +314,9 @@ sdpplin_t *sdpplin_parse(char *data) {
             lprintf("stream count out of bound: %lu\n", tmp);
         else
             desc->stream_count = tmp;
-        desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
-        handled=1;
-        data=nl(data);
+      desc->stream = malloc(sizeof(sdpplin_stream_t*)*desc->stream_count);
+      handled=1;
+      data=nl(data);
     }
     if(filter(data,"a=Flags:integer;",&buf, BUFLEN)) {
       desc->flags=atoi(buf);
@@ -329,8 +335,8 @@ sdpplin_t *sdpplin_parse(char *data) {
     }
   }
 
-  free( decoded );
-  free( buf );
+  free(decoded);
+  free(buf);
   return desc;
 }
 
@@ -342,36 +348,36 @@ void sdpplin_free(sdpplin_t *description) {
 
   for( i=0; i<description->stream_count; i++ ) {
     if( description->stream[i] ) {
-      free( description->stream[i]->id );
-      free( description->stream[i]->bandwidth );
-      free( description->stream[i]->range );
-      free( description->stream[i]->length );
-      free( description->stream[i]->rtpmap );
-      free( description->stream[i]->mimetype );
-      free( description->stream[i]->stream_name );
-      free( description->stream[i]->mime_type );
-      free( description->stream[i]->mlti_data );
-      free( description->stream[i]->rmff_flags );
-      free( description->stream[i]->asm_rule_book );
+      if( description->stream[i]->id ) free( description->stream[i]->id );
+      if( description->stream[i]->bandwidth ) free( description->stream[i]->bandwidth );
+      if( description->stream[i]->range ) free( description->stream[i]->range );
+      if( description->stream[i]->length ) free( description->stream[i]->length );
+      if( description->stream[i]->rtpmap ) free( description->stream[i]->rtpmap );
+      if( description->stream[i]->mimetype ) free( description->stream[i]->mimetype );
+      if( description->stream[i]->stream_name ) free( description->stream[i]->stream_name );
+      if( description->stream[i]->mime_type ) free( description->stream[i]->mime_type );
+      if( description->stream[i]->mlti_data ) free( description->stream[i]->mlti_data );
+      if( description->stream[i]->rmff_flags ) free( description->stream[i]->rmff_flags );
+      if( description->stream[i]->asm_rule_book ) free( description->stream[i]->asm_rule_book );
       free( description->stream[i] );
     }
   }
   if( description->stream_count ) free( description->stream );
 
-  free( description->owner );
-  free( description->session_name );
-  free( description->session_info );
-  free( description->uri );
-  free( description->email );
-  free( description->phone );
-  free( description->connection );
-  free( description->bandwidth );
-  free( description->title );
-  free( description->author );
-  free( description->copyright );
-  free( description->keywords );
-  free( description->asm_rule_book );
-  free( description->abstract );
-  free( description->range );
+  if( description->owner ) free( description->owner );
+  if( description->session_name ) free( description->session_name );
+  if( description->session_info ) free( description->session_info );
+  if( description->uri ) free( description->uri );
+  if( description->email ) free( description->email );
+  if( description->phone ) free( description->phone );
+  if( description->connection ) free( description->connection );
+  if( description->bandwidth ) free( description->bandwidth );
+  if( description->title ) free( description->title );
+  if( description->author ) free( description->author );
+  if( description->copyright ) free( description->copyright );
+  if( description->keywords ) free( description->keywords );
+  if( description->asm_rule_book ) free( description->asm_rule_book );
+  if( description->abstract ) free( description->abstract );
+  if( description->range ) free( description->range );
   free(description);
 }
diff --git a/modules/access/rtsp/rtsp.c b/modules/access/rtsp/rtsp.c
index 6730a93..4972b3f 100644
--- a/modules/access/rtsp/rtsp.c
+++ b/modules/access/rtsp/rtsp.c
@@ -27,6 +27,8 @@
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
+#include <stdio.h>
+#include <stdlib.h>
 
 #include <vlc/vlc.h>
 
@@ -91,9 +93,8 @@ static char *rtsp_get( rtsp_client_t *rtsp )
   char *psz_buffer = malloc( BUF_SIZE );
   char *psz_string = NULL;
 
-  if( rtsp->pf_read_line( rtsp->p_userdata, (uint8_t*)psz_buffer, (unsigned int)BUF_SIZE ) >= 0 )
+  if( rtsp->pf_read_line( rtsp->p_userdata, psz_buffer, (unsigned int)BUF_SIZE ) >= 0 )
   {
-    //printf( "<< '%s'\n", psz_buffer );
       psz_string = strdup( psz_buffer );
   }
 
@@ -115,8 +116,8 @@ static int rtsp_put( rtsp_client_t *rtsp, const char *psz_string )
     strcpy( psz_buffer, psz_string );
     psz_buffer[i_buffer] = '\r'; psz_buffer[i_buffer+1] = '\n';
     psz_buffer[i_buffer+2] = 0;
-
-    i_ret = rtsp->pf_write( rtsp->p_userdata, (uint8_t*)psz_buffer, i_buffer + 2 );
+    
+    i_ret = rtsp->pf_write( rtsp->p_userdata, psz_buffer, i_buffer + 2 );
 
     free( psz_buffer );
     return i_ret;
@@ -161,6 +162,7 @@ static int rtsp_send_request( rtsp_client_t *rtsp, const char *psz_type,
     char *psz_buffer;
     int i_ret;
 
+
     psz_buffer = malloc( strlen(psz_type) + strlen(psz_what) +
                          sizeof("RTSP/1.0") + 2 );
 
@@ -175,6 +177,7 @@ static int rtsp_send_request( rtsp_client_t *rtsp, const char *psz_type,
             ppsz_payload++;
         }
     rtsp_put( rtsp, "" );
+
     rtsp_unschedule_all( rtsp );
 
     return i_ret;
@@ -215,6 +218,13 @@ static int rtsp_get_answers( rtsp_client_t *rtsp )
 
     answer = rtsp_get( rtsp );
     if( !answer ) return 0;
+    if ( answer[0] == '$' ) 
+    {
+        free( answer );
+        rtsp_free_answers( rtsp );
+        return 0;
+    }
+
     code = rtsp_get_status_code( rtsp, answer );
     free( answer );
 
@@ -234,13 +244,14 @@ static int rtsp_get_answers( rtsp_client_t *rtsp )
             //       answer_seq, rtsp->p_private->cseq );
 
               rtsp->p_private->cseq = answer_seq;
+              rtsp->p_private->cseq ++;
           }
       }
       if( !strncasecmp( answer, "Server:", 7 ) )
       {
           char *buf = malloc( strlen(answer) );
           sscanf( answer, "%*s %s", buf );
-          free( rtsp->p_private->server );
+          if( rtsp->p_private->server ) free( rtsp->p_private->server );
           rtsp->p_private->server = buf;
       }
       if( !strncasecmp( answer, "Session:", 8 ) )
@@ -251,7 +262,7 @@ static int rtsp_get_answers( rtsp_client_t *rtsp )
           {
               if( strcmp( buf, rtsp->p_private->session ) )
               {
-                  //fprintf( stderr,
+                  //fprintf( stderr, 
                   //         "rtsp: warning: setting NEW session: %s\n", buf );
                   free( rtsp->p_private->session );
                   rtsp->p_private->session = strdup( buf );
@@ -379,12 +390,22 @@ int rtsp_request_play( rtsp_client_t *rtsp, const char *what )
                  rtsp->p_private->port, rtsp->p_private->path );
     }
 
+    rtsp_schedule_standard( rtsp );
+
     rtsp_send_request( rtsp, "PLAY", buf );
     free( buf );
 
     return rtsp_get_answers( rtsp );
 }
 
+int rtsp_request_pause( rtsp_client_t *rtsp, const char *what )
+{
+    rtsp_schedule_field( rtsp, "User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)" );
+    //rtsp_schedule_standard( rtsp );
+    rtsp_send_request( rtsp, "PAUSE", "" );
+    return rtsp_get_answers( rtsp );
+}
+
 int rtsp_request_tearoff( rtsp_client_t *rtsp, const char *what )
 {
     rtsp_send_request( rtsp, "TEAROFF", what );
@@ -394,14 +415,13 @@ int rtsp_request_tearoff( rtsp_client_t *rtsp, const char *what )
 /*
  * read opaque data from stream
  */
-
 int rtsp_read_data( rtsp_client_t *rtsp, uint8_t *buffer, unsigned int size )
 {
     int i, seq;
 
     if( size >= 4 )
     {
-        i = rtsp->pf_read( rtsp->p_userdata, (uint8_t*)buffer, (unsigned int) 4 );
+        i = rtsp->pf_read( rtsp->p_userdata, buffer, (unsigned int) 4 );
         if( i < 4 ) return i;
 
         if( buffer[0]=='S' && buffer[1]=='E' && buffer[2]=='T' &&
@@ -495,7 +515,7 @@ int rtsp_connect( rtsp_client_t *rtsp, const char *psz_mrl,
     colon = strchr( mrl_ptr, ':' );
 
     if( !slash ) slash = mrl_ptr + strlen(mrl_ptr) + 1;
-    if( !colon ) colon = slash;
+    if( !colon ) colon = slash; 
     if( colon > slash ) colon = slash;
 
     pathbegin = slash - mrl_ptr;
@@ -548,7 +568,7 @@ int rtsp_connect( rtsp_client_t *rtsp, const char *psz_mrl,
 }
 
 /*
- * closes an rtsp connection
+ * closes an rtsp connection 
  */
 
 void rtsp_close( rtsp_client_t *rtsp )
@@ -559,12 +579,12 @@ void rtsp_close( rtsp_client_t *rtsp )
         rtsp->pf_disconnect( rtsp->p_userdata );
     }
 
-    free( rtsp->p_private->path );
-    free( rtsp->p_private->host );
-    free( rtsp->p_private->mrl );
-    free( rtsp->p_private->session );
-    free( rtsp->p_private->user_agent );
-    free( rtsp->p_private->server );
+    if( rtsp->p_private->path ) free( rtsp->p_private->path );
+    if( rtsp->p_private->host ) free( rtsp->p_private->host );
+    if( rtsp->p_private->mrl ) free( rtsp->p_private->mrl );
+    if( rtsp->p_private->session ) free( rtsp->p_private->session );
+    if( rtsp->p_private->user_agent ) free( rtsp->p_private->user_agent );
+    if( rtsp->p_private->server ) free( rtsp->p_private->server );
     rtsp_free_answers( rtsp );
     rtsp_unschedule_all( rtsp );
     free( rtsp->p_private );
@@ -604,7 +624,7 @@ char *rtsp_search_answers( rtsp_client_t *rtsp, const char *tag )
 
 void rtsp_set_session( rtsp_client_t *rtsp, const char *id )
 {
-    free( rtsp->p_private->session );
+    if( rtsp->p_private->session ) free( rtsp->p_private->session );
     rtsp->p_private->session = strdup(id);
 }
 
@@ -634,7 +654,7 @@ void rtsp_schedule_field( rtsp_client_t *rtsp, const char *string )
 }
 
 /*
- * removes the first scheduled field which prefix matches string.
+ * removes the first scheduled field which prefix matches string. 
  */
 
 void rtsp_unschedule_field( rtsp_client_t *rtsp, const char *string )
@@ -647,7 +667,7 @@ void rtsp_unschedule_field( rtsp_client_t *rtsp, const char *string )
     {
       if( !strncmp(*ptr, string, strlen(string)) ) break;
     }
-    free( *ptr );
+    if( *ptr ) free( *ptr );
     ptr++;
     do
     {
diff --git a/modules/codec/Modules.am b/modules/codec/Modules.am
index f790127..b3cf15c 100644
--- a/modules/codec/Modules.am
+++ b/modules/codec/Modules.am
@@ -28,6 +28,7 @@ SOURCES_svcdsub = svcdsub.c
 SOURCES_cvdsub = cvdsub.c
 SOURCES_fake = fake.c
 SOURCES_realaudio = realaudio.c
+SOURCES_realvideo = realvideo.c
 SOURCES_sdl_image = sdl_image.c
 SOURCES_zvbi = zvbi.c
 SOURCES_csri = csri.c
diff --git a/modules/codec/realvideo.c b/modules/codec/realvideo.c
new file mode 100755
index 0000000..65b4dc3
--- /dev/null
+++ b/modules/codec/realvideo.c
@@ -0,0 +1,529 @@
+
+/*****************************************************************************
+ * realaudio.c: a realaudio decoder that uses the realaudio library/dll
+ *****************************************************************************
+ * Copyright (C) 2005 the VideoLAN team
+ * $Id$
+ *
+ * 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
+ *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+/*
+#include <vlc/vlc.h>
+#include <vlc/aout.h>
+#include <vlc/vout.h>
+#include <vlc/decoder.h>
+#ifndef WIN32
+#include <dlfcn.h>
+#endif
+
+#if defined(LOADER) && defined(WIN32)
+#include "wine/windef.h"
+#include "ldt_keeper.h"
+
+HMODULE   WINAPI LoadLibraryA(LPCSTR);
+FARPROC   WINAPI GetProcAddress(HMODULE,LPCSTR);
+int       WINAPI FreeLibrary(HMODULE);
+#endif
+*/
+
+#include <vlc/vlc.h>
+#include <vlc_plugin.h>
+#include <vlc_aout.h>
+#include <vlc_vout.h>
+#include <vlc_codec.h>
+
+#ifdef LOADER
+/* Need the w32dll loader from mplayer */
+#   include <wine/winerror.h>
+#   include <ldt_keeper.h>
+#   include <wine/windef.h>
+
+void *WINAPI LoadLibraryA( char *name );
+void *WINAPI GetProcAddress( void *handle, char *func );
+int WINAPI FreeLibrary( void *handle );
+#endif
+
+#ifndef WINAPI
+#   define WINAPI
+#endif
+
+#if defined(HAVE_DL_DLOPEN)
+#   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
+#       include <dlfcn.h>
+#   endif
+#   if defined(HAVE_SYS_DL_H)
+#       include <sys/dl.h>
+#   endif
+#endif
+
+typedef struct cmsg_data_s
+{
+    uint32_t data1;
+    uint32_t data2;
+    uint32_t* dimensions;
+} cmsg_data_t;
+
+typedef struct transform_in_s
+{
+    uint32_t len;
+    uint32_t unknown1;
+    uint32_t chunks;
+    uint32_t* extra;
+    uint32_t unknown2;
+    uint32_t timestamp;
+} transform_in_t;
+
+// copypaste from demux_real.c - it should match to get it working!
+typedef struct dp_hdr_s
+{
+    uint32_t chunks;    // number of chunks
+    uint32_t timestamp;    // timestamp from packet header
+    uint32_t len;    // length of actual data
+    uint32_t chunktab;    // offset to chunk offset array
+} dp_hdr_t;
+
+/* we need exact positions */
+struct rv_init_t
+{
+    short unk1;
+    short w;
+    short h;
+    short unk3;
+    int unk2;
+    int subformat;
+    int unk5;
+    int format;
+} rv_init_t;
+
+struct decoder_sys_t
+{
+    /* library */
+#ifdef LOADER
+    ldt_fs_t    *ldt_fs;
+#endif
+    void        *handle;
+    void         *rv_handle;
+    int          inited;
+    char         *plane;
+};
+
+int dll_type = 1;
+#ifdef WIN32
+const char *g_decode_path="plugins\\drv43260.dll";
+#else
+const char *g_decode_path="../lib/vlc/codec/drv4.so.6.0";
+#endif
+
+static unsigned long (*rvyuv_custom_message)(cmsg_data_t* ,void*);
+static unsigned long (*rvyuv_free)(void*);
+static unsigned long (*rvyuv_init)(void*, void*); // initdata,context
+static unsigned long (*rvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
+#ifdef WIN32
+static unsigned long WINAPI (*wrvyuv_custom_message)(cmsg_data_t* ,void*);
+static unsigned long WINAPI (*wrvyuv_free)(void*);
+static unsigned long WINAPI (*wrvyuv_init)(void*, void*); // initdata,context
+static unsigned long WINAPI (*wrvyuv_transform)(char*, char*,transform_in_t*,unsigned int*,void*);
+#endif
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+static int  Open ( vlc_object_t * );
+static void Close( vlc_object_t * );
+
+//static int  OpenPacketizer( vlc_object_t * );
+static picture_t *DecodeVideo( decoder_t *, block_t ** );
+
+vlc_module_begin();
+    set_description( N_("RealVideo library decoder") );
+    set_capability( "decoder", 10 );
+    set_category( CAT_INPUT );
+    set_subcategory( SUBCAT_INPUT_VCODEC );
+    set_callbacks( Open, Close );
+vlc_module_end();
+
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+
+#ifdef WIN32
+static void * load_syms(decoder_t *p_dec, char *path) 
+{
+    void *handle;
+
+    msg_Dbg( p_dec, "opening win32 dll '%s'\n", path);
+#ifdef LOADER
+    Setup_LDT_Keeper();
+#endif
+    handle = LoadLibraryA(path);
+    msg_Dbg( p_dec, "win32 real codec handle=%p  \n",handle);
+    if (!handle)
+    {
+        msg_Err( p_dec, "Error loading dll\n");
+        return NULL;
+    }
+
+    wrvyuv_custom_message = GetProcAddress(handle, "RV20toYUV420CustomMessage");
+    wrvyuv_free = GetProcAddress(handle, "RV20toYUV420Free");
+    wrvyuv_init = GetProcAddress(handle, "RV20toYUV420Init");
+    wrvyuv_transform = GetProcAddress(handle, "RV20toYUV420Transform");
+
+    if (wrvyuv_custom_message && wrvyuv_free && wrvyuv_init && wrvyuv_transform)
+    {
+        dll_type = 1;
+        return handle;
+    }
+    msg_Err( p_dec, "Error resolving symbols! (version incompatibility?)\n");
+    FreeLibrary(handle);
+    return NULL; // error
+}
+#else
+void * load_syms_linux(decoder_t *p_dec, char *path) 
+{
+    void *handle;
+
+    msg_Dbg( p_dec, "opening shared obj '%s'\n", path);
+
+    handle = dlopen (path, RTLD_LAZY);
+    if (!handle) 
+    {
+        msg_Err( p_dec,"Error: %s\n",dlerror());
+        return NULL;
+    }
+
+    rvyuv_custom_message = dlsym(handle, "RV20toYUV420CustomMessage");
+    rvyuv_free = dlsym(handle, "RV20toYUV420Free");
+    rvyuv_init = dlsym(handle, "RV20toYUV420Init");
+    rvyuv_transform = dlsym(handle, "RV20toYUV420Transform");
+
+    if(rvyuv_custom_message && rvyuv_free && rvyuv_init && rvyuv_transform)
+    {
+        dll_type = 0;
+        return handle;
+    }
+
+    msg_Err( p_dec,"Error resolving symbols! (version incompatibility?)\n");
+    dlclose(handle);
+    return 0;
+}
+#endif
+
+static int InitVideo(decoder_t *p_dec)
+{
+    int result;
+    struct rv_init_t init_data;
+        char fcc[4];
+    vlc_value_t   lockval;
+        int  i_vide = p_dec->fmt_in.i_extra;
+        unsigned int *p_vide = p_dec->fmt_in.p_extra;
+    decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
+    memset(p_sys,0,sizeof( decoder_sys_t ) );
+
+        if( i_vide < 8 )
+         {
+            msg_Err( p_dec, "missing extra info" );
+            free( p_sys );
+            return VLC_EGENERIC;
+         }
+        if (p_sys->plane) free(p_sys->plane);
+    p_sys->plane = malloc (p_dec->fmt_in.video.i_width*p_dec->fmt_in.video.i_height*3/2 + 1024 );
+    if (NULL == p_sys->plane)
+    {
+        msg_Err( p_dec, "cannot alloc plane buffer" );
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+
+    p_dec->p_sys = p_sys;
+        p_dec->pf_decode_video = DecodeVideo;
+
+        memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
+    init_data.unk1 = 11;
+    init_data.w = p_dec->fmt_in.video.i_width ;
+    init_data.h = p_dec->fmt_in.video.i_height ;
+    init_data.unk3 = 0;
+    init_data.unk2 = 0;
+    init_data.subformat = (unsigned int*)p_vide[0];
+    init_data.unk5 = 1;
+    init_data.format = (unsigned int*)p_vide[1];
+    
+    /* first try to load linux dlls, if failed and we're supporting win32 dlls,
+       then try to load the windows ones */
+#ifdef WIN32
+    if ( NULL== (p_sys->rv_handle = load_syms(p_dec, g_decode_path)) )
+#else
+    if ( NULL== (p_sys->rv_handle = load_syms_linux(p_dec, g_decode_path)) )
+#endif
+    {
+        msg_Err( p_dec, "Cannot load real decoder library: %s",  g_decode_path);
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+
+    var_Get( p_dec->p_libvlc, "rm_mutex", &lockval );
+        vlc_mutex_lock( lockval.p_address );
+
+    p_sys->handle=NULL;
+    #ifdef WIN32
+    if (dll_type == 1)
+        result=(*wrvyuv_init)(&init_data, &p_sys->handle);
+    else
+    #endif
+        result=(*rvyuv_init)(&init_data, &p_sys->handle);
+    if (result)
+    {
+        msg_Err( p_dec, "Cannot Init real decoder library: %s",  g_decode_path);
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+    // setup rv30 codec (codec sub-type and image dimensions):
+    //if ( p_dec->fmt_in.i_codec == VLC_FOURCC('R','V','3','0') )
+    if (p_vide[1]>=0x20200002)
+    {
+        int i, cmsg_cnt;
+        uint32_t cmsg24[16]={p_dec->fmt_in.video.i_width,p_dec->fmt_in.video.i_height};
+        cmsg_data_t cmsg_data={0x24,1+(p_vide[1]&7), &cmsg24[0]};
+        cmsg_cnt = (p_vide[1]&7)*2;
+        if (i_vide - 8 < cmsg_cnt) {
+                    cmsg_cnt = i_vide - 8;
+        }
+        for (i = 0; i < cmsg_cnt; i++)
+            cmsg24[2+i] = p_vide[8+i]*4;
+        #ifdef WIN32
+        if (dll_type == 1)
+            (*wrvyuv_custom_message)(&cmsg_data,p_sys->handle);
+        else
+        #endif
+            (*rvyuv_custom_message)(&cmsg_data,p_sys->handle);
+    }
+   // es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'Y', 'V', '1', '2' ));
+//    es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'Y', 'U', 'Y', '2' ));
+    es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'I', '4', '2', '0' ));
+     
+        p_dec->fmt_out.video.i_width = p_dec->fmt_in.video.i_width;
+        p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
+        p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
+        p_sys->inited = 0;
+
+      vlc_mutex_unlock( lockval.p_address );
+        return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * Open: probe the decoder and return score
+ *****************************************************************************
+ * Tries to launch a decoder and return score so that the interface is able
+ * to choose.
+ *****************************************************************************/
+static int Open( vlc_object_t *p_this )
+{
+    decoder_t *p_dec = (decoder_t*)p_this;
+
+    /* create a mutex */
+    var_Create( p_this->p_libvlc, "rm_mutex", VLC_VAR_MUTEX );
+
+    switch ( p_dec->fmt_in.i_codec )
+    {
+    case VLC_FOURCC('R','V','1','0'): 
+    case VLC_FOURCC('R','V','2','0'): 
+    case VLC_FOURCC('R','V','3','0'):
+    case VLC_FOURCC('R','V','4','0'): 
+        p_dec->p_sys = NULL;
+        p_dec->pf_decode_video = DecodeVideo;
+        return InitVideo(p_dec);
+
+    default:
+        return VLC_EGENERIC;
+    }
+}
+
+/*****************************************************************************
+ * Close:
+ *****************************************************************************/
+static void Close( vlc_object_t *p_this )
+{
+    decoder_t     *p_dec = (decoder_t*)p_this;
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    vlc_value_t   lockval;
+
+    /* get lock, avoid segfault */
+    var_Get( p_dec->p_libvlc, "rm_mutex", &lockval );
+    vlc_mutex_lock( lockval.p_address );
+
+    #ifdef WIN32
+    if (dll_type == 1)
+    {
+        if (wrvyuv_free)
+            wrvyuv_free(p_sys->handle);
+    }
+    else
+    #endif
+        if (rvyuv_free)
+            rvyuv_free(p_sys->handle);
+#ifdef WIN32
+    if (dll_type == 1)
+    {
+        if (p_sys->rv_handle)
+            FreeLibrary(p_sys->rv_handle);
+    }
+    else
+#endif
+        p_sys->rv_handle=NULL;
+
+    if (p_sys->plane)
+    {
+        free(p_sys->plane);
+        p_sys->plane = NULL;
+    }
+
+    msg_Dbg( p_dec, "FreeLibrary ok." );
+#ifdef LOADER
+    Restore_LDT_Keeper( p_sys->ldt_fs );
+    msg_Dbg( p_dec, "Restore_LDT_Keeper" );
+#endif
+    p_sys->inited = 0;
+
+    vlc_mutex_unlock( lockval.p_address );
+    var_Destroy( p_dec->p_libvlc, "rm_mutex" );
+
+    if ( p_sys )
+        free( p_sys );
+}
+
+/*****************************************************************************
+ * DecodeVideo:
+ *****************************************************************************/
+static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    vlc_value_t   lockval;
+    block_t       *p_block;
+    picture_t     *p_pic;
+    mtime_t       i_pts;
+    int           result;
+
+    /* We must do open and close in the same thread (unless we do
+     * Setup_LDT_Keeper in the main thread before all others */
+    if ( pp_block == NULL || *pp_block == NULL )
+    {
+        return NULL;
+    }
+    p_block = *pp_block;
+    *pp_block = NULL;
+
+    i_pts = p_block->i_pts ? p_block->i_pts : p_block->i_dts;
+
+
+    var_Get( p_dec->p_libvlc, "rm_mutex", &lockval );
+    vlc_mutex_lock( lockval.p_address );
+
+    p_pic = p_dec->pf_vout_buffer_new( p_dec );
+
+    if ( p_pic )
+    {
+        unsigned int transform_out[5];
+        dp_hdr_t dp_hdr;
+        transform_in_t transform_in;
+        uint32_t pkg_len = ((uint32_t*)p_block->p_buffer)[0];
+        unsigned char* dp_data=((unsigned char*)p_block->p_buffer)+8;
+        uint32_t* extra=(uint32_t*)(((char*)p_block->p_buffer)+8+pkg_len);
+        uint32_t img_size;
+
+
+        dp_hdr.len = pkg_len;
+        dp_hdr.chunktab = 8 + pkg_len;
+        dp_hdr.chunks = ((uint32_t*)p_block->p_buffer)[1]-1;
+        dp_hdr.timestamp = i_pts;
+
+        memset(&transform_in, 0, sizeof(transform_in_t));
+
+        transform_in.len = dp_hdr.len;
+        transform_in.extra = extra;
+        transform_in.chunks = dp_hdr.chunks;
+        transform_in.timestamp = dp_hdr.timestamp;
+
+        memset (p_sys->plane, 0, p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height *3/2 );
+
+        #ifdef WIN32
+        if (dll_type == 1)
+            result=(*wrvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
+        else
+        #endif
+            result=(*rvyuv_transform)(dp_data, p_sys->plane, &transform_in, transform_out, p_sys->handle);
+
+        /* msg_Warn(p_dec, "Real Size %d X %d", transform_out[3],transform_out[4]); */
+        /* some bug rm file will print the messages :
+                [00000551] realvideo decoder warning: Real Size 320 X 240
+                [00000551] realvideo decoder warning: Real Size 480 X 272
+                [00000551] realvideo decoder warning: Real Size 320 X 240
+                [00000551] realvideo decoder warning: Real Size 320 X 240
+                ...
+            so it need fix!
+        */
+        if ( p_sys->inited == 0 )
+        {
+            /* fix and get the correct image size! */
+            if ( p_dec->fmt_in.video.i_width != transform_out[3] ||  p_dec->fmt_in.video.i_height  != transform_out[4] )
+            {
+                msg_Warn(p_dec, "Warning, Real's Header give a wrong information about media's width and height!\n"\
+                    "\tRealHeader: \t %d X %d  \t %d X %d",
+                    p_dec->fmt_in.video.i_width , p_dec->fmt_in.video.i_height , transform_out[3],transform_out[4]);
+                
+                if ( p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height >= transform_out[3] * transform_out[4] )
+                {
+                    p_dec->fmt_out.video.i_width = 
+                    p_dec->fmt_out.video.i_visible_width = 
+                    p_dec->fmt_in.video.i_width = transform_out[3] ;
+
+                    p_dec->fmt_out.video.i_height= 
+                    p_dec->fmt_out.video.i_visible_height = 
+                    p_dec->fmt_in.video.i_height= transform_out[4];
+
+                    p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
+                }
+                else
+                {
+                    // todo, realloc plane's size! but [in fact] it maybe not happen! 
+                    msg_Err(p_dec,"plane space not enough ,skip");
+                }
+            }
+            p_sys->inited = 1;
+        }
+
+        img_size = p_dec->fmt_in.video.i_width * p_dec->fmt_in.video.i_height;
+         memcpy( p_pic->p[0].p_pixels, p_sys->plane,  img_size);
+        memcpy( p_pic->p[1].p_pixels, p_sys->plane + img_size, img_size/4);
+        memcpy( p_pic->p[2].p_pixels, p_sys->plane + img_size * 5/4, img_size/4);
+        p_pic->date = i_pts ;
+
+        /*  real video frame is small( frame and frame's time-shift is short), 
+            so it will become late picture easier (when render-time changed)and droped by video-output.*/
+        p_pic->b_force = 1;
+    }
+
+    vlc_mutex_unlock( lockval.p_address );
+
+    block_Release( p_block );
+    return p_pic;
+}
+
diff --git a/modules/demux/real.c b/modules/demux/real.c
index 57e239d..e676cf8 100644
--- a/modules/demux/real.c
+++ b/modules/demux/real.c
@@ -56,6 +56,8 @@
 #include <vlc_demux.h>
 #include <vlc_charset.h>
 #include <vlc_meta.h>
+#include <stdlib.h>                                      /* malloc(), free() */
+
 
 /*****************************************************************************
  * Module descriptor
@@ -95,10 +97,19 @@ typedef struct
     int         i_subpacket;
     int         i_subpackets;
     block_t     **p_subpackets;
+    int64_t        *p_subpackets_timecode;
+
     int         i_out_subpacket;
 
 } real_track_t;
 
+typedef struct
+{
+    uint32_t file_offset;
+    uint32_t time_offset;
+    uint32_t frame_index;
+} rm_index_t;
+
 struct demux_sys_t
 {
     int64_t  i_data_offset;
@@ -121,6 +132,12 @@ struct demux_sys_t
     uint8_t buffer[65536];
 
     int64_t     i_pcr;
+
+    vlc_meta_t *p_meta;
+
+    int64_t i_index_offset;
+    int        b_seek;
+    rm_index_t * p_index;
 };
 
 static int Demux( demux_t *p_demux );
@@ -163,6 +180,7 @@ static int Open( vlc_object_t *p_this )
     p_sys->track   = NULL;
     p_sys->i_pcr   = 1;
 
+    p_sys->b_seek  = 0;
 
     /* Parse the headers */
     if( HeaderRead( p_demux ) )
@@ -212,7 +230,11 @@ static void Close( vlc_object_t *p_this )
             if( tk->p_subpackets[ j ] )
                 block_Release( tk->p_subpackets[ j ] );
         }
-        if( tk->i_subpackets ) free( tk->p_subpackets );
+        if( tk->i_subpackets )
+        {
+            if ( tk->p_subpackets ) free( tk->p_subpackets );
+            if ( tk->p_subpackets_timecode ) free( tk->p_subpackets_timecode );
+        }
 
         free( tk );
     }
@@ -222,6 +244,8 @@ static void Close( vlc_object_t *p_this )
     free( p_sys->psz_copyright );
     free( p_sys->psz_description );
 
+    if ( p_sys->p_index ) free( p_sys->p_index );
+
     if( p_sys->i_track > 0 ) free( p_sys->track );
     free( p_sys );
 }
@@ -359,8 +383,10 @@ static int Demux( demux_t *p_demux )
                 /* last fragment -> fixes */
                 i_copy = i_offset;
                 i_offset = i_len - i_copy;
+                #if 0
                 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
                          i_copy, i_offset );
+                #endif
             }
 
             if( tk->p_frame &&
@@ -368,6 +394,27 @@ static int Demux( demux_t *p_demux )
                   tk->i_frame != i_len ) )
             {
                 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
+                /*
+                it make sound bad!, audio PTS out of range!
+                //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 );
+                }
+                else
+                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 );
+                }
+                //msg_Dbg( p_demux, "Debug late picture p_sys->i_pcr  "I64Fd"  tk->p_frame->i_dts "I64Fd" i_pts "I64Fd" ", p_sys->i_pcr , tk->p_frame->i_dts, i_pts );
+                */
+
 
                 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
 
@@ -516,22 +563,37 @@ static int Demux( demux_t *p_demux )
             int i_index, i;
 
             /* Sanity check */
-            if( i_flags & 2 ) y = tk->i_subpacket = 0;
+            if( i_flags & 2 || ( p_sys->b_seek ) )
+            {
+                y = tk->i_subpacket = 0;
+                tk->i_out_subpacket = 0;
+                p_sys->b_seek = 0;
+            }
 
             if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
                 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
-            for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
             {
-                block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
-                memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
-                p_buf += tk->i_subpacket_size;
+                int num = tk->i_frame_size / tk->i_subpacket_size;
+                for( i = 0; i <  num; i++ )
+                {
+                    block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
+                    memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
+                    p_buf += tk->i_subpacket_size;
 
-                i_index = tk->i_subpacket_h * i +
-                    ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
+                    i_index = tk->i_subpacket_h * i +
+                        ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
 
-                p_block->i_dts = p_block->i_pts = i_pts;
-                tk->p_subpackets[i_index] = p_block;
-                tk->i_subpacket++;
+                    if ( tk->p_subpackets[i_index]  != NULL )
+                    {
+                        msg_Dbg(p_demux, "p_subpackets[ %d ] not null!",  i_index );
+                        free( tk->p_subpackets[i_index] );
+                    }
+
+                    tk->p_subpackets[i_index] = p_block;
+                    tk->i_subpacket++;
+                    p_block->i_dts = p_block->i_pts = 0;
+                }
+                tk->p_subpackets_timecode[tk->i_subpacket - num] = i_pts;
             }
 
             if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
@@ -554,6 +616,7 @@ static int Demux( demux_t *p_demux )
                    tk->p_subpackets[tk->i_out_subpacket] )
             {
                 /* Set the PCR */
+#if 0
                 if (tk->i_out_subpacket == 0)
                 {
                     p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
@@ -565,6 +628,23 @@ static int Demux( demux_t *p_demux )
                 tk->p_subpackets[tk->i_out_subpacket] = 0;
 
                 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
+#endif
+
+                block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
+                tk->p_subpackets[tk->i_out_subpacket] = 0;
+                //if ( p_block->i_dts )
+                if ( tk->p_subpackets_timecode[tk->i_out_subpacket]  )
+                {
+                    p_block->i_dts = p_block->i_pts =
+                        tk->p_subpackets_timecode[tk->i_out_subpacket];
+                    tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
+
+//msg_Dbg(p_demux, "Cook send pcr %3d "I64Fd" "I64Fd" ", tk->i_out_subpacket, p_block->i_dts,  mdate() );
+                    p_sys->i_pcr = p_block->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_block );
 
                 tk->i_out_subpacket++;
@@ -583,8 +663,11 @@ static int Demux( demux_t *p_demux )
                 tk->i_out_subpacket = 0;
             }
         }
-        else
+        else 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];
+
             /* Set PCR */
             if( p_sys->i_pcr < i_pts )
             {
@@ -593,53 +676,55 @@ static int Demux( demux_t *p_demux )
                         (int64_t)p_sys->i_pcr );
             }
 
-            if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
+            int i;
+            for( i = 0; i < i_sub; i++ )
             {
-                int     i_sub = (p_sys->buffer[1] >> 4)&0x0f;
-                uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
-
-                int i;
-                for( i = 0; i < i_sub; i++ )
+                int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
+                block_t *p_block = block_New( p_demux, i_sub_size );
+                if( p_block )
                 {
-                    int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
-                    block_t *p_block = block_New( p_demux, i_sub_size );
-                    if( p_block )
-                    {
-                        memcpy( p_block->p_buffer, p_sub, i_sub_size );
-                        p_sub += i_sub_size;
+                    memcpy( p_block->p_buffer, p_sub, i_sub_size );
+                    p_sub += i_sub_size;
 
-                        p_block->i_dts =
-                            p_block->i_pts = ( i == 0 ? i_pts : 0 );
+                    p_block->i_dts =
+                    p_block->i_pts = ( i == 0 ? i_pts : 0 );
 
-                        es_out_Send( p_demux->out, tk->p_es, p_block );
-                    }
+                    es_out_Send( p_demux->out, tk->p_es, p_block );
                 }
             }
-            else
-            {
-                block_t *p_block = block_New( p_demux, i_size );
+        }
+        else
+        {
+            block_t *p_block = block_New( p_demux, i_size );
 
-                if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
-                {
-                    uint8_t *src = p_sys->buffer;
-                    uint8_t *dst = p_block->p_buffer;
+            /* Set the 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 );
+            }
 
-                    /* byte swap data */
-                    while( dst < &p_block->p_buffer[i_size- 1])
-                    {
-                        *dst++ = src[1];
-                        *dst++ = src[0];
+            if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
+            {
+                uint8_t *src = p_sys->buffer;
+                uint8_t *dst = p_block->p_buffer;
 
-                        src += 2;
-                    }
-                }
-                else
+               /* byte swap data */
+                while( dst < &p_block->p_buffer[i_size- 1])
                 {
-                    memcpy( p_block->p_buffer, p_sys->buffer, i_size );
+                    *dst++ = src[1];
+                    *dst++ = src[0];
+
+                    src += 2;
                 }
-                p_block->i_dts = p_block->i_pts = i_pts;
-                es_out_Send( p_demux->out, tk->p_es, p_block );
             }
+            else
+            {
+                memcpy( p_block->p_buffer, p_sys->buffer, i_size );
+            }
+            p_block->i_dts = p_block->i_pts = i_pts;
+            es_out_Send( p_demux->out, tk->p_es, p_block );
         }
     }
 
@@ -653,17 +738,26 @@ static int Demux( demux_t *p_demux )
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-#if 0
+#if 1
     double f, *pf;
     int64_t i64;
+    rm_index_t * p_index;
 #endif
     int64_t *pi64;
 
     switch( i_query )
     {
-#if 0
         case DEMUX_GET_POSITION:
             pf = (double*) va_arg( args, double* );
+
+            if( p_sys->i_our_duration > 0 )
+            {
+                *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
+            }
+            else *pf = 0.0;
+
+#if 0
+            // ÕâÀïʹÓÃStreamµÄpositionÀ´¶¨£¬Ê¹ÓÃframeºÍdruationÓ¦¸Ã¸ü¾«×¼£¡
             i64 = stream_Size( p_demux->s );
             if( i64 > 0 )
             {
@@ -673,30 +767,123 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             {
                 *pf = 0.0;
             }
-            return VLC_SUCCESS;
 
-        case DEMUX_SET_POSITION:
-            f = (double) va_arg( args, double );
-            i64 = stream_Size( p_demux->s );
-
-            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
+            msg_Dbg(p_demux,"DEMUX_GET_POSITION : %f size: "I64Fd" ", *pf, i64);
+#endif
 
-            return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
+            return VLC_SUCCESS;
 
         case DEMUX_GET_TIME:
             pi64 = (int64_t*)va_arg( args, int64_t * );
-            if( p_sys->i_mux_rate > 0 )
+
+            if( p_sys->i_our_duration > 0 )
             {
-                *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
+                *pi64 = p_sys->i_pcr;
+            }
+            else
+                *pi64 = 0;
+            return VLC_SUCCESS;
+
+#if 0
+            // ÕâÀïʹÓÃStreamµÄpositionÀ´¶¨£¬Ê¹ÓÃframeºÍdruationÓ¦¸Ã¸ü¾«×¼£¡
+            i64 = stream_Size( p_demux->s );
+            if( p_sys->i_our_duration > 0 && i64 > 0 )
+            {
+                *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
+                msg_Dbg(p_demux,"DEMUX_GET_TIME :OK  "I64Fd" ", *pi64);
                 return VLC_SUCCESS;
             }
+
             *pi64 = 0;
             return VLC_EGENERIC;
 #endif
 
+        case DEMUX_SET_POSITION:
+            f = (double) va_arg( args, double );
+            i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
+
+            //msg_Dbg(p_demux,"Seek Real  DEMUX_SET_POSITION : %f file_offset :"I64Fd" p_sys->i_pcr "I64Fd" ", f, i64 , p_sys->i_pcr );
+
+            if ( p_sys->i_index_offset == 0 && i64 != 0 )
+            {
+                msg_Err(p_demux,"Seek No Index Real File failed!" );
+                return VLC_EGENERIC; // no index!
+            }
+            if ( i64 == 0 )
+            {
+                /* it is a rtsp stream*/
+
+                msg_Dbg(p_demux, "Seek in real rtsp stream!");
+                p_sys->i_pcr = (int64_t)1000 * ( p_sys->i_our_duration * f  );
+
+                es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
+                p_sys->b_seek = 1;
+#if 0
+                int i;
+                for( i = 0; i < p_sys->i_track; i++ )
+                {
+                    real_track_t *tk = p_sys->track[i];
+
+                    if( tk->fmt.i_cat == AUDIO_ES && tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' )  )
+                    {
+                        msg_Dbg(p_demux, "Cook find do seek  %d %d", tk->i_out_subpacket, tk->i_subpacket );
+                        tk->i_out_subpacket = tk->i_subpacket  = 0;
+                    }
+                }
+#endif
+                return stream_Seek( p_demux->s, p_sys->i_pcr );
+            }
+
+            if ( p_sys->i_index_offset > 0 )
+            {
+                p_index = p_sys->p_index;
+                while( p_index->file_offset !=0 )
+                {
+                    if ( p_index->file_offset > i64 )
+                    {
+    /*
+                    msg_Dbg(p_demux, "Seek Real find! %d %d %d", p_index->time_offset, p_index->file_offset ,(uint32_t) i64);
+    */
+                        if ( p_index != p_sys->p_index ) p_index --;
+                        i64 = p_index->file_offset;
+                        break;
+                    }
+                    p_index++;
+                }
+
+                //msg_Dbg(p_demux, "Seek Real pcr from :"I64Fd" to "I64Fd"  ", p_sys->i_pcr , 1000 * (int64_t) p_index->time_offset  );
+                p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
+
+                es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
+
+                return stream_Seek( p_demux->s, i64 );
+            }
+        case DEMUX_SET_TIME:
+            i64 = (int64_t) va_arg( args, int64_t ) / 1000;
+            //msg_Dbg(p_demux,"DEMUX_SET_TIME :OK  "I64Fd" ",i64);
+
+            p_index = p_sys->p_index;
+            while( p_index->file_offset !=0 )
+            {
+                if ( p_index->time_offset > i64 )
+                {
+                    if ( p_index != p_sys->p_index )
+                        p_index --;
+                    i64 = p_index->file_offset;
+                    break;
+                }
+                p_index++;
+            }
+
+            p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
+            es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
+
+            return stream_Seek( p_demux->s, i64 );
+
+            //return VLC_EGENERIC;
         case DEMUX_GET_LENGTH:
             pi64 = (int64_t*)va_arg( args, int64_t * );
- 
+
             /* the commented following lines are fen's implementation, which doesn't seem to
              * work for one reason or another -- FK */
             /*if( p_sys->i_mux_rate > 0 )
@@ -708,7 +895,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             {
                 /* our stored duration is in ms, so... */
                 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
- 
+
                 return VLC_SUCCESS;
             }
             *pi64 = 0;
@@ -731,7 +918,6 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             return VLC_SUCCESS;
         }
 
-        case DEMUX_SET_TIME:
         case DEMUX_GET_FPS:
         default:
             return VLC_EGENERIC;
@@ -740,6 +926,81 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 }
 
 /*****************************************************************************
+ * ReadRealIndex:
+ *****************************************************************************/
+
+static void ReadRealIndex( demux_t *p_demux )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+    uint8_t buffer[100];
+    uint32_t    i_id;
+    uint32_t    i_size;
+    int         i_version;
+    int            i;
+
+    uint32_t    i_index_count;
+
+    if ( p_sys->i_index_offset == 0 )
+        return;
+
+    stream_Seek( p_demux->s, p_sys->i_index_offset );
+
+    if ( stream_Read( p_demux->s, buffer, 20 ) < 20 )
+        return ;
+
+    i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
+    i_size      = GetDWBE( &buffer[4] );
+    i_version   = GetWBE( &buffer[8] );
+
+    msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
+                 (char*)&i_id, i_size, i_version );
+
+    if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
+        return;
+
+    i_index_count = GetDWBE( &buffer[10] );
+
+    msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
+
+    if ( i_index_count == 0 )
+        return;
+
+    if (  GetDWBE( &buffer[16] ) > 0 )
+        msg_Dbg( p_demux, "Real Index: next index is exist? %d ", GetDWBE( &buffer[16] )  );
+
+    p_sys->p_index =  ( rm_index_t *) malloc( sizeof(rm_index_t) * (i_index_count+1) );
+    if ( p_sys->p_index == NULL )
+    {
+        msg_Err( p_demux, "Real Index: Error , fail to malloc index buffer " );
+        return;
+    }
+
+    memset( p_sys->p_index, 0, sizeof(rm_index_t) * (i_index_count+1) );
+
+    for( i=0; i<i_index_count; i++ )
+    {
+        if ( stream_Read( p_demux->s, buffer, 14 ) < 14 )
+            return ;
+
+        if ( GetWBE( &buffer[0] ) != 0 )
+        {
+            msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ", GetWBE( &buffer[0] ) );
+            return;
+        }
+
+        p_sys->p_index[i].time_offset = GetDWBE( &buffer[2] );
+        p_sys->p_index[i].file_offset = GetDWBE( &buffer[6] );
+        p_sys->p_index[i].frame_index = GetDWBE( &buffer[10] );
+#if 0
+        msg_Dbg( p_demux, "Real Index: time %d file %d frame %d ", p_sys->p_index[i].time_offset, p_sys->p_index[i].file_offset , p_sys->p_index[i].frame_index );
+#endif
+
+    }
+
+}
+
+
+/*****************************************************************************
  * HeaderRead:
  *****************************************************************************/
 static int HeaderRead( demux_t *p_demux )
@@ -752,6 +1013,8 @@ static int HeaderRead( demux_t *p_demux )
     int64_t     i_skip;
     int         i_version;
 
+    p_sys->p_meta = vlc_meta_New();
+
     for( ;; )
     {
         /* Read the header */
@@ -801,6 +1064,8 @@ static int HeaderRead( demux_t *p_demux )
             /* set the duration for export in control */
             p_sys->i_our_duration = (int)GetDWBE(&header[20]);
  
+            p_sys->i_index_offset = GetDWBE(&header[28]);
+
             i_flags = GetWBE(&header[38]);
             msg_Dbg( p_demux, "    - flags=0x%x %s%s%s",
                      i_flags,
@@ -827,6 +1092,8 @@ static int HeaderRead( demux_t *p_demux )
                 EnsureUTF8( psz );
                 msg_Dbg( p_demux, "    - title=`%s'", psz );
                 p_sys->psz_title = psz;
+                vlc_meta_Add( p_sys->p_meta, VLC_META_TITLE, psz );
+                free( psz );
                 i_skip -= i_len;
             }
             i_skip -= 2;
@@ -841,6 +1108,8 @@ static int HeaderRead( demux_t *p_demux )
                 EnsureUTF8( psz );
                 msg_Dbg( p_demux, "    - author=`%s'", psz );
                 p_sys->psz_artist = psz;
+                vlc_meta_Add( p_sys->p_meta, VLC_META_ARTIST, psz );
+                free( psz );
                 i_skip -= i_len;
             }
             i_skip -= 2;
@@ -855,6 +1124,8 @@ static int HeaderRead( demux_t *p_demux )
                 EnsureUTF8( psz );
                 msg_Dbg( p_demux, "    - copyright=`%s'", psz );
                 p_sys->psz_copyright = psz;
+                vlc_meta_Add( p_sys->p_meta, VLC_META_COPYRIGHT, psz );
+                free( psz );
                 i_skip -= i_len;
             }
             i_skip -= 2;
@@ -869,6 +1140,8 @@ static int HeaderRead( demux_t *p_demux )
                 EnsureUTF8( psz );
                 msg_Dbg( p_demux, "    - comment=`%s'", psz );
                 p_sys->psz_description = psz;
+                vlc_meta_Add( p_sys->p_meta, VLC_META_DESCRIPTION, psz );
+                free( psz );
                 i_skip -= i_len;
             }
             i_skip -= 2;
@@ -958,6 +1231,14 @@ static int HeaderRead( demux_t *p_demux )
 
     /* TODO read index if possible */
 
+    if ( p_sys->i_index_offset > 0 )
+    {
+        int64_t pos;
+        pos = stream_Tell( p_demux->s );
+        ReadRealIndex( p_demux );
+        stream_Seek( p_demux->s, pos );
+    }
+
     return VLC_SUCCESS;
 }
 
@@ -977,6 +1258,7 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                         p_peek[10], p_peek[11] ) );
         fmt.video.i_width = GetWBE( &p_peek[12] );
         fmt.video.i_height= GetWBE( &p_peek[14] );
+//    msg_Dbg( p_demux, "Real Image Size %d X %d (%d)", fmt.video.i_width, fmt.video.i_height, i_num );
 
         fmt.i_extra = 8;
         fmt.p_extra = malloc( 8 );
@@ -1014,6 +1296,7 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
         tk->i_subpacket = 0;
         tk->i_subpackets = 0;
         tk->p_subpackets = NULL;
+        tk->p_subpackets_timecode = NULL;
         tk->i_id = i_num;
         tk->fmt = fmt;
         tk->i_frame = 0;
@@ -1214,6 +1497,12 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                 }
                 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
             }
+            /*
+            fmt.audio.i_blockalign = i_subpacket_size;
+            if( !(fmt.i_extra = GetDWBE( p_peek )) ) break;
+            fmt.p_extra = malloc( fmt.i_extra );
+            memcpy( fmt.p_extra, p_peek + 4, fmt.i_extra );
+            */
             break;
 
         case VLC_FOURCC('r','a','l','f'):
@@ -1246,6 +1535,7 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
             tk->i_subpacket = 0;
             tk->i_subpackets = 0;
             tk->p_subpackets = NULL;
+            tk->p_subpackets_timecode = NULL;
             if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
              || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
             {
@@ -1253,6 +1543,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                     i_subpacket_h * i_frame_size / tk->i_subpacket_size;
                 tk->p_subpackets =
                     calloc( tk->i_subpackets, sizeof(block_t *) );
+                tk->p_subpackets_timecode =
+                    calloc( tk->i_subpackets , sizeof( int64_t ) );
             }
             else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
             {
@@ -1260,6 +1552,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                     i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
                 tk->p_subpackets =
                     calloc( tk->i_subpackets, sizeof(block_t *) );
+                tk->p_subpackets_timecode =
+                    calloc( tk->i_subpackets , sizeof( int64_t ) );
             }
 
             /* Check if the calloc went correctly */
-- 
1.5.5.1




More information about the vlc-devel mailing list