--- configure	2006-10-30 19:07:32.000000000 +0100
+++ configure	2006-10-30 22:31:32.000000000 +0100
@@ -259,6 +259,8 @@
   --disable-pthreads     Disable Posix threads support [autodetect]
   --disable-ass          Disable internal SSA/ASS subtitles support [autodetect]
   --enable-rpath         Enable runtime linker path for extra libs [disabled]
+  --enable-sharedmem	 Enable put readed frame to shared memory area in mencoder and
+ 			    shared memory reader stream [disable]
 
 Codecs:
   --enable-gif		 enable gif support [autodetect]
@@ -1723,6 +1725,7 @@
 _pthreads=auto
 _ass=auto
 _rpath=no
+_sharedmem=no
 _asmalign_pot=auto
 _color_console=no
 for ac_option do
@@ -2038,6 +2041,8 @@
   --disable-ass)        _ass=no         ;;
   --enable-rpath)       _rpath=yes      ;;
   --disable-rpath)      _rpath=no       ;;
+  --enable-sharedmem)	_sharedmem=yes	;;
+  --disable-sharedmem)	_sharedmem=no	;;
   --enable-color-console)  _color_console=yes ;;
   --disable-color-console) _color_console=no  ;;
 
@@ -2997,6 +3002,22 @@
 fi
 echores "$_dynamic_plugins"
 
+echocheck "sharedmem"
+if test "$_sharedmem" = yes ; then
+  cat > $TMPC << EOF
+#include <sys/types.h>
+#include <sys/shm.h>
+int main(void) { shmget(0, 0, 0); shmat(0, 0, 0); shmctl(0, 0, 0); return 0; }
+EOF
+  _sharedmem=no
+  cc_check && _sharedmem=yes
+fi
+if test "$_sharedmem" = yes ; then
+  _def_sharedmem='#define HAVE_SHAREDMEMOUT 1'
+else
+  _def_sharedmem='#undef HAVE_SHAREDMEMOUT'
+fi
+echores "$_sharedmem"
 
 #echocheck "dynamic linking"
 # FIXME !! make this dynamic detection work and modify at the end (search _ld_dl_dynamic)
@@ -7942,6 +7963,9 @@
 /* Define this if your system has pthreads */
 $_def_pthreads
 
+/* Define this if use sharedmem  */
+$_def_sharedmem
+
 /* Define this if you enabled thread support for libavcodec */
 $_def_threads
 
--- mencoder.c	2006-10-30 19:07:32.000000000 +0100
+++ mencoder.c	2006-10-30 22:30:40.000000000 +0100
@@ -113,6 +113,16 @@
 char* audio_lang=NULL;
 char* dvdsub_lang=NULL;
 static char* spudec_ifo=NULL;
+#ifdef HAVE_SHAREDMEMOUT
+#include <sys/ipc.h>
+#include <sys/shm.h>
+int smemid=0;
+int shmid=0;
+int smemsize=0;
+int smemmaxsize=0;
+char* shmptr=NULL;
+struct shmid_ds smem_ds;
+#endif
 
 static char** audio_codec_list=NULL;  // override audio codec
 static char** video_codec_list=NULL;  // override video codec
@@ -305,6 +315,12 @@
     else
 	mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_Exiting);
 
+#ifdef HAVE_SHAREDMEMOUT
+    if (shmptr) shmdt(shmptr);
+    shmptr=NULL;
+    if (shmid) shmctl(shmid, IPC_RMID, 0);
+    shmid=0;
+#endif	
     exit(level);
 }
 
@@ -1353,6 +1369,56 @@
     }
 }
 
