changeset 10263:0df8816f4665

TiVo demuxer and sub-cc/osd decoder patch by usenet@wingert.org (http://tivo-mplayer.sourceforge.net/releases/MPlayer-20030501-tivo-patch.gz) changes by me: - spit demux_ty to demux_ty and demux_ty_osd (later handles mpeg user-data decoding, ie sub-cc and osd) - removed some cosmetics changes - some compile fixes (gcc3 specific variable decl etc)
author arpi
date Mon, 09 Jun 2003 00:24:49 +0000
parents e6f5487b2042
children 8a099cab3ce7
files libmpdemux/Makefile libmpdemux/demux_ty.c libmpdemux/demux_ty_osd.c libmpdemux/demuxer.c libmpdemux/demuxer.h libmpdemux/video.c libvo/sub.c
diffstat 7 files changed, 2025 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libmpdemux/Makefile	Sun Jun 08 20:36:27 2003 +0000
+++ b/libmpdemux/Makefile	Mon Jun 09 00:24:49 2003 +0000
@@ -3,7 +3,7 @@
 
 include ../config.mak
 
-SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c muxer.c muxer_avi.c muxer_mpeg.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_pva.c demux_viv.c demuxer.c dvdauth.c dvdnav_stream.c open.c parse_es.c stream.c stream_file.c stream_netstream.c stream_vcd.c stream_null.c tv.c tvi_dummy.c tvi_v4l.c tvi_bsdbt848.c frequencies.c demux_fli.c demux_real.c demux_y4m.c yuv4mpeg.c yuv4mpeg_ratio.c demux_nuv.c demux_film.c demux_roq.c mf.c demux_mf.c demux_audio.c demux_demuxers.c demux_ogg.c demux_bmp.c cdda.c demux_rawaudio.c demux_rawvideo.c cddb.c cdinfo.c demux_rawdv.c ai_alsa.c ai_oss.c audio_in.c demux_smjpeg.c cue_read.c extension.c demux_gif.c demux_ts.c demux_realaud.c
+SRCS = mp3_hdr.c video.c mpeg_hdr.c cache2.c asfheader.c aviheader.c aviprint.c muxer.c muxer_avi.c muxer_mpeg.c demux_asf.c demux_avi.c demux_mov.c parse_mp4.c demux_mpg.c demux_ty.c demux_ty_osd.c demux_pva.c demux_viv.c demuxer.c dvdauth.c dvdnav_stream.c open.c parse_es.c stream.c stream_file.c stream_netstream.c stream_vcd.c stream_null.c tv.c tvi_dummy.c tvi_v4l.c tvi_bsdbt848.c frequencies.c demux_fli.c demux_real.c demux_y4m.c yuv4mpeg.c yuv4mpeg_ratio.c demux_nuv.c demux_film.c demux_roq.c mf.c demux_mf.c demux_audio.c demux_demuxers.c demux_ogg.c demux_bmp.c cdda.c demux_rawaudio.c demux_rawvideo.c cddb.c cdinfo.c demux_rawdv.c ai_alsa.c ai_oss.c audio_in.c demux_smjpeg.c cue_read.c extension.c demux_gif.c demux_ts.c demux_realaud.c
 ifeq ($(XMMS_PLUGINS),yes)
 SRCS += demux_xmms.c
 endif 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmpdemux/demux_ty.c	Mon Jun 09 00:24:49 2003 +0000
@@ -0,0 +1,1056 @@
+/*
+ * tivo@wingert.org, February 2003
+ *
+ * Copyright (C) 2003 Christopher R. Wingert
+ *
+ * The license covers the portions of this file regarding TiVo additions.
+ *
+ * Olaf Beck and Tridge (indirectly) were essential at providing 
+ * information regarding the format of the TiVo streams.  
+ *
+ * However, no code in the following subsection is directly copied from 
+ * either author.
+ *
+ *
+ * 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-1307, USA.
+ *
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
+#include <stdarg.h>
+
+#include "config.h"
+#include "mp_msg.h"
+#include "help_mp.h"
+
+#include "stream.h"
+#include "demuxer.h"
+#include "parse_es.h"
+#include "stheader.h"
+//#include "mp3_hdr.h"
+//#include "../subreader.h"
+#include "../sub_cc.h"
+//#include "../libvo/sub.h"
+
+//#include "dvdauth.h"
+
+extern void resync_audio_stream( sh_audio_t *sh_audio );
+extern void skip_audio_frame( sh_audio_t *sh_audio );
+extern int sub_justify;
+
+// 2/c0: audio data
+// 3/c0: audio packet header (PES header)
+// 4/c0: audio data (S/A only?)
+// 9/c0: audio packet header, AC-3 audio
+// 2/e0: video data
+// 6/e0: video packet header (PES header)
+// 7/e0: video sequence header start
+// 8/e0: video I-frame header start
+// a/e0: video P-frame header start
+// b/e0: video B-frame header start
+// c/e0: video GOP header start
+// e/01: closed-caption data
+// e/02: Extended data services data 
+
+
+#define TIVO_PES_FILEID   ( 0xf5467abd )
+#define TIVO_PART_LENGTH  ( 0x20000000 )
+
+#define CHUNKSIZE        ( 128 * 1024 )
+#define MAX_AUDIO_BUFFER ( 16 * 1024 )
+
+#define PTS_MHZ          ( 90 )
+#define PTS_KHZ          ( PTS_MHZ * 1000 )
+
+#define TY_V             ( 1 )
+#define TY_A             ( 1 )
+
+typedef struct sTivoInfo
+{
+   unsigned char lastAudio[ MAX_AUDIO_BUFFER ];
+   int           lastAudioEnd;
+
+   int           tivoType;           // 1 = SA, 2 = DTiVo
+
+   float         firstAudioPTS;
+   float         firstVideoPTS;
+
+   float         lastAudioPTS;
+   float         lastVideoPTS;
+
+   int           headerOk;
+   unsigned int  pesFileId;          // Should be 0xf5467abd
+   int           streamType;         // Should be 0x02
+   int           chunkSize;          // Should always be 128k
+   off_t         size;
+   int           readHeader;
+} TiVoInfo;
+
+off_t vstream_streamsize( );
+void ty_ClearOSD( int start );
+
+// DTiVo MPEG 336, 480, 576, 768
+// SA TiVo 864
+// DTiVo AC-3 1550
+//
+#define SERIES1_PTS_LENGTH         ( 11 )
+#define SERIES1_PTS_OFFSET         ( 6 )
+#define SERIES2_PTS_LENGTH         ( 16 )
+#define SERIES2_PTS_OFFSET         ( 9 )
+#define AC3_PTS_LENGTH             ( 16 )
+#define AC3_PTS_OFFSET             ( 9 )
+
+#define NUMBER_DIFFERENT_AUDIO_SIZES ( 6 )
+static int Series1AudioWithPTS[ NUMBER_DIFFERENT_AUDIO_SIZES ] = 
+{ 
+   336 + SERIES1_PTS_LENGTH, 
+   480 + SERIES1_PTS_LENGTH, 
+   576 + SERIES1_PTS_LENGTH, 
+   768 + SERIES1_PTS_LENGTH, 
+   864 + SERIES1_PTS_LENGTH 
+};
+static int Series2AudioWithPTS[ NUMBER_DIFFERENT_AUDIO_SIZES ] = 
+{ 
+   336 + SERIES2_PTS_LENGTH, 
+   480 + SERIES2_PTS_LENGTH, 
+   576 + SERIES2_PTS_LENGTH, 
+   768 + SERIES2_PTS_LENGTH, 
+   864 + SERIES2_PTS_LENGTH 
+};
+
+static int IsValidAudioPacket( int size, int *ptsOffset, int *ptsLen )
+{
+   int count;
+
+   *ptsOffset = 0;
+   *ptsLen = 0;
+
+   // AC-3
+   if ( ( size == 1550 ) || ( size == 1552 ) )
+   {
+      *ptsOffset = AC3_PTS_OFFSET;
+      *ptsLen = AC3_PTS_LENGTH;
+      return( 1 );
+   }
+
+   // MPEG
+   for( count = 0 ; count < NUMBER_DIFFERENT_AUDIO_SIZES ; count++ )
+   {
+      if ( size == Series1AudioWithPTS[ count ] )
+      {
+         *ptsOffset = SERIES1_PTS_OFFSET;
+         *ptsLen = SERIES1_PTS_LENGTH;
+         break;
+      }
+   }
+   if ( *ptsOffset == 0 )
+   {
+      for( count = 0 ; count < NUMBER_DIFFERENT_AUDIO_SIZES ; count++ )
+      {
+         if ( size == Series2AudioWithPTS[ count ] )
+         {
+            *ptsOffset = SERIES2_PTS_OFFSET;
+            *ptsLen = SERIES2_PTS_LENGTH;
+            break;
+         }
+      }
+   }
+   if ( *ptsOffset == 0 )
+   {
+      mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Tossing Audio Packet Size %d\n", 
+         size );
+      return( 0 );
+   }
+   else
+   {
+      return( 1 );
+   }
+}
+
+
+static float get_ty_pts( unsigned char *buf )
+{
+   float result = 0;
+   unsigned char temp;
+
+   temp = ( buf[ 0 ] & 0xE ) >> 1;
+   result = ( (float) temp ) * ( (float) ( 1L << 30 ) ) / ( (float)PTS_KHZ );
+   temp = buf[ 1 ];
+   result += ( (float) temp ) * ( (float) ( 1L << 22 ) ) / ( (float)PTS_KHZ );
+   temp = ( buf[ 2 ] & 0xFE ) >> 1;
+   result += ( (float) temp ) * ( (float) ( 1L << 15 ) ) / ( (float)PTS_KHZ );
+   temp = buf[ 3 ];
+   result += ( (float) temp ) * ( (float) ( 1L << 7 ) ) / ( (float)PTS_KHZ );
+   temp = ( buf[ 4 ] & 0xFE ) >> 1;
+   result += ( (float) temp ) / ( (float)PTS_MHZ );
+
+   return result;
+}
+
+static void demux_ty_AddToAudioBuffer( TiVoInfo *tivo, unsigned char *buffer, 
+   int size )
+{
+   if ( ( tivo->lastAudioEnd + size ) < MAX_AUDIO_BUFFER )
+   {
+      memcpy( &( tivo->lastAudio[ tivo->lastAudioEnd ] ), 
+         buffer, size );
+      tivo->lastAudioEnd += size;
+   }
+   else
+   {
+      mp_msg( MSGT_DEMUX, MSGL_ERR,
+         "ty:WARNING - Would have blown my audio buffer\n" );
+   }
+}
+
+static void demux_ty_CopyToDemuxPacket( int type, TiVoInfo *tivo, demux_stream_t *ds, 
+	unsigned char *buffer, int size, off_t pos, float pts )
+{
+   demux_packet_t   *dp;
+
+   // mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Calling ds_add_packet() %7.1f\n", pts );
+   // printf( "%x %x %x %x\n", 
+   //    buffer[ 0 ], buffer[ 1 ], buffer[ 2 ], buffer[ 3 ] );
+
+   dp = new_demux_packet( size );
+   memcpy( dp->buffer, buffer, size );
+   dp->pts = pts;
+   dp->pos = pos;
+   dp->flags = 0;
+   ds_add_packet( ds, dp );
+	ds->pts = pts;
+	if ( type == TY_V )
+	{
+		if ( tivo->firstVideoPTS == -1 )
+		{
+			tivo->firstVideoPTS = pts;
+		}
+	}
+	if ( type == TY_A )
+	{
+		if ( tivo->firstAudioPTS == -1 )
+		{
+			tivo->firstAudioPTS = pts;
+		}
+	}
+}
+
+static int demux_ty_FindESHeader( unsigned char *header, int headerSize, 
+   unsigned char *buffer, int bufferSize, int *esOffset1 )
+{
+   int count;
+
+   *esOffset1 = -1;
+   for( count = 0 ; count < bufferSize ; count++ )
+   {
+      if ( ( buffer[ count + 0 ] == header[ 0 ] ) &&
+         ( buffer[ count + 1 ] == header[ 1 ] ) &&
+         ( buffer[ count + 2 ] == header[ 2 ] ) &&
+         ( buffer[ count + 3 ] == header[ 3 ] ) )
+      {
+         *esOffset1 = count;
+         return( 1 );
+      }
+   }
+   return( -1 );
+}
+
+static void demux_ty_FindESPacket( unsigned char *header, int headerSize, 
+   unsigned char *buffer, int bufferSize, int *esOffset1, int *esOffset2 )
+{
+   int count;
+
+   *esOffset1 = -1;
+   *esOffset2 = -1;
+
+   for( count = 0 ; count < bufferSize ; count++ )
+   {
+      if ( ( buffer[ count + 0 ] == header[ 0 ] ) &&
+         ( buffer[ count + 1 ] == header[ 1 ] ) &&
+         ( buffer[ count + 2 ] == header[ 2 ] ) &&
+         ( buffer[ count + 3 ] == header[ 3 ] ) )
+      {
+         *esOffset1 = count;
+         break;
+      }
+   }
+
+   if ( *esOffset1 != -1 )
+   {
+      for( count = *esOffset1 + 1 ; 
+         count < bufferSize ; count++ )
+      {
+         if ( ( buffer[ count + 0 ] == header[ 0 ] ) &&
+            ( buffer[ count + 1 ] == header[ 1 ] ) &&
+            ( buffer[ count + 2 ] == header[ 2 ] ) &&
+            ( buffer[ count + 3 ] == header[ 3 ] ) )
+         {
+            *esOffset2 = count;
+            break;
+         }
+      }
+   }
+}
+
+static int tivobuffer2hostlong( unsigned char *buffer )
+{
+   return
+   ( 
+      buffer[ 0 ] << 24 | buffer[ 1 ] << 16 | buffer[ 2 ] << 8 | buffer[ 3 ]
+   );
+}
+
+static unsigned char tivo_reversebyte( unsigned char val )
+{
+   int           count;
+   unsigned char ret;
+
+   ret = 0;
+   for ( count = 0 ; count < 8 ; count++ )
+   {
+      ret = ret << 1;
+      ret |= ( ( val >> count ) & 0x01 );
+   }
+   return( ret );
+}
+
+
+static unsigned char ty_VideoPacket[] = { 0x00, 0x00, 0x01, 0xe0 };
+static unsigned char ty_MPEGAudioPacket[] = { 0x00, 0x00, 0x01, 0xc0 };
+static unsigned char ty_AC3AudioPacket[] = { 0x00, 0x00, 0x01, 0xbd };
+
+int demux_ty_fill_buffer( demuxer_t *demux )
+{
+   int              invalidType = 0;
+   int              errorHeader = 0;
+   int              recordsDecoded = 0;
+   off_t            filePos = 0;
+
+   unsigned char    chunk[ CHUNKSIZE ];
+   int              whichChunk;
+   int              readSize;
+   unsigned int     pesFileId = 0;
+
+   int              numberRecs;
+   unsigned char    *recPtr;
+   int              offset;
+   int              size;
+
+   int              type;
+   int              nybbleType;
+
+   int              counter;
+
+   int              aid;
+   demux_stream_t   *ds = NULL;
+   
+   int              esOffset1;
+   int              esOffset2;
+
+   TiVoInfo         *tivo = 0;
+
+   if ( demux->stream->type == STREAMTYPE_DVD )
+	{
+		return( 0 );
+	}
+
+   mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Parsing a chunk\n" );
+   if ( ( demux->a_streams[ MAX_A_STREAMS - 1 ] ) == 0 )
+   {
+      demux->a_streams[ MAX_A_STREAMS - 1 ] = malloc( sizeof( TiVoInfo ) );
+      tivo = demux->a_streams[ MAX_A_STREAMS - 1 ];
+      memset( tivo, 0, sizeof( TiVoInfo ) );
+      tivo->firstAudioPTS = -1;
+      tivo->firstVideoPTS = -1;
+   }
+   else
+   {
+      tivo = demux->a_streams[ MAX_A_STREAMS - 1 ];
+   }
+
+   if( demux->stream->eof ) return 0;
+ 
+   // ======================================================================
+   // If we haven't figured out the size of the stream, lets do so
+   // ======================================================================
+#ifdef STREAMTYPE_STREAM_TY
+   if ( demux->stream->type == STREAMTYPE_STREAM_TY )
+   {
+      // The vstream code figures out the exact size of the stream
+      demux->movi_start = 0;
+      demux->movi_end = vstream_streamsize();
+      tivo->size = vstream_streamsize();
+   }
+   else
+#endif
+   {
+      // If its a local file, try to find the Part Headers, so we can
+      // calculate the ACTUAL stream size
+      // If we can't find it, go off with the file size and hope the
+      // extract program did the "right thing"
+      if ( tivo->readHeader == 0 )
+      {
+         tivo->readHeader = 1;
+         filePos = demux->filepos;
+         stream_seek( demux->stream, 0 );
+         // mp_msg( MSGT_DEMUX, MSGL_DBG3, 
+			// 	"ty:Reading a chunk %d\n", __LINE__ );
+         readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
+         if ( readSize == CHUNKSIZE )
+         {
+            tivo->pesFileId = tivobuffer2hostlong( &chunk[ 0x00 ] );
+            tivo->streamType = tivobuffer2hostlong( &chunk[ 0x04 ] );
+            tivo->chunkSize = tivobuffer2hostlong( &chunk[ 0x08 ] );
+            tivo->size = tivobuffer2hostlong( &chunk[ 0x0c ] );
+            if ( tivo->pesFileId == TIVO_PES_FILEID )
+            {
+               off_t numberParts;
+               off_t size;
+
+               if ( demux->stream->end_pos > TIVO_PART_LENGTH )
+               {
+                  numberParts = demux->stream->end_pos / TIVO_PART_LENGTH;
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Number Parts %d\n",
+                    numberParts );
+                  stream_seek( demux->stream, numberParts * TIVO_PART_LENGTH );
+                  // mp_msg( MSGT_DEMUX, MSGL_DBG3, 
+				      //    "ty:Reading a chunk %d\n", __LINE__ );
+                  readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
+                  pesFileId = tivobuffer2hostlong( &chunk[ 0x00 ] );
+                  if ( pesFileId == TIVO_PES_FILEID )
+                  {
+                     size = tivobuffer2hostlong( &chunk[ 0x0c ] );
+                     size /= 256;
+                     size -= 4;
+                     size *= CHUNKSIZE;
+                     tivo->size = numberParts * TIVO_PART_LENGTH;
+                     tivo->size += size;
+                     mp_msg( MSGT_DEMUX, MSGL_DBG3, 
+                        "ty:Header Calc Stream Size %lld\n", tivo->size );
+                  }
+                  else
+                  {
+                     tivo->size = demux->stream->end_pos;
+                  }
+               }
+               else
+               {
+                  tivo->size = demux->stream->end_pos;
+               }
+            }
+            else
+            {
+               tivo->size = demux->stream->end_pos;
+            }
+         }
+         if ( tivo->size > demux->stream->end_pos )
+         {
+            tivo->size = demux->stream->end_pos;
+         }
+
+			if ( demux->stream->start_pos > 0 )
+			{
+				filePos = demux->stream->start_pos;
+			}
+         stream_seek( demux->stream, filePos );
+			demux->filepos = stream_tell( demux->stream );
+      }
+      demux->movi_start = 0;
+      demux->movi_end = tivo->size;
+   }
+
+   // ======================================================================
+   // Give a clue as to where we are in the stream
+   // ======================================================================
+   mp_msg( MSGT_DEMUX, MSGL_DBG3,
+      "ty:ty header size %llx\n", tivo->size );
+   mp_msg( MSGT_DEMUX, MSGL_DBG3,
+      "ty:file end_pos   %llx\n", demux->stream->end_pos );
+//   mp_msg( MSGT_DEMUX, MSGL_DBG3,
+//      "ty:vstream size   %llx\n", vstream_streamsize() );
+
+   mp_msg( MSGT_DEMUX, MSGL_DBG3,
+      "\nty:wanted current offset %llx\n", stream_tell( demux->stream ) );
+
+   if ( tivo->size > 0 )
+   {
+      if ( stream_tell( demux->stream ) > tivo->size )
+      {
+         demux->stream->eof = 1;
+         return( 0 );
+      }
+   }
+
+   // Make sure we are on a 128k boundary
+   if ( ( demux->filepos % CHUNKSIZE ) != 0 )
+   {
+      whichChunk = demux->filepos / CHUNKSIZE;
+      if ( ( demux->filepos % CHUNKSIZE ) > ( CHUNKSIZE / 2 ) )
+      {
+         whichChunk++;
+      }
+      stream_seek( demux->stream, ( whichChunk * CHUNKSIZE ) );
+   }
+
+   demux->filepos = stream_tell( demux->stream );
+   readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
+   if ( readSize != CHUNKSIZE )
+   {
+      return( 0 );
+   }
+
+   // We found a part header, skip it
+   pesFileId = tivobuffer2hostlong( &chunk[ 0x00 ] );
+   if( pesFileId == TIVO_PES_FILEID )
+   {
+      demux->filepos = stream_tell( demux->stream );
+      mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Skipping PART Header\n" );
+      readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
+      if ( readSize != CHUNKSIZE )
+      {
+         return( 0 );
+      }
+   }   
+   mp_msg( MSGT_DEMUX, MSGL_DBG3,
+      "\nty:actual current offset %llx\n", ( stream_tell( demux->stream ) - 
+		0x20000 ) );
+
+
+   // Lets make a Video Demux Stream for Mplayer
+   aid = 0x0;
+   if( !demux->v_streams[ aid ] ) new_sh_video( demux, aid );
+   if( demux->video->id == -1 ) demux->video->id = aid;
+   if( demux->video->id == aid )
+   {
+      ds = demux->video;
+      if( !ds->sh ) ds->sh = demux->v_streams[ aid ];
+   }
+
+   // ======================================================================
+   // Finally, we get to actually parse the chunk
+   // ======================================================================
+   numberRecs = chunk[ 0 ];
+   recPtr = &chunk[ 4 ];
+   offset = ( numberRecs * 16 ) + 4;
+   for ( counter = 0 ; counter < numberRecs ; counter++ )
+   {
+      size = ( recPtr[ 0 ] << 8 | recPtr[ 1 ] ) << 4 | ( recPtr[ 2 ] >> 4 );
+      type = recPtr[ 3 ];
+      nybbleType = recPtr[ 2 ] & 0x0f;
+      recordsDecoded++;
+
+      mp_msg( MSGT_DEMUX, MSGL_DBG3,
+         "ty:Record Type %x/%x %d\n", nybbleType, type, size );
+
+      // ================================================================
+      // Video Parsing
+      // ================================================================
+      if ( type == 0xe0 )
+      {
+         if ( ( size > 0 ) && ( ( size + offset ) <= CHUNKSIZE ) )
+         {
+#if 0
+            printf( "Video Chunk Header " );
+            for( count = 0 ; count < 24 ; count++ )
+            {
+               printf( "%2.2x ", chunk[ offset + count ] );
+            }
+            printf( "\n" );
+#endif
+            demux_ty_FindESHeader( ty_VideoPacket, 4, &chunk[ offset ], 
+               size, &esOffset1 );
+            if ( esOffset1 != -1 )
+            {
+               tivo->lastVideoPTS = get_ty_pts( 
+                  &chunk[ offset + esOffset1 + 9 ] );
+               mp_msg( MSGT_DEMUX, MSGL_DBG3, "Video PTS %7.1f\n", 
+                  tivo->lastVideoPTS );
+            }
+
+            // Do NOT Pass the PES Header onto the MPEG2 Decode
+            if( nybbleType != 0x06 )
+            { 
+               demux_ty_CopyToDemuxPacket( TY_V, tivo, demux->video, 
+						&chunk[ offset ], size, ( demux->filepos + offset ), 
+						tivo->lastVideoPTS );
+            }
+            offset += size;
+         }
+			else
+			{
+				errorHeader++;
+			}
+      }
+      // ================================================================
+      // Audio Parsing
+      // ================================================================
+		else if ( type == 0xc0 )
+      {
+         if ( ( size > 0 ) && ( ( size + offset ) <= CHUNKSIZE ) )
+         {
+#if 0
+            printf( "Audio Chunk Header " );
+            for( count = 0 ; count < 24 ; count++ )
+            {
+               printf( "%2.2x ", chunk[ offset + count ] );
+            }
+            printf( "\n" );
+#endif
+
+            if( demux->audio->id == -1 )
+            {
+               if ( nybbleType == 0x02 )
+               {
+                  continue;    // DTiVo inconclusive, wait for more
+               }
+               else if ( nybbleType == 0x09 )
+               {
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Setting AC-3 Audio\n" );
+                  aid = 0x80;  // AC-3
+               }
+               else
+               {
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Setting MPEG Audio\n" );
+                  aid = 0x0;   // MPEG Audio
+               }
+
+               demux->audio->id = aid;
+               if( !demux->a_streams[ aid ] ) new_sh_audio( demux, aid );
+               if( demux->audio->id == aid )
+               {
+                  ds = demux->audio;
+                  if( !ds->sh ) ds->sh = demux->a_streams[ aid ];
+               }
+            }
+
+            aid = demux->audio->id;
+
+
+            // SA DTiVo Audio Data, no PES
+            // ================================================
+            if ( nybbleType == 0x02 )
+            {
+               if ( tivo->tivoType == 2 )
+               {
+                  demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size );
+               }
+               else
+               {
+
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3,
+                     "ty:Adding Audio Packet Size %d\n", size );
+                  demux_ty_CopyToDemuxPacket( TY_A, tivo, demux->audio, 
+							&chunk[ offset ], size, ( demux->filepos + offset ), 
+							tivo->lastAudioPTS );
+               }
+            }
+
+            // MPEG Audio with PES Header, either SA or DTiVo
+            // ================================================
+            if ( nybbleType == 0x03 )
+            {
+               demux_ty_FindESHeader( ty_MPEGAudioPacket, 4, &chunk[ offset ], 
+                  size, &esOffset1 );
+
+               // SA PES Header, No Audio Data
+               // ================================================
+               if ( ( esOffset1 == 0 ) && ( size == 16 ) )
+               {
+                  tivo->tivoType = 1;
+                  tivo->lastAudioPTS = get_ty_pts( &chunk[ offset + 
+                     SERIES2_PTS_OFFSET ] );
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, "SA Audio PTS %7.1f\n", 
+                     tivo->lastAudioPTS );
+               }
+               else
+               // DTiVo Audio with PES Header
+               // ================================================
+               {
+                  tivo->tivoType = 2;
+
+                  demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size );
+                  demux_ty_FindESPacket( ty_MPEGAudioPacket, 4,
+                     tivo->lastAudio, tivo->lastAudioEnd, &esOffset1,
+                     &esOffset2 );
+
+                  if ( ( esOffset1 != -1 ) && ( esOffset2 != -1 ) )
+                  {
+                     int packetSize = esOffset2 - esOffset1;
+                     int headerSize;
+                     int ptsOffset;
+
+                     if ( IsValidAudioPacket( packetSize, &ptsOffset, 
+                           &headerSize ) )
+                     {
+                        mp_msg( MSGT_DEMUX, MSGL_DBG3,
+                           "ty:Adding DTiVo Audio Packet Size %d\n", 
+                           packetSize );
+
+                        tivo->lastAudioPTS = get_ty_pts( 
+                           &tivo->lastAudio[ esOffset1 + ptsOffset ] );
+                        mp_msg( MSGT_DEMUX, MSGL_DBG3, 
+                           "MPEG Audio PTS %7.1f\n", tivo->lastAudioPTS );
+
+                        demux_ty_CopyToDemuxPacket
+                        ( 
+								   TY_A,
+								   tivo,
+                           demux->audio, 
+                           &( tivo->lastAudio[ esOffset1 + headerSize ] ), 
+                           ( packetSize - headerSize ),
+                           ( demux->filepos + offset ), 
+                           tivo->lastAudioPTS 
+                        );
+
+                     }
+
+                     // Collapse the Audio Buffer
+                     memmove( &(tivo->lastAudio[ 0 ] ), 
+                        &( tivo->lastAudio[ esOffset2 ] ), 
+                        ( tivo->lastAudioEnd - esOffset2 ) );
+                     tivo->lastAudioEnd -= esOffset2;
+                  }
+               }
+            }
+
+            // SA Audio with no PES Header
+            // ================================================
+            if ( nybbleType == 0x04 )
+            {
+               mp_msg( MSGT_DEMUX, MSGL_DBG3,
+                  "ty:Adding Audio Packet Size %d\n", size );
+               demux_ty_CopyToDemuxPacket( TY_A, tivo, demux->audio, 
+						&chunk[ offset ], size, ( demux->filepos + offset ), 
+						tivo->lastAudioPTS );
+            }
+
+            // DTiVo AC3 Audio Data with PES Header
+            // ================================================
+            if ( nybbleType == 0x09 )
+            {
+               tivo->tivoType = 2;
+
+               demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size );
+               demux_ty_FindESPacket( ty_AC3AudioPacket, 4,
+                  tivo->lastAudio, tivo->lastAudioEnd, &esOffset1,
+                  &esOffset2 );
+
+               if ( ( esOffset1 != -1 ) && ( esOffset2 != -1 ) )
+               {
+                  int packetSize = esOffset2 - esOffset1;
+                  int headerSize;
+                  int ptsOffset;
+
+                  if ( IsValidAudioPacket( packetSize, &ptsOffset, 
+                        &headerSize ) )
+                  {
+                     mp_msg( MSGT_DEMUX, MSGL_DBG3,
+                        "ty:Adding DTiVo Audio Packet Size %d\n", 
+                        packetSize );
+
+                     tivo->lastAudioPTS = get_ty_pts( 
+                        &tivo->lastAudio[ esOffset1 + ptsOffset ] );
+                     mp_msg( MSGT_DEMUX, MSGL_DBG3, 
+                        "AC3 Audio PTS %7.1f\n", tivo->lastAudioPTS );
+
+                     // AC3 Decoder WANTS the PTS
+                     demux_ty_CopyToDemuxPacket
+                     ( 
+							   TY_A,
+							   tivo,
+                        demux->audio, 
+                        &( tivo->lastAudio[ esOffset1 ] ), 
+                        ( packetSize ),
+                        ( demux->filepos + offset ), 
+                        tivo->lastAudioPTS 
+                     );
+
+                  }
+
+                  // Collapse the Audio Buffer
+                  memmove( &(tivo->lastAudio[ 0 ] ), 
+                     &( tivo->lastAudio[ esOffset2 ] ), 
+                     ( tivo->lastAudioEnd - esOffset2 ) );
+                  tivo->lastAudioEnd -= esOffset2;
+               }
+            }
+            offset += size;
+         }
+			else
+			{
+				errorHeader++;
+			}
+		}
+      // ================================================================
+      // Closed Caption
+      // ================================================================
+      else if ( type == 0x01 )
+		{
+			unsigned char b1;
+			unsigned char b2;
+			unsigned char buffer[ 16 ];
+
+			b1 = ( ( ( recPtr[ 0 ] & 0x0f ) << 4 ) | 
+				( ( recPtr[ 1 ] & 0xf0 ) >> 4 ) );
+			b1 &= 0x7f;
+			b2 = ( ( ( recPtr[ 1 ] & 0x0f ) << 4 ) | 
+				( ( recPtr[ 2 ] & 0xf0 ) >> 4 ) );
+			b2 &= 0x7f;
+
+			mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:CC %x %x\n", b1, b2 );
+
+			buffer[ 0x00 ] = 0x00;
+			buffer[ 0x01 ] = 0x00;
+			buffer[ 0x02 ] = 0x01;
+			buffer[ 0x03 ] = 0xb2;
+			buffer[ 0x04 ] = 'T';
+			buffer[ 0x05 ] = 'Y';
+			buffer[ 0x06 ] = 0x01;
+			buffer[ 0x07 ] = b1;
+			buffer[ 0x08 ] = b2;
+			demux_ty_CopyToDemuxPacket( TY_V, tivo, demux->video, buffer, 0x09,
+				( demux->filepos + offset ), tivo->lastVideoPTS );
+		}
+      // ================================================================
+      // Extended Data Services
+      // ================================================================
+		else if ( type == 0x02 )
+		{
+			unsigned char b1;
+			unsigned char b2;
+			unsigned char buffer[ 16 ];
+
+			b1 = ( ( ( recPtr[ 0 ] & 0x0f ) << 4 ) | 
+				( ( recPtr[ 1 ] & 0xf0 ) >> 4 ) );
+			b1 &= 0x7f;
+			b2 = ( ( ( recPtr[ 1 ] & 0x0f ) << 4 ) | 
+				( ( recPtr[ 2 ] & 0xf0 ) >> 4 ) );
+			b2 &= 0x7f;
+
+         mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:XDS %x %x\n", b1, b2 );
+
+			buffer[ 0x00 ] = 0x00;
+			buffer[ 0x01 ] = 0x00;
+			buffer[ 0x02 ] = 0x01;
+			buffer[ 0x03 ] = 0xb2;
+			buffer[ 0x04 ] = 'T';
+			buffer[ 0x05 ] = 'Y';
+			buffer[ 0x06 ] = 0x02;
+			buffer[ 0x07 ] = b1;
+			buffer[ 0x08 ] = b2;
+			demux_ty_CopyToDemuxPacket( TY_V, tivo, demux->video, buffer, 0x09,
+				( demux->filepos + offset ), tivo->lastVideoPTS );
+		}
+      // ================================================================
+      // Found a 0x03 on Droid's TiVo, I have no idea what it is
+      // ================================================================
+		else if ( type == 0x03 )
+		{
+         if ( ( size > 0 ) && ( ( size + offset ) <= CHUNKSIZE ) )
+         {
+            offset += size;
+			}
+		}
+      // ================================================================
+      // Unknown
+      // ================================================================
+		else if ( type == 0x05 )
+		{
+         if ( ( size > 0 ) && ( ( size + offset ) <= CHUNKSIZE ) )
+         {
+            offset += size;
+			}
+		}
+		else
+		{
+         if ( ( size > 0 ) && ( ( size + offset ) <= CHUNKSIZE ) )
+         {
+            offset += size;
+			}
+         mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Invalid Type %x\n", type );
+			invalidType++;
+		}
+     recPtr += 16;
+   }
+
+   if ( errorHeader > 0 )
+   {
+      mp_msg( MSGT_DEMUX, MSGL_DBG3, 
+         "ty:Error Check - Records %d, Parsed %d, Errors %d\n",
+         numberRecs, recordsDecoded, errorHeader );
+
+      // Invalid MPEG ES Size Check
+      if ( errorHeader > ( numberRecs / 2 ) )
+      {
+         return( 0 );
+      }
+
+      // Invalid MPEG Stream Type Check
+      if ( invalidType > ( numberRecs / 2 ) )
+      {
+         return( 0 );
+		}
+   }
+
+   demux->filepos = stream_tell( demux->stream );
+
+   return( 1 );
+}
+
+void demux_seek_ty( demuxer_t *demuxer, float rel_seek_secs, int flags )
+{
+   demux_stream_t *d_audio = demuxer->audio;
+   demux_stream_t *d_video = demuxer->video;
+   sh_audio_t     *sh_audio = d_audio->sh;
+   sh_video_t     *sh_video = d_video->sh;
+   off_t          newpos;
+   off_t          res;
+   TiVoInfo       *tivo = 0;
+
+   mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Seeking to %7.1f\n", rel_seek_secs );
+
+   if ( ( demuxer->a_streams[ MAX_A_STREAMS - 1 ] ) != 0 )
+   {
+      tivo = demuxer->a_streams[ MAX_A_STREAMS - 1 ];
+      tivo->lastAudioEnd = 0;
+      tivo->lastAudioPTS = 0;
+      tivo->lastVideoPTS = 0;
+   }
+   //
+   //================= seek in MPEG ==========================
+   demuxer->filepos = stream_tell( demuxer->stream );
+
+   newpos = ( flags & 1 ) ? demuxer->movi_start : demuxer->filepos;
+	
+   if( flags & 2 )
+   {
+	   // float seek 0..1
+	   newpos += ( demuxer->movi_end - demuxer->movi_start ) * rel_seek_secs;
+   } 
+   else 
+   {
+	   // time seek (secs)
+      if( ! sh_video->i_bps ) // unspecified or VBR
+      {
+         newpos += 2324 * 75 * rel_seek_secs; // 174.3 kbyte/sec
+      }
+      else
+      {
+         newpos += sh_video->i_bps * rel_seek_secs;
+      }
+   }
+
+   if ( newpos < demuxer->movi_start )
+   {
+	   if( demuxer->stream->type != STREAMTYPE_VCD ) demuxer->movi_start = 0;
+	   if( newpos < demuxer->movi_start ) newpos = demuxer->movi_start;
+	}
+
+   res = newpos / CHUNKSIZE;
+   if ( rel_seek_secs >= 0 )
+   {
+      newpos = ( res + 1 ) * CHUNKSIZE;
+   }
+   else
+   {
+      newpos = res * CHUNKSIZE;
+   }
+
+   if ( newpos < 0 )
+   {
+      newpos = 0;
+   }
+   stream_seek( demuxer->stream, newpos  );
+
+   // re-sync video:
+   videobuf_code_len = 0; // reset ES stream buffer
+
+	ds_fill_buffer( d_video );
+	if( sh_audio )
+   {
+	  ds_fill_buffer( d_audio );
+	  resync_audio_stream( sh_audio );
+	}
+
+	while( 1 )
+   {
+	  int i;
+     if( sh_audio && !d_audio->eof && d_video->pts && d_audio->pts )
+     {
+	     float a_pts = d_audio->pts;
+        a_pts += ( ds_tell_pts( d_audio ) - sh_audio->a_in_buffer_len ) /
+           (float)sh_audio->i_bps;
+	     if( d_video->pts > a_pts )
+        {
+	        skip_audio_frame( sh_audio );  // sync audio
+	        continue;
+	     }
+     }
+     i = sync_video_packet( d_video );
+     if( i == 0x1B3 || i == 0x1B8 ) break; // found it!
+     if( !i || !skip_video_packet( d_video ) ) break; // EOF?
+   }
+	if ( subcc_enabled )
+	{
+	   ty_ClearOSD( 0 );
+	}
+}
+
+int demux_ty_control( demuxer_t *demuxer,int cmd, void *arg )
+{
+   demux_stream_t *d_video = demuxer->video;
+   sh_video_t     *sh_video = d_video->sh;
+
+   switch(cmd) 
+   {
+	   case DEMUXER_CTRL_GET_TIME_LENGTH:
+	      if(!sh_video->i_bps)  // unspecified or VBR 
+    		   return DEMUXER_CTRL_DONTKNOW;
+	      *((unsigned long *)arg)=
+            (demuxer->movi_end-demuxer->movi_start)/sh_video->i_bps;
+	      return DEMUXER_CTRL_GUESS;
+
+	   case DEMUXER_CTRL_GET_PERCENT_POS:
+	      if (demuxer->movi_end==demuxer->movi_start) 
+    		   return DEMUXER_CTRL_DONTKNOW;
+    	    *((int *)arg)=
+             (int)((demuxer->filepos-demuxer->movi_start)/
+             ((demuxer->movi_end-demuxer->movi_start)/100));
+	       return DEMUXER_CTRL_OK;
+	    default:
+	       return DEMUXER_CTRL_NOTIMPL;
+    }
+}
+
+
+int demux_close_ty( demuxer_t *demux )
+{
+   TiVoInfo         *tivo = 0;
+
+   if ( ( demux->a_streams[ MAX_A_STREAMS - 1 ] ) != 0 )
+   {
+      tivo = demux->a_streams[ MAX_A_STREAMS - 1 ];
+      free( tivo );
+      demux->a_streams[ MAX_A_STREAMS - 1 ] = 0;
+	   sub_justify = 0;
+   }
+   return( 0 );
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmpdemux/demux_ty_osd.c	Mon Jun 09 00:24:49 2003 +0000
@@ -0,0 +1,907 @@
+// Most of this was written by mbm@linux.com and released on the GPL2 License.
+//
+// Modifications and SEVERE cleanup of the code was done by 
+// Christopher Wingert 
+// Copyright 2003
+// 
+// Released under GPL2 License.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
+#include <stdarg.h>
+
+#include "config.h"
+#include "mp_msg.h"
+#include "help_mp.h"
+
+//#include "stream.h"
+//#include "demuxer.h"
+//#include "parse_es.h"
+//#include "stheader.h"
+//#include "mp3_hdr.h"
+//#include "../subreader.h"
+#include "../sub_cc.h"
+#include "../libvo/sub.h"
+
+//#include "dvdauth.h"
+
+extern int sub_justify;
+
+#define TY_TEXT_MODE        ( 1 << 0 )
+#define TY_OSD_MODE         ( 1 << 1 )
+
+static int TY_OSD_flags = TY_TEXT_MODE | TY_OSD_MODE;
+static int TY_OSD_debug = 0;
+
+// ===========================================================================
+// Closed Caption Decoding and OSD Presentation
+// ===========================================================================
+#define TY_CCNONE     ( -3 )
+#define TY_CCTEXTMODE ( -2 )
+#define TY_CCPOPUPNB  ( -1 )
+#define TY_CCPOPUP    (  0 )
+#define TY_CCPAINTON  (  1 )
+
+#define TY_CC_MAX_X   ( 45 )
+
+static int      TY_CC_CUR_X;
+static int      TY_CC_CUR_Y;
+static int      TY_CC_stat = TY_CCNONE;
+static char     TY_CC_buf[ 255 ];
+static char     *TY_CC_ptr = TY_CC_buf;
+static unsigned TY_CC_lastcap = 0;
+static int      TY_CC_TextItalic;
+static int      TY_CC_Y_Offset;
+
+static subtitle ty_OSD1;
+static subtitle ty_OSD2;
+static subtitle *ty_pOSD1;
+static subtitle *ty_pOSD2;
+static int             tyOSDInited = 0;
+static int             tyOSDUpdate = 0;
+
+static void ty_DrawOSD()
+{
+	// printf( "Calling ty_DrawOSD()\n" );
+	tyOSDUpdate = 1;
+}
+
+void ty_ClearOSD( int start )
+{
+	int index;
+	// printf( "Calling ty_ClearOSD()\n" );
+   for ( index = start ; index < SUB_MAX_TEXT ; index++ )
+	{
+		memset( ty_OSD1.text[ index ], ' ', TY_CC_MAX_X - 1 );
+		ty_OSD1.text[ index ][ TY_CC_MAX_X - 1 ] = 0;
+		memset( ty_OSD2.text[ index ], ' ', TY_CC_MAX_X - 1 );
+		ty_OSD2.text[ index ][ TY_CC_MAX_X - 1 ] = 0;
+	}
+}
+
+static void ty_DrawChar( int *x, int *y, char disChar, int fgColor, int bgColor )
+{
+	int index;
+   int cx;
+   int cy;
+
+   cx = *x;
+   cy = *y;
+
+	if ( *x >= ( TY_CC_MAX_X - 1 ) )
+	{
+      cx = 0;
+	}
+	if ( ( *y + TY_CC_Y_Offset ) > SUB_MAX_TEXT )
+	{
+	   cy = SUB_MAX_TEXT - TY_CC_Y_Offset - 1;
+   }
+
+	// printf( "Calling ty_DrawChar() x:%d y:%d %c fg:%d bg:%d\n",
+	// 	cx, cy, disChar, fgColor, bgColor );
+
+   ty_OSD1.text[ TY_CC_Y_Offset + cy ][ cx ] = disChar;
+   memset( &( ty_OSD1.text[ TY_CC_Y_Offset + cy ][ cx + 1 ] ), ' ',
+      TY_CC_MAX_X - cx - 2 );
+	( *x )++;
+}
+
+static void ty_RollupBuf( int dest, int source, int numLines )
+{
+	int index;
+
+	// printf( "Calling ty_RollupBuf() dest:%d source %d, numLines %d\n",
+	//    dest, source, numLines );
+	//
+	if ( ( source + TY_CC_Y_Offset + numLines ) > SUB_MAX_TEXT )
+	{
+      ty_ClearOSD( 1 );
+		return;
+	}
+
+	if ( ( source + TY_CC_Y_Offset + numLines ) < 0 )
+	{
+      ty_ClearOSD( 1 );
+		return;
+	}
+
+	if ( numLines > SUB_MAX_TEXT )
+	{
+      ty_ClearOSD( 1 );
+		return;
+	}
+
+	for ( index = 0 ; index < numLines ; index++ )
+	{
+		strcpy( ty_OSD1.text[ TY_CC_Y_Offset + dest ],
+			ty_OSD1.text[ TY_CC_Y_Offset + source ] );
+	   dest++;
+		source++;
+	}
+	memset( ty_OSD1.text[ TY_CC_Y_Offset + source - 1 ], ' ', TY_CC_MAX_X - 1 );
+	ty_OSD1.text[ TY_CC_Y_Offset + source - 1 ][ TY_CC_MAX_X - 1 ] = 0;
+}
+
+static void ty_drawchar( char c )
+{
+   if ( c < 2 ) return;
+  
+   if ( TY_OSD_flags & TY_OSD_MODE && TY_CC_stat != TY_CCNONE && 
+      TY_CC_CUR_Y != -1 )
+      ty_DrawChar( &TY_CC_CUR_X, &TY_CC_CUR_Y, c, 4, 13 );
+
+   if ( TY_CC_ptr - TY_CC_buf > sizeof( TY_CC_buf ) - 1 ) 
+   {        // buffer overflow
+      TY_CC_ptr = TY_CC_buf;
+      memset( TY_CC_buf, 0, sizeof( TY_CC_buf ) );
+   }
+   *( TY_CC_ptr++ ) = ( c == 14 ) ? '/' : c; // swap a '/' for musical note
+}
+
+static void ty_draw()
+{
+   if ( TY_CC_ptr != TY_CC_buf && TY_OSD_flags & TY_TEXT_MODE ) 
+   {
+      if ( *( TY_CC_ptr - 1 ) == '\n' ) *( TY_CC_ptr - 1 ) = 0;
+    
+      mp_msg( MSGT_DEMUX, MSGL_V, "CC: %s\n", TY_CC_buf );
+  }
+  TY_CC_lastcap = time( NULL );
+
+  TY_CC_ptr = TY_CC_buf;
+  memset( TY_CC_buf, 0, sizeof( TY_CC_buf) );
+
+  if ( TY_OSD_flags & TY_OSD_MODE ) ty_DrawOSD();
+  if ( TY_CC_TextItalic ) TY_CC_TextItalic = 0;
+}
+
+
+static int CC_last = 0;
+static char CC_mode = 0;
+static int CC_row[] = 
+{ 
+   11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10 
+};
+
+// char specialchar[] = { '®', '°', '½', '¿', '*', '¢', '£', 14, 'à', ' ', 'è', 'â', 'ê', 'î', 'ô', 'û' };
+
+static int ty_CCdecode( char b1, char b2 )
+{
+   int x;
+   int data = ( b2 << 8 ) + b1;
+
+   if ( b1 & 0x60 )                // text
+   {
+       if ( !TY_OSD_debug && TY_CC_stat == TY_CCNONE ) return 0;
+       if ( TY_OSD_debug > 3 ) 
+       {
+          mp_msg( MSGT_DEMUX, MSGL_DBG3, "%c %c", b1, b2 );
+       }
+       ty_drawchar( b1 );
+       ty_drawchar( b2 );
+
+       if ( TY_CC_stat > 0 && TY_OSD_flags & TY_OSD_MODE ) ty_DrawOSD();
+   } 
+   else if ( ( b1 & 0x10 ) && ( b2 > 0x1F ) && ( data != CC_last ) ) 
+   {
+      #define CURRENT ( ( b1 & 0x08 ) >> 3 )
+
+      if ( CC_mode != CURRENT && TY_CC_stat != TY_CCNONE ) 
+      {
+         if ( TY_OSD_debug && TY_CC_ptr != TY_CC_buf ) ty_draw();
+         TY_CC_stat = TY_CCNONE;
+         return 0;
+      }
+
+      if ( TY_CC_stat == TY_CCNONE || TY_CC_CUR_Y == -1 ) 
+      {
+         if ( TY_CC_ptr != TY_CC_buf ) 
+         {
+            if ( TY_OSD_debug ) 
+               mp_msg( MSGT_DEMUX, MSGL_DBG3, "(TY_OSD_debug) %s\n", 
+                  TY_CC_buf );
+            TY_CC_ptr = TY_CC_buf;
+            memset(TY_CC_buf, 0, sizeof(TY_CC_buf));
+         }
+
+         if ( CC_mode != CURRENT ) return 0;
+      }
+    
+      // preamble address code (row & indent)
+      if ( b2 & 0x40 ) 
+      {
+         TY_CC_CUR_Y = CC_row[ ( ( b1 << 1 ) & 14 ) | ( ( b2 >> 5 ) & 1 ) ];
+
+			// Offset into MPlayer's Buffer
+			if ( ( TY_CC_CUR_Y >= 1 ) && ( TY_CC_CUR_Y <= 4 ) )
+			{
+				TY_CC_Y_Offset = SUB_MAX_TEXT - 5 - 1;
+			}
+			if ( ( TY_CC_CUR_Y >= 5 ) && ( TY_CC_CUR_Y <= 10 ) )
+			{
+				TY_CC_Y_Offset = SUB_MAX_TEXT - 5 - 5;
+			}
+			if ( ( TY_CC_CUR_Y >= 12 ) && ( TY_CC_CUR_Y <= 15 ) )
+			{
+				TY_CC_Y_Offset = SUB_MAX_TEXT - 5 - 12;
+			}
+
+         if ( TY_OSD_debug > 3 ) 
+            mp_msg( MSGT_DEMUX, MSGL_DBG3, "<< preamble %d >>\n", TY_CC_CUR_Y );
+
+         // we still have something in the text buffer
+         if (TY_CC_ptr != TY_CC_buf) 
+         {
+            *(TY_CC_ptr++) = '\n';
+            if ( TY_CC_TextItalic ) 
+            {
+               TY_CC_TextItalic = 0;
+            }
+         }
+
+         TY_CC_CUR_X = 1;
+         // row contains indent flag
+         if ( b2 & 0x10 )
+         {
+            for ( x = 0 ; x < ( ( b2 & 0x0F ) << 1 ) ; x++ ) 
+            {
+               TY_CC_CUR_X++;
+               *(TY_CC_ptr++) = ' ';
+            }
+         }
+      } 
+      else 
+      // !(b2 & 0x40)
+      {
+         if ( TY_OSD_debug > 3 ) 
+            mp_msg( MSGT_DEMUX, MSGL_DBG3, "<< %02x >>\n", b1 & 0x7 );
+         switch (b1 & 0x07) 
+         {
+            case 0x00:                      // attribute
+				{
+               if ( TY_OSD_debug > 1 ) 
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, "<<A: %d>>\n", b2 );
+               break;
+				}
+            case 0x01:                      // midrow or char
+				{
+               switch (b2 & 0x70) 
+               {
+                  case 0x20:                // midrow attribute change
+                  {
+                     switch (b2 & 0x0e) 
+                     {
+                        case 0x00:          // italics off
+                        {
+                           TY_CC_TextItalic = 0;
+                           *(TY_CC_ptr++) = ' ';
+                           break;
+                        }
+                        case 0x0e:          // italics on
+                        {
+                           ty_drawchar(' ');
+                           TY_CC_TextItalic = 1;
+                           break;
+                        }
+                        default:
+                        {
+                           if ( TY_OSD_debug > 1 ) 
+                              mp_msg( MSGT_DEMUX, MSGL_DBG3, "<<D: %d>>\n", 
+                                 b2 & 0x0e );
+                        }
+                     }
+                     if ( b2 & 0x01 ) 
+                     {       
+                        // TextUnderline = 1;
+                     } 
+                     else 
+                     {
+                        // TextUnderline = 0;
+                     }
+                     break;
+                  }
+                  case 0x30:                // special character..
+                  {
+                     // transparent space
+                     if ( ( b2 & 0x0f ) == 9 ) 
+                     {
+                        TY_CC_CUR_X++;
+                        *(TY_CC_ptr++) = ' ';
+                     } 
+                     else 
+                     {
+                        // ty_drawchar(specialchar[ b2 & 0x0f ] );
+                        ty_drawchar( ' ' );
+                     }
+                     break;
+                  }
+               }
+               break;
+				}
+
+            case 0x04:                      // misc
+            case 0x05:                      // misc + F
+				{
+               if ( TY_OSD_debug > 3 ) 
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, "<< misc %02x >>\n", b2 );
+               switch ( b2 ) 
+               {
+                  case 0x20:                // resume caption (new caption)
+                  {
+                     if ( TY_OSD_flags & TY_OSD_MODE && 
+                        TY_CC_stat != TY_CCPOPUP ) 
+								ty_ClearOSD( 1 );
+                     TY_CC_stat = TY_CCPOPUP;
+                     break;
+                  }
+
+                  case 0x21:                // backspace
+                  {
+                     TY_CC_CUR_X--;
+                     break;
+                  }
+              
+                  case 0x25 ... 0x27:       // 2-4 row captions
+                  {
+                     if ( TY_CC_stat == TY_CCPOPUP ) ty_ClearOSD( 1 );
+                     TY_CC_stat = b2 - 0x23;
+                     if ( TY_CC_CUR_Y < TY_CC_stat ) TY_CC_CUR_Y = TY_CC_stat;
+                     break;
+                  }
+
+                  case 0x29:                // resume direct caption
+                  {
+                     TY_CC_stat = TY_CCPAINTON;
+                     break;
+                  }
+
+                  case 0x2A:                // text restart
+                  {
+                     ty_draw();
+                     /* FALL */
+                  }
+
+                  case 0x2B:                // resume text display
+                  {
+                     TY_CC_stat = TY_CCTEXTMODE;
+                     break;
+                  }
+
+                  case 0x2C:                // erase displayed memory
+                  {
+                     TY_CC_lastcap = 0;
+                     if ( TY_OSD_flags & TY_OSD_MODE ) 
+                     {
+                        if ( TY_CC_stat > TY_CCPOPUP || TY_CC_ptr == TY_CC_buf ) 
+                        {
+                           ty_ClearOSD( 1 );
+                           ty_draw();
+                        } 
+                        else 
+                        { 
+                           ty_ClearOSD( 1 );
+
+                           // CRW - 
+                           // new buffer
+                           // Used to be a buffer swap here, dunno why
+                        }
+                     }
+                     break;
+                  }
+
+                  case 0x2D:                // carriage return
+                  {
+                     ty_draw();
+                     TY_CC_CUR_X = 1;
+                     if ( TY_OSD_flags & TY_OSD_MODE ) 
+                     {
+                        if ( TY_CC_stat > TY_CCPAINTON )
+                           ty_RollupBuf
+                           (
+                              TY_CC_CUR_Y - TY_CC_stat + 1 , 
+                              TY_CC_CUR_Y - TY_CC_stat + 2, 
+                              TY_CC_stat - 1
+                            );
+                        else
+                           TY_CC_CUR_Y++;
+                      }
+                      break;
+                  }
+
+                  case 0x2F:                // end caption + swap memory
+                  {
+                     ty_draw();
+                     /* FALL THROUGH TO 0x2E */
+                  }
+
+                  case 0x2E:                // erase non-displayed memory
+                  {
+                     if ( TY_OSD_debug && TY_CC_ptr != TY_CC_buf )
+                        mp_msg( MSGT_DEMUX, MSGL_DBG3, "(TY_OSD_debug) %s\n", 
+                           TY_CC_buf );
+                     if ( TY_OSD_flags & TY_OSD_MODE ) ty_ClearOSD( 1 );
+
+                     TY_CC_CUR_X = 1;
+                     TY_CC_CUR_Y = -1;
+              
+                     TY_CC_ptr = TY_CC_buf;
+                     memset( TY_CC_buf, 0, sizeof( TY_CC_buf ) );
+                  }
+               }
+               break;
+				}
+            case 0x07:                      // misc (TAB)
+            {
+               for ( x = 0 ; x < ( b2 - 0x20 ) ; x++ )
+                  TY_CC_CUR_X++;
+               break;
+            }
+         }
+      }
+   }
+   CC_last = data;
+   return 0;
+}
+
+// ===========================================================================
+// Extended Data Service Decoding and OSD Presentation
+// ===========================================================================
+#define XDS_BUFFER_LENGTH     ( 16 )
+#define XDS_DISPLAY_FRAMES    ( 120 )
+static char *ty_XDS_Display[ XDS_BUFFER_LENGTH ];
+static int ty_XDSAddLine = -1;
+static int ty_XDSDisplayCount = -1;
+
+
+static void ty_AddXDSToDisplay( char *format, ... )
+{
+   char line[ 80 ];
+   int  index;
+   va_list ap;
+
+   if ( ty_XDSAddLine == -1 )
+   {
+      for( index = 0 ; index < XDS_BUFFER_LENGTH ; index++ )
+      {
+         ty_XDS_Display[ index ] = 0;
+      }
+      ty_XDSAddLine = 0;
+   }
+
+   va_start( ap, format );
+   vsnprintf( line, 80, format, ap );
+   va_end( ap );
+   mp_msg( MSGT_DEMUX, MSGL_V, "XDS: %s\n", line );
+
+   if ( ty_XDSAddLine == XDS_BUFFER_LENGTH )
+   {
+      mp_msg( MSGT_DEMUX, MSGL_ERR, "XDS Buffer would have been blown\n" );
+   }
+
+   if ( ty_XDS_Display[ ty_XDSAddLine ] != 0 )
+   {
+      free( ty_XDS_Display[ ty_XDSAddLine ] );
+      ty_XDS_Display[ ty_XDSAddLine ] = 0;
+   }
+
+   ty_XDS_Display[ ty_XDSAddLine ] = malloc( strlen( line ) + 1 );
+   strcpy( ty_XDS_Display[ ty_XDSAddLine ], line );
+   ty_XDSAddLine++;
+}
+
+
+static void ty_DisplayXDSInfo()
+{
+   int index;
+   int size;
+
+   if ( ty_XDSDisplayCount == -1 )
+   {
+      for( index = 0 ; index < XDS_BUFFER_LENGTH ; index++ )
+      {
+         if ( ty_XDS_Display[ index ] != 0 )
+         {
+            break;
+         }
+      }
+      if ( index != XDS_BUFFER_LENGTH )
+      {
+         size =  strlen( ty_XDS_Display[ index ] );
+
+         // Right Justify the XDS Stuff
+         memcpy( &( ty_OSD1.text[ 0 ][ TY_CC_MAX_X - size - 1 ] ), 
+            ty_XDS_Display[ index ], size );
+         free( ty_XDS_Display[ index ] );
+         ty_XDS_Display[ index ] = 0;
+         ty_XDSDisplayCount = 0;
+         tyOSDUpdate = 1;
+
+      }
+      else
+      {
+         // We cleaned out all the XDS stuff to be displayed
+         ty_XDSAddLine = 0;
+      }
+   }
+   else
+   {
+      // We displayed that piece of XDS information long enough
+      // Lets move on
+      ty_XDSDisplayCount++;
+      if ( ty_XDSDisplayCount >= XDS_DISPLAY_FRAMES )
+      {
+		   memset( ty_OSD1.text[ 0 ], ' ', TY_CC_MAX_X - 1 );
+		      ty_OSD1.text[ 0 ][ TY_CC_MAX_X - 1 ] = 0;
+         ty_XDSDisplayCount = -1;
+         tyOSDUpdate = 1;
+      }
+   }
+}
+
+
+static int  TY_XDS_mode = 0;
+static int  TY_XDS_type = 0;
+static int  TY_XDS_length = 0;
+static char TY_XDS_checksum = 0;
+
+// Array of [ Mode ][ Type ][ Length ]
+static char TY_XDS    [ 8 ][ 25 ][ 34 ];
+static char TY_XDS_new[ 8 ][ 25 ][ 34 ];
+
+// Array of [ MPAARating|TVRating ][ NumberRatings ]
+static char *TY_XDS_CHIP[ 2 ][ 8 ] = 
+{
+   { "(NOT APPLICABLE)", "G", "PG", "PG-13", "R", "NC-17", "X", "(NOT RATED)" },
+   { "(NOT RATED)", "TV-Y", "TV-Y7", "TV-G", "TV-PG", "TV-14", "TV-MA", 
+      "(NOT RATED)" }
+};
+
+static char *TY_XDS_modes[] = 
+{
+  "CURRENT",                        // 01h-02h current program
+  "FUTURE ",                        // 03h-04h future program
+  "CHANNEL",                        // 05h-06h channel
+  "MISC.  ",                        // 07h-08h miscellaneous
+  "PUBLIC ",                        // 09h-0Ah public service
+  "RESERV.",                        // 0Bh-0Ch reserved
+  "UNDEF. ",
+  "INVALID",
+  "INVALID",
+  "INVALID"
+};
+
+static int ty_XDSdecode( char b1, char b2 )
+{
+   char line[ 80 ];
+
+   if ( b1 < 0x0F ) 
+   {                                        // start packet 
+      TY_XDS_length = 0;
+      TY_XDS_mode = b1 >> 1;                // every other mode is a resume
+      TY_XDS_type = b2;
+      TY_XDS_checksum = b1 + b2;
+      return 0;
+   }
+
+   TY_XDS_checksum += b1 + b2;
+  
+   // eof (next byte is checksum)
+   if ( b1 == 0x0F ) 
+   {        
+      // validity check
+      if ( !TY_XDS_length || TY_XDS_checksum & 0x7F )  
+      {
+         if ( TY_OSD_debug > 3 && !TY_XDS_length ) 
+         {
+            mp_msg( MSGT_DEMUX, MSGL_DBG3, 
+               "%% TY_XDS CHECKSUM ERROR (ignoring)\n" );
+         } 
+         else 
+         {
+            TY_XDS_mode = 0;
+            TY_XDS_type = 0;
+            return 1;
+         }
+      }
+
+      // check to see if the data has changed.
+      if ( strncmp( TY_XDS[ TY_XDS_mode ][ TY_XDS_type ],
+         TY_XDS_new[ TY_XDS_mode ][ TY_XDS_type ], TY_XDS_length - 1 ) ) 
+      {
+         char *TY_XDS_ptr = TY_XDS[ TY_XDS_mode ][ TY_XDS_type ];
+
+         TY_XDS_ptr[ TY_XDS_length ] = 0;
+         memcpy( TY_XDS[ TY_XDS_mode ][ TY_XDS_type ], 
+            TY_XDS_new[ TY_XDS_mode ][ TY_XDS_type ], TY_XDS_length );
+
+         // nasty hack: only print time codes if seconds are 0
+         if ( TY_XDS_mode == 3 && TY_XDS_type == 1 && 
+            !( TY_XDS_new[ 3 ][ 1 ][ 3 ] & 0x20 ) ) 
+			{
+            return 0;
+			}
+         if ( TY_XDS_mode == 0 && TY_XDS_type == 2 &&  
+            ( TY_XDS_new[ 0 ][ 2 ][ 4 ] & 0x3f ) > 1 ) 
+			{
+            return 0;
+			}
+
+         mp_msg( MSGT_DEMUX, MSGL_DBG3, "%% %s ", TY_XDS_modes[ TY_XDS_mode ] );
+
+         line[ 0 ] = 0;
+         // printf( "XDS Code %x\n", 
+			//    ( TY_XDS_mode << 9 ) + TY_XDS_type + 0x100 );
+         switch ( ( TY_XDS_mode << 9 ) + TY_XDS_type + 0x100 ) 
+         {
+            // cases are specified in 2 bytes hex representing mode, type.
+            // TY_XDS_ptr will point to the current class buffer
+            case 0x0101:                    // current
+            case 0x0301:                    // future
+            {
+               char *mon[] = 
+               { 
+                  "0", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
+                  "Aug", "Sep", "Oct", "Nov", "Dec", "13", "14", "15"
+               };
+               ty_AddXDSToDisplay( "AIR DATE: %s %2d %d:%02d:00",
+                 mon[ TY_XDS_ptr[ 3 ] & 0x0f ], 
+                 TY_XDS_ptr[ 2 ] & 0x1f,
+                 TY_XDS_ptr[ 1 ] & 0x1f, 
+                 TY_XDS_ptr[ 0 ] & 0x3f
+                 );
+
+               // Program is tape delayed
+               if ( TY_XDS_ptr[ 3 ] & 0x10 ) ty_AddXDSToDisplay( " TAPE" );
+            }
+            break;
+
+            case 0x0102:                    // current program length
+            case 0x0302:                    // future
+            {
+               ty_AddXDSToDisplay(
+                  "DURATION: %d:%02d:%02d of %d:%02d:%02d",
+                  TY_XDS_ptr[ 3 ] & 0x3f, 
+                  TY_XDS_ptr[ 2 ] & 0x3f,
+                  TY_XDS_ptr[ 4 ] & 0x3f, 
+                  TY_XDS_ptr[ 1 ] & 0x3f,
+                  TY_XDS_ptr[ 0 ] & 0x3f, 0);
+               break;
+            }
+
+            case 0x0103:                    // current program name
+            case 0x0303:                    // future
+            {
+               ty_AddXDSToDisplay( "TITLE: %s", TY_XDS_ptr ); 
+               break;
+            }
+
+            case 0x0104:                    // current program type
+            case 0x0304:                    // future
+            {
+               // for now just print out the raw data
+               // requires a 127 string array to parse
+               // properly and isn't worth it.
+               sprintf ( line, "%sGENRE:", line );
+               {
+                  int x;
+                  for ( x = 0 ; x < TY_XDS_length ; x++ )
+                     sprintf( line, "%s %02x", line, TY_XDS_ptr[ x ] );
+               }
+               ty_AddXDSToDisplay( line );
+               break;
+            }
+
+            case 0x0105:                    // current program rating
+            case 0x0305:                    // future
+            {
+               sprintf( line, "%sRATING: %s", line,
+                  TY_XDS_CHIP[ ( TY_XDS_ptr[ 0 ] & 0x08 ) >> 3 ]
+                  [ TY_XDS_ptr[ 1 ] & 0x07 ] );
+               if ( TY_XDS_ptr[ 0 ] & 0x20 ) 
+                  sprintf( line, "%s DIALOGUE", line );
+               if ( TY_XDS_ptr[ 1 ] & 0x08 ) 
+                  sprintf( line, "%s LANGUAGE", line );
+               if ( TY_XDS_ptr[ 1 ] & 0x10 ) 
+                  sprintf( line, "%s SEXUAL", line );
+               if ( TY_XDS_ptr[ 1 ] & 0x20 ) 
+                  sprintf( line, "%s VIOLENCE", line );
+               ty_AddXDSToDisplay( line );
+
+               // raw output for verification.
+               if ( TY_OSD_debug > 1 )
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, " (%02x %02x)", 
+                     TY_XDS_ptr[ 0 ], TY_XDS_ptr[ 1 ] );
+               break;
+            }
+
+            case 0x0106:                    // current program audio services
+            case 0x0306:                    // future
+            {
+               // requires table, never actually seen it used either
+               ty_AddXDSToDisplay( "AUDIO: %02x %02x", TY_XDS_ptr[ 0 ], 
+                  TY_XDS_ptr[ 1 ] );
+               break;
+            }
+
+            case 0x0109:                    // current program aspect ratio
+            case 0x0309:                    // future
+            {
+               // requires table, rare
+               ty_AddXDSToDisplay( "ASPECT: %02x %02x", 
+                  TY_XDS_ptr[ 0 ], TY_XDS_ptr[ 1 ] );
+               break;
+            }
+
+            case 0x0110 ... 0x0117:         // program description
+            {
+               ty_AddXDSToDisplay( "DESCRIP: %s", TY_XDS_ptr ); 
+               break;
+            }
+
+            case 0x0501:                    // channel network name
+            {
+               ty_AddXDSToDisplay( "NETWORK: %s", TY_XDS_ptr ); 
+               break;
+            }
+
+            case 0x0502:                    // channel network call letters
+            {
+               ty_AddXDSToDisplay( "CALLSIGN: %s", TY_XDS_ptr ); 
+               break;
+            }
+
+            case 0x0701:                    // misc. time of day
+            {
+#define TIMEZONE          ( TY_XDS[ 3 ][ 4 ][ 0 ] & 0x1f )
+#define DST               ( ( TY_XDS[ 3 ][ 4 ][ 0 ] & 0x20 ) >> 5 )
+               struct tm tm = 
+               {
+                  0,                                 // sec
+                  ( TY_XDS_ptr[ 0 ] & 0x3F ),        // min
+                  ( TY_XDS_ptr[ 1 ] & 0x1F ),        // hour
+                  ( TY_XDS_ptr[ 2 ] & 0x1F ),        // day
+                  ( TY_XDS_ptr[ 3 ] & 0x1f ) - 1,    // month
+                  ( TY_XDS_ptr[ 5 ] & 0x3f ) + 90,   // year
+                  0,                                 // day of week
+                  0,                                 // day of year
+                  0,                                 // DST
+               };
+
+               time_t time_t = mktime( &tm );
+               char *timestr;
+    
+               time_t -= ( ( TIMEZONE - DST ) * 60 * 60 );
+               timestr = ctime( &time_t );
+               timestr[ strlen( timestr ) - 1 ] = 0;
+    
+               sprintf( line, "%sCUR.TIME: %s ", line, timestr );
+               if ( TY_XDS[ 3 ][ 4 ][ 0 ] ) 
+               {
+                  sprintf( line, "%sUTC-%d", line, TIMEZONE );
+                  if (DST) sprintf( line, "%s DST", line );
+               } 
+               else
+                  sprintf( line, "%sUTC", line );
+
+               ty_AddXDSToDisplay( line );
+
+               break;
+            }
+
+            case 0x0704:                    //misc. local time zone
+            {
+               sprintf( line, "%sTIMEZONE: UTC-%d", 
+                  line, TY_XDS_ptr[ 0 ] & 0x1f );
+               if ( TY_XDS_ptr[ 0 ] & 0x20 ) sprintf( line, "%s DST", line );
+               ty_AddXDSToDisplay( line );
+               break;
+            }
+
+            default:
+            {
+               mp_msg( MSGT_DEMUX, MSGL_DBG3, "UNKNOWN CLASS %d TYPE %d", 
+                  ( TY_XDS_mode << 1 ) + 1, TY_XDS_type );
+              if ( TY_OSD_debug > 1 ) 
+              {
+                  int x;
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, "\nDUMP:\n" );
+                  for ( x = 0 ; x < TY_XDS_length ; x++ )
+                    mp_msg( MSGT_DEMUX, MSGL_DBG3, " %02x %c", 
+                       TY_XDS_ptr[ x ], TY_XDS_ptr[ x ] );
+                  mp_msg( MSGT_DEMUX, MSGL_DBG3, "\n" );
+               } 
+            }
+         }
+         if ( TY_OSD_debug > 1 ) 
+            mp_msg( MSGT_DEMUX, MSGL_DBG3, " (%d)", TY_XDS_length );
+      }
+      TY_XDS_mode = 0;
+      TY_XDS_type = 0;
+   } 
+   else if ( TY_XDS_length < 34 ) 
+   {
+      TY_XDS_new[ TY_XDS_mode ][ TY_XDS_type ][ TY_XDS_length++ ] = b1;
+      TY_XDS_new[ TY_XDS_mode ][ TY_XDS_type ][ TY_XDS_length++ ] = b2;
+   }
+   return 0;
+}
+
+
+// 42 x 10
+static char *testline = "0123456789012345678901234567890123456789012";
+
+// ===========================================================================
+// Callback from Video Display Processing to put up the OSD
+// ===========================================================================
+void ty_processuserdata( unsigned char* buf, int len )
+{
+	int index;
+
+	sub_justify = 1;
+
+	if ( subcc_enabled )
+	{
+		if ( tyOSDInited == 0 )
+		{
+			for ( index = 0; index < SUB_MAX_TEXT ; index++ )
+			{
+				ty_OSD1.text[ index ] = malloc( TY_CC_MAX_X );
+				ty_OSD2.text[ index ] = malloc( TY_CC_MAX_X );
+			}
+			ty_ClearOSD( 0 );
+			ty_OSD1.lines = SUB_MAX_TEXT;
+			ty_OSD2.lines = SUB_MAX_TEXT;
+			ty_pOSD1 = &ty_OSD1;
+			ty_pOSD2 = &ty_OSD2;
+			tyOSDUpdate = 0;
+			tyOSDInited = 1;
+		}
+
+		if ( buf[ 0 ] == 0x01 )
+		{
+			ty_CCdecode( buf[ 1 ], buf[ 2 ] );
+		}
+		if ( buf[ 0 ] == 0x02 )
+		{
+			ty_XDSdecode( buf[ 1 ], buf[ 2 ] );
+		}
+
+      ty_DisplayXDSInfo();
+
+		if ( tyOSDUpdate )
+		{
+			// for ( index = 0; index < SUB_MAX_TEXT ; index++ )
+			// {
+         //    printf( "OSD:%d:%s\n", index, ty_OSD1.text[ index ] );
+         // }
+		   vo_sub = &ty_OSD1;
+   		vo_osd_changed( OSDTYPE_SUBTITLE );
+			tyOSDUpdate = 0;
+		}
+	}
+}
+
+
+
--- a/libmpdemux/demuxer.c	Sun Jun 08 20:36:27 2003 +0000
+++ b/libmpdemux/demuxer.c	Mon Jun 09 00:24:49 2003 +0000
@@ -137,6 +137,8 @@
 extern void demux_close_ts(demuxer_t* demuxer);
 extern void demux_close_mkv(demuxer_t* demuxer);
 extern void demux_close_ra(demuxer_t* demuxer);
+extern void demux_close_ty(demuxer_t* demuxer);
+
 
 #ifdef USE_TV
 #include "tv.h"
@@ -173,6 +175,8 @@
       demux_close_fli(demuxer); break;
     case DEMUXER_TYPE_NUV:
       demux_close_nuv(demuxer); break;
+    case DEMUXER_TYPE_MPEG_TY:
+      demux_close_ty(demuxer); break;
 #if defined(USE_TV) && defined(HAVE_TV_V4L)
     case DEMUXER_TYPE_TV:
 	demux_close_tv(demuxer); break;
@@ -278,6 +282,7 @@
 int demux_fli_fill_buffer(demuxer_t *demux);
 int demux_mpg_es_fill_buffer(demuxer_t *demux);
 int demux_mpg_fill_buffer(demuxer_t *demux);
+int demux_ty_fill_buffer(demuxer_t *demux);
 int demux_avi_fill_buffer(demuxer_t *demux);
 int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t *ds);
 int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t *ds);
@@ -312,6 +317,7 @@
     case DEMUXER_TYPE_FILM: return demux_film_fill_buffer(demux);
     case DEMUXER_TYPE_BMP: return demux_bmp_fill_buffer(demux);
     case DEMUXER_TYPE_FLI: return demux_fli_fill_buffer(demux);