+#ifdef HAVE_SHAREDMEMOUT
+if (smemid)
+  {
+  int smemopened = 0;
+  if (shmid==0)
+    {
+    int msize;
+    msize=sizeof(smem_t)+frame_data.in_size;
+    smemmaxsize=frame_data.in_size;
+    if((shmid = shmget(smemid, msize, IPC_CREAT|0666)) < 0) 
+      fprintf(stderr,"Can't open shared memory! (%d)\n",smemid);
+      else smemopened = 1;
+    }
+  if (shmid>0 && !shmptr)
+    {
+    if((shmptr=shmat(shmid,0,0))==(char*)-1)
+      {
+      fprintf(stderr,"Can't mount shared memory!\n");
+      shmctl(shmid, IPC_RMID, 0);
+      shmid=-1;
+      smemopened=0;
+      }
+    }
+  if (shmid>0)
+    {
+    smem_ds.shm_nattch=0;    
+    if (shmctl(shmid,IPC_STAT,&smem_ds)==0)
+      {
+//      printf("attach: %d \n",smem_ds.shm_nattch);
+      }
+    }
+  if (shmid>0 && shmptr && (smem_ds.shm_nattch>1 || smemopened))
+    {
+    smem_t *ptr;
+    ptr=shmptr;
+    if (frame_data.in_size<smemmaxsize) smemsize=frame_data.in_size; else smemsize=smemmaxsize;
+    ptr->mplayerid[0]='M';
+    ptr->mplayerid[1]='P';
+    ptr->type=0;
+    ptr->size=smemsize;
+    ptr->demuxer_type=file_format;
+    ptr->format=sh_video->format;
+    ptr->width=sh_video->disp_w;
+    ptr->height=sh_video->disp_h;
+    memcpy(ptr->data,frame_data.start, smemsize);
+    }
+  smemopened=0;
+  }
+#endif
+
 videosamples++;
 videorate+=(GetTimerMS() - ptimer_start);
 
--- cfg-mencoder.h	2006-10-30 19:07:32.000000000 +0100
+++ cfg-mencoder.h	2006-10-30 22:30:40.000000000 +0100
@@ -238,6 +238,10 @@
 	{"odml", &write_odml, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL},
 	{"noodml", &write_odml, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL},
 	
+#ifdef HAVE_SHAREDMEMOUT
+	// shared memory
+	{"smemid", &smemid, CONF_TYPE_INT, 0, 0, 0, NULL},
+#endif
 	// info header strings
 	{"info", info_conf, CONF_TYPE_SUBCONFIG, CONF_GLOBAL, 0, 0, NULL},
 
--- stream/Makefile	2006-10-30 19:07:00.000000000 +0100
+++ stream/Makefile	2006-10-30 22:32:31.000000000 +0100
@@ -24,6 +24,7 @@
         stream_file.c \
         stream_mf.c \
         stream_null.c \
+        stream_sharedmem.c \
 
 ifeq ($(HAVE_DVD),yes)
 SRCS += stream_dvd.c
--- stream/stream.c	2006-10-30 19:07:00.000000000 +0100
+++ stream/stream.c	2006-10-30 22:33:29.000000000 +0100
@@ -41,6 +41,9 @@
 #ifdef HAVE_CDDA
 extern stream_info_t stream_info_cdda;
 #endif
+#ifdef HAVE_SHAREDMEMOUT
+extern stream_info_t stream_info_sharedmem;
+#endif
 #ifdef MPLAYER_NETWORK
 extern stream_info_t stream_info_netstream;
 extern stream_info_t stream_info_pnm;
@@ -95,6 +98,9 @@
 #ifdef HAVE_CDDA
   &stream_info_cdda,
 #endif
+#ifdef HAVE_SHAREDMEMOUT
+  &stream_info_sharedmem,
+#endif
 #ifdef MPLAYER_NETWORK
   &stream_info_netstream,
   &stream_info_http1,
--- stream/stream.h	2006-10-30 19:07:00.000000000 +0100
+++ stream/stream.h	2006-10-30 22:34:37.000000000 +0100
@@ -25,6 +25,9 @@
 #define STREAMTYPE_TV 17
 #define STREAMTYPE_MF 18
 #define STREAMTYPE_RADIO 19