+    case DEMUXER_TYPE_MPEG_TY: return demux_ty_fill_buffer( demux );
     case DEMUXER_TYPE_MPEG4_ES:
     case DEMUXER_TYPE_H264_ES:
     case DEMUXER_TYPE_MPEG_ES: return demux_mpg_es_fill_buffer(demux);
@@ -881,6 +887,18 @@
 		demuxer=NULL;
 	}
 }
+//=============== Try to open as MPEG-TY file: =================
+if(file_format==DEMUXER_TYPE_UNKNOWN || file_format==DEMUXER_TYPE_MPEG_TY)
+{
+  demuxer=new_demuxer(stream,DEMUXER_TYPE_MPEG_TY,audio_id,video_id,dvdsub_id);
+  if(ds_fill_buffer(demuxer->video)){
+      mp_msg(MSGT_DEMUXER,MSGL_INFO,MSGTR_Detected_XXX_FileFormat,"TiVo (DeMuxer By WyngNut)");
+      file_format=DEMUXER_TYPE_MPEG_TY;
+  } else {
+      free_demuxer(demuxer);
+      demuxer = NULL;
+  }
+}
 //=============== Try to open as MPEG-PS file: =================
 if(file_format==DEMUXER_TYPE_UNKNOWN || file_format==DEMUXER_TYPE_MPEG_PS){
  int pes=1;
@@ -1187,6 +1205,26 @@
    sh_video=d_video->sh;sh_video->ds=d_video;
    break;
  }
+
+ case DEMUXER_TYPE_MPEG_TY: {
+  sh_video=d_video->sh;sh_video->ds=d_video;
+
+  if(audio_id!=-2) {
+   if(!ds_fill_buffer(d_audio)){
+    mp_msg(MSGT_DEMUXER,MSGL_INFO,"MPEG: " MSGTR_MissingAudioStream);
+    sh_audio=NULL;
+   } else {
+    sh_audio=d_audio->sh;sh_audio->ds=d_audio;
+    switch(d_audio->id & 0xE0){  // 1110 0000 b  (high 3 bit: type  low 5: id)
+      case 0x00: sh_audio->format=0x50;break; // mpeg
+      case 0xA0: sh_audio->format=0x10001;break;  // dvd pcm
+      case 0x80: sh_audio->format=0x2000;break; // ac3
+      default: sh_audio=NULL; // unknown type
+    }
+   }
+  }
+  break;
+ }
  case DEMUXER_TYPE_MPEG_PS: {
   sh_video=d_video->sh;sh_video->ds=d_video;
 //  if(demuxer->stream->type!=STREAMTYPE_VCD) demuxer->movi_start=0; // for VCD
@@ -1303,6 +1341,7 @@
 int demux_seek_avi(demuxer_t *demuxer,float rel_seek_secs,int flags);
 int demux_seek_asf(demuxer_t *demuxer,float rel_seek_secs,int flags);
 int demux_seek_mpg(demuxer_t *demuxer,float rel_seek_secs,int flags);
+int demux_seek_ty(demuxer_t *demuxer,float rel_seek_secs,int flags);
 int demux_seek_y4m(demuxer_t *demuxer,float rel_seek_secs,int flags);
 int demux_seek_fli(demuxer_t *demuxer,float rel_seek_secs,int flags);
 int demux_seek_film(demuxer_t *demuxer,float rel_seek_secs,int flags);
@@ -1369,6 +1408,9 @@
 
   case DEMUXER_TYPE_ASF:
       demux_seek_asf(demuxer,rel_seek_secs,flags);  break;
+
+  case DEMUXER_TYPE_MPEG_TY:
+      demux_seek_ty(demuxer,rel_seek_secs,flags);  break;
   
   case DEMUXER_TYPE_H264_ES:
   case DEMUXER_TYPE_MPEG4_ES:
@@ -1475,6 +1517,7 @@
   return NULL;
 }
 