+#ifdef HAVE_SHAREDMEMOUT
+#define STREAMTYPE_SHAREDMEM 20
+#endif
 
 #define STREAM_BUFFER_SIZE 2048
 
@@ -56,6 +59,11 @@
 #define STREAM_CTRL_SEEK_TO_CHAPTER 2
 #define STREAM_CTRL_GET_CURRENT_CHAPTER 3
 #define STREAM_CTRL_GET_NUM_CHAPTERS 4
+#ifdef HAVE_SHAREDMEMOUT
+#define STREAM_CTRL_GET_WIDTH 5
+#define STREAM_CTRL_GET_HEIGHT 6
+#define STREAM_CTRL_GET_FORMAT 7
+#endif
 
 #ifdef MPLAYER_NETWORK
 #include "network.h"
@@ -109,6 +117,19 @@
   unsigned char buffer[STREAM_BUFFER_SIZE>VCD_SECTOR_SIZE?STREAM_BUFFER_SIZE:VCD_SECTOR_SIZE];
 } stream_t;
 
+#ifdef HAVE_SHAREDMEMOUT
+typedef struct smem_st {
+  char mplayerid[2];
+  int type;
+  int size;
+  int demuxer_type;
+  int format;
+  int width;
+  int height;
+  char data[0];
+} smem_t;
+#endif
+
 #ifdef USE_STREAM_CACHE
 int stream_enable_cache(stream_t *stream,int size,int min,int prefill);
 int cache_stream_fill_buffer(stream_t *s);
--- libmpdemux/demux_rawvideo.c	2006-10-30 19:07:30.000000000 +0100
+++ libmpdemux/demux_rawvideo.c	2006-10-30 22:30:47.000000000 +0100
@@ -55,6 +55,11 @@
 static demuxer_t* demux_rawvideo_open(demuxer_t* demuxer) {
   sh_video_t* sh_video;
 
+#ifdef HAVE_SHAREDMEMOUT
+  if (width==0) stream_control(demuxer->stream, STREAM_CTRL_GET_WIDTH, &width);
+  if (height==0) stream_control(demuxer->stream, STREAM_CTRL_GET_HEIGHT, &height);
+  if (format==0) stream_control(demuxer->stream, STREAM_CTRL_GET_FORMAT, &format);
+#endif
   switch(size_id){
   case 1: width=128; height=96; break;
   case 2: width=176; height=144; break;
--- DOCS/man/en/mplayer.1	2006-10-30 19:07:18.000000000 +0100
+++ DOCS/man/en/mplayer.1	2006-10-30 22:37:47.000000000 +0100
@@ -122,6 +122,11 @@
 .B mplayer
 tivo://host/[list|llist|fsid]
 [options]
+.br
+.in
+.B mplayer
+smem://memid[,memid[,memid ... ]]
+[options]
 .
 .br
 .B gmplayer
@@ -6759,6 +6764,11 @@
 .B \-vobsuboutindex <index>
 Specify the index of the subtitles in the output files (default: 0).
 .
+.TP
+.B \-smemid <memoryid>
+Specify the shared memory id where mencoder stores grabbed raw frames 
+(eg. TV grab) for mplayer smem://memoryid.
+.
 .
 .
 .SH "CODEC SPECIFIC ENCODING OPTIONS (MENCODER ONLY)"
@@ -10235,6 +10245,17 @@
 rar p test-SVCD.rar | mencoder \-ovc lavc \-lavcopts vcodec=mpeg4:vbitrate=800 \-ofps 24 \-
 .fi
 .
+.PP
+.B Show the encoded tv channels on shared screen:
+.nf
+mencoder tv:// -smemid 1000 [options]
+mencoder tv:// -smemid 1001 [options]
+mencoder tv:// -smemid 1002 [options]
+mencoder tv:// -smemid 1003 [options]
+.
+mplayer smem://1000,1001,1002,1003 -vf tile=2:2 [options]
+.fi
+.
 .
 .\" --------------------------------------------------------------------------
 .\" Bugs, authors, standard disclaimer