+extern int demux_ty(demuxer_t *demuxer, int cmd, void *arg);
 extern int demux_mpg_control(demuxer_t *demuxer, int cmd, void *arg);
 extern int demux_asf_control(demuxer_t *demuxer, int cmd, void *arg);
 extern int demux_avi_control(demuxer_t *demuxer, int cmd, void *arg);
@@ -1483,6 +1526,8 @@
 
 int demux_control(demuxer_t *demuxer, int cmd, void *arg) {
     switch(demuxer->type) {
+	case DEMUXER_TYPE_MPEG_TY:
+	    return demux_ty_control(demuxer,cmd,arg);
 	case DEMUXER_TYPE_MPEG4_ES:
 	case DEMUXER_TYPE_MPEG_ES:
 	case DEMUXER_TYPE_MPEG_PS:
--- a/libmpdemux/demuxer.h	Sun Jun 08 20:36:27 2003 +0000
+++ b/libmpdemux/demuxer.h	Mon Jun 09 00:24:49 2003 +0000
@@ -41,11 +41,12 @@
 #define DEMUXER_TYPE_H264_ES 30
 #define DEMUXER_TYPE_MATROSKA 31
 #define DEMUXER_TYPE_REALAUDIO 32
+#define DEMUXER_TYPE_MPEG_TY 33
 
 // This should always match the higest demuxer type number.
 // Unless you want to disallow users to force the demuxer to some types
 #define DEMUXER_TYPE_MIN 0
-#define DEMUXER_TYPE_MAX 32
+#define DEMUXER_TYPE_MAX 33
 
 #define DEMUXER_TYPE_DEMUXERS (1<<16)
 // A virtual demuxer type for the network code
--- a/libmpdemux/video.c	Sun Jun 08 20:36:27 2003 +0000
+++ b/libmpdemux/video.c	Mon Jun 09 00:24:49 2003 +0000
@@ -176,6 +176,7 @@
  case DEMUXER_TYPE_PVA:
  case DEMUXER_TYPE_MPEG_TS:
  case DEMUXER_TYPE_MPEG_ES:
+ case DEMUXER_TYPE_MPEG_TY:
  case DEMUXER_TYPE_MPEG_PS: {
 //mpeg_header_parser:
    // Find sequence_header first:
@@ -282,6 +283,8 @@
 return 1;
 }
 
+void ty_processuserdata( unsigned char* buf, int len );
+
 static void process_userdata(unsigned char* buf,int len){
     int i;
     /* if the user data starts with "CC", assume it is a CC info packet */
@@ -289,6 +292,11 @@
 //    	mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
 	if(subcc_enabled) subcc_process_data(buf+2,len-2);
     }
+    if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
+    {
+       ty_processuserdata( buf + 2, len - 2 );
+       return;
+    }
     if(verbose<2) return;
     printf( "user_data: len=%3d  %02X %02X %02X %02X '",
 	    len, buf[0], buf[1], buf[2], buf[3]);
@@ -312,6 +320,7 @@
 
   if(demuxer->file_format==DEMUXER_TYPE_MPEG_ES || demuxer->file_format==DEMUXER_TYPE_MPEG_PS
 		  || demuxer->file_format==DEMUXER_TYPE_PVA || demuxer->file_format==DEMUXER_TYPE_MPEG_TS
+		  || demuxer->file_format==DEMUXER_TYPE_MPEG_TY
 #ifdef STREAMING_LIVE_DOT_COM
     || (demuxer->file_format==DEMUXER_TYPE_RTP && demux_is_mpeg_rtp_stream(demuxer))
 #endif
@@ -473,7 +482,8 @@
     
     if(demuxer->file_format==DEMUXER_TYPE_MPEG_PS ||
        demuxer->file_format==DEMUXER_TYPE_MPEG_TS ||
-       demuxer->file_format==DEMUXER_TYPE_MPEG_ES){
+       demuxer->file_format==DEMUXER_TYPE_MPEG_ES ||
+       demuxer->file_format==DEMUXER_TYPE_MPEG_TY){
 
 //	if(pts>0.0001) printf("\r!!! pts: %5.3f [%d] (%5.3f)   \n",pts,picture_coding_type,i_pts);
 
--- a/libvo/sub.c	Sun Jun 08 20:36:27 2003 +0000
+++ b/libvo/sub.c	Mon Jun 09 00:24:49 2003 +0000
@@ -42,6 +42,7 @@
 int sub_visibility=1;
 int sub_bg_color=0; /* subtitles background color */
 int sub_bg_alpha=0;
+int sub_justify=0;
 
 // return the real height of a char:
 static inline int get_height(int c,int h){
@@ -449,6 +450,7 @@
     obj->bbox.y2 = obj->y + h;
 
     // calculate bbox:
+    if (sub_justify) xmin = 10;
     obj->bbox.x1=xmin;
     obj->bbox.x2=xmax;
     obj->bbox.y1=obj->y;
@@ -462,6 +464,7 @@
     i=j=0;
     if ((l=obj->params.subtitle.lines)) for (;;) {
  	 x=obj->params.subtitle.xtbl[i++]; 
+	 if (sub_justify) x = 10;
 	 prevc = -1;
 	 while ((c=obj->params.subtitle.utbl[j++])){
 	       x += kerning(vo_font,prevc,c);