[mythtv] PATCH: closed caption support

Lonnie Hutchinson chinacat at chinacat.org
Thu Mar 13 17:59:31 EST 2003


The attached patch adds closed caption suport. row and column
information is used for positioning, but I don't know if the positions
are correct, I just guessed (if someone points me at a spec I can verify
it). color hasn't been implemented.

Please test, comment, send me changes you want to it goes into cvs. I
feel it is ready to go in now, except that it isn't a bug fix and
probably shouldn't go into 0.8 for that reason.

the cc.* files go in MC/libs/libvbitext/

Thanks to Erik Arendse for a significant amount of this patch.

-------------- next part --------------
? libs/libvbitext/cc.c
? libs/libvbitext/cc.h
Index: libs/libmythtv/NuppelVideoPlayer.cpp
===================================================================
RCS file: /var/lib/cvs/MC/libs/libmythtv/NuppelVideoPlayer.cpp,v
retrieving revision 1.150
diff -u -u -d -w -r1.150 NuppelVideoPlayer.cpp
--- libs/libmythtv/NuppelVideoPlayer.cpp	13 Mar 2003 16:37:07 -0000	1.150
+++ libs/libmythtv/NuppelVideoPlayer.cpp	14 Mar 2003 01:19:43 -0000
@@ -1327,6 +1327,7 @@
             }
             else
             {
+              if (cc) {
                 memcpy(tbuffer[wtxt], strm, frameheader.packetlength);
                 txttimecodes[wtxt] = frameheader.timecode;
                 txttype[wtxt] = frameheader.comptype;
@@ -1338,6 +1339,7 @@
         }
     }
 }
+}
 
 void NuppelVideoPlayer::ReduceJitter(struct timeval *nexttrigger)
 {
@@ -1496,18 +1498,21 @@
 {
     if (cc)
     {
-        printf("turn tt off\n");
+      //printf("turn tt off\n");
+        osd->ClearAllCCText();
         cc = false;
     }
     else
     {
-        printf("turn tt on\n");
+      //printf("turn tt on\n");
         cc = true;
     }
 }
 
 void NuppelVideoPlayer::ShowText(void)
 {
+
+
     // check if subtitles need to be updated on the OSD
     if (tbuffer_numvalid() && txttimecodes[rtxt] && 
         (txttimecodes[rtxt] <= timecodes[rpos]))
@@ -1544,12 +1549,63 @@
         }
         else if (txttype[rtxt] == 'C')
         {
-            // TODO: show US close caption
-            //
-            // as far as I understand this is a stream of characters,
-            // which upon display is broken into words and lines and scrolled,
-            // vbipagenr could be used to select the streamnr like
-            // the pagenr for teletext
+	  int j;
+	  static QString *cclines[] = { 0, 0, 0 };
+
+	  unsigned char *inpos = tbuffer[rtxt];
+	  struct ccsubtitle subtitle;
+
+	  memcpy (&subtitle, inpos, sizeof (subtitle));
+	  inpos += sizeof (subtitle);
+
+	  if (subtitle.clr)
+	    {
+	      //printf ("erase displayed memory\n");
+	      for (j = 0; j < 3; j++)
+		{
+		  delete cclines[j];
+		  cclines[j] = 0;
+		}
+	      osd->ClearAllCCText ();
+	    }
+
+	  if (subtitle.len)
+	    {
+	      unsigned char *end = inpos + subtitle.len;
+	      int row = 0;
+	      while (inpos < end)
+		{
+		  unsigned char *cur = inpos;
+
+		  //null terminate at EOL
+		  while (cur < end && *cur != '\n' && *cur != 0)
+		    cur++;
+		  *cur = 0;
+
+		  if (row < 3)
+		    {
+		      if (cclines[row])
+			delete cclines[row];
+		      cclines[row++] = new QString ((const char *) inpos);
+
+		      //printf ("CC text: %s\n", inpos);
+		    }
+
+		  inpos = cur + 1;
+		}
+	      if (subtitle.rowcount != row)
+		subtitle.rowcount = row;
+
+	    }
+
+	  //redraw
+	  osd->ClearAllCCText ();
+	  for (j = 0; j < subtitle.rowcount; j++)
+	    {
+	      if (cclines[j] == 0)
+		continue;
+	      osd->AddCCText (*cclines[j], subtitle.row + j, 1, 1, false);
+	    }
         }
 
         /* update rtxt */
Index: libs/libmythtv/NuppelVideoRecorder.cpp
===================================================================
RCS file: /var/lib/cvs/MC/libs/libmythtv/NuppelVideoRecorder.cpp,v
retrieving revision 1.88
diff -u -u -d -w -r1.88 NuppelVideoRecorder.cpp
--- libs/libmythtv/NuppelVideoRecorder.cpp	13 Mar 2003 16:37:07 -0000	1.88
+++ libs/libmythtv/NuppelVideoRecorder.cpp	14 Mar 2003 01:19:44 -0000
@@ -18,8 +18,10 @@
 #include "NuppelVideoRecorder.h"
 #include "commercial_skip.h"
 
-extern "C" {
+extern "C"
+{
 #include "../libvbitext/vbi.h"
+#include "../libvbitext/cc.h"
 }
 
 #include <linux/videodev.h>
@@ -131,6 +133,8 @@
     videoFilterList = "";
 
     origaudio = new struct video_audio;
+
+    memset(&subtitle, 0, sizeof(subtitle));
 }
 
 NuppelVideoRecorder::~NuppelVideoRecorder(void)
@@ -1705,7 +1709,200 @@
     textbuffer[act]->freeToEncode = 1;
 }
 
-static void vbi_event(struct VBIData *data, struct vt_event *ev)
+
+void
+NuppelVideoRecorder::FormatCC (struct cc *cc, int data)
+{
+  static ccsubtitle subtitle;
+  static int count = 0;
+  count++;
+
+  if (count == 1)
+    memset (&subtitle, 0, sizeof (ccsubtitle));
+
+  struct timeval tnow;
+  gettimeofday (&tnow, &tzone);
+
+  int act = act_text_buffer;
+  if (!textbuffer[act]->freeToBuffer)
+    {
+      cerr << act << " ran out of free TEXT buffers :-(\n";
+      return;
+    }
+
+  // calculate timecode:
+  // compute the difference  between now and stm (start time)
+  textbuffer[act]->timecode = (tnow.tv_sec - stm.tv_sec) * 1000 +
+    tnow.tv_usec / 1000 - stm.tv_usec / 1000;
+
+  const int rowdata[] = { 11, -1, 1, 2, 3, 4, 12, 13,
+    14, 15, 5, 6, 7, 8, 9, 10
+  };
+  const char *specialchar[] =
+    { "®", "°", "½", "¿", "(TM)", "¢", "£", "o/~ ",
+    "à", " ", "è", "â", "ê", "î", "ô", "û"
+  };
+
+  int b1, b2, row, len, x;
+
+  if (data == -1)		// invalid data. flush buffers to be safe.
+    {
+      // TODO: write textbuffer[act]
+      //printf (" TODO: write textbuffer[act]\n");
+
+      memset (&subtitle, 0, sizeof (ccsubtitle));
+      memset (cc->ccbuf, 0, 2 * CC_BUFSIZE);
+      return;
+    }
+
+  b1 = data & 0x7f;
+  b2 = (data >> 8) & 0x7f;
+  len = strlen (cc->ccbuf[cc->ccmode]);
+
+  if (b1 & 0x60 && data != cc->lastcode)	// text
+    {
+      cc->ccbuf[cc->ccmode][len++] = b1;
+      if (b2 & 0x60)
+        cc->ccbuf[cc->ccmode][len++] = b2;
+      /* TODO: I don't think we need this for just showing CC?
+         if (b1 == ']' || b2 == ']')
+         webtv_check(cc->ccbuf[cc->ccmode], len);
+       */
+    }
+  else if ((b1 & 0x10) && (b2 > 0x1F) && (data != cc->lastcode))
+    //codes are always transmitted twice (apparently not,
+    // ignore the second occurance)
+    {
+      cc->ccmode = (b1 >> 3) & 1;
+      len = strlen (cc->ccbuf[cc->ccmode]);
+
+      if (b2 & 0x40)		//preamble address code (row & indent)
+	{
+	  row = rowdata[((b1 << 1) & 14) | ((b2 >> 5) & 1)];
+          subtitle.row = row;
+	  if (len != 0)
+	    cc->ccbuf[cc->ccmode][len++] = '\n';
+
+	  if (b2 & 0x10)	//row contains indent flag
+	    for (x = 0; x < (b2 & 0x0F) << 1; x++)
+	      cc->ccbuf[cc->ccmode][len++] = ' ';
+	}
+      else
+	{
+	  switch (b1 & 0x07)
+	    {
+	    case 0x00:		//attribute
+              /*
+	      printf ("<ATTRIBUTE %d %d>\n", b1, b2);
+	      fflush (stdout);
+              */
+	      break;
+
+	    case 0x01:		//midrow or char
+	      switch (b2 & 0x70)
+		{
+		case 0x20:	//midrow attribute change
+		  // TODO: we _do_ want colors, is that an attribute?
+		  break;
+
+		case 0x30:	//special character..
+		  strcat (cc->ccbuf[cc->ccmode], specialchar[b2 & 0x0f]);
+		  break;
+
+		}
+	      break;
+
+	    case 0x04:		//misc
+	    case 0x05:		//misc + F
+//                                      printf("ccmode %d cmd %02x\n",ccmode,b2);
+	      switch (b2)
+		{
+		case 0x21:	//backspace
+		  cc->ccbuf[cc->ccmode][len--] = 0;
+		  break;
+
+		case 0x25:	//2 row caption
+		  subtitle.rowcount = 1;
+		  break;
+		case 0x26:	//3 row caption
+		  subtitle.rowcount = 2;
+		  break;
+		case 0x27:	//4 row caption
+		  subtitle.rowcount = 3;
+		  break;
+		case 0x29:	//resume direct caption
+		  printf ("\nresume direct caption\n");
+                  subtitle.resumedirect = 1;
+		  break;
+		case 0x2B:	//resume text display
+                  subtitle.resumetext = 1;
+		  printf ("\nresume text\n");
+		  break;
+		case 0x2C:	//erase displayed memory
+		  subtitle.clr = 1;
+		  memcpy (textbuffer[act]->buffer, &subtitle,
+			  sizeof (ccsubtitle));
+		  textbuffer[act]->bufferlen = sizeof (ccsubtitle);
+		  textbuffer[act]->freeToBuffer = 0;
+		  act_text_buffer++;
+		  if (act_text_buffer >= text_buffer_count)
+		    act_text_buffer = 0;
+		  textbuffer[act]->freeToEncode = 1;
+		  break;
+
+		case 0x2D:	//carriage return
+		  if (cc->ccmode == 1)
+		    break;
+
+		case 0x2F:	//end caption + swap memory
+		case 0x20:	//resume caption (new caption)
+		  subtitle.len = len;
+                  if (!len)
+		     break;
+
+		  memcpy (textbuffer[act]->buffer, &subtitle,
+			  sizeof (ccsubtitle));
+		  memcpy ((char *) textbuffer[act]->buffer +
+			  sizeof (ccsubtitle), cc->ccbuf[cc->ccmode],
+			  subtitle.len);
+		  textbuffer[act]->bufferlen =
+		    subtitle.len + sizeof (ccsubtitle);
+		  textbuffer[act]->freeToBuffer = 0;
+		  act_text_buffer++;
+		  if (act_text_buffer >= text_buffer_count)
+		    act_text_buffer = 0;
+		  textbuffer[act]->freeToEncode = 1;
+
+                  /*
+		  if (subtitle.len)
+		    printf ("CC text channel %i: %s\n", cc->ccmode + 1,
+			    cc->ccbuf[cc->ccmode]);
+		  fflush (stdout);
+                  */
+		  /* fallthrough */
+		case 0x2A:	//text restart
+		case 0x2E:	//erase non-displayed memory
+		  memset (&subtitle, 0, sizeof (ccsubtitle));
+		  memset (cc->ccbuf[cc->ccmode], 0, CC_BUFSIZE);
+		  break;
+
+		}
+	      break;
+
+	    case 0x07:		//misc (TAB)
+	      for (x = 0; x < (b2 & 0x03); x++)
+		cc->ccbuf[cc->ccmode][len++] = ' ';
+	      break;
+	    }
+	}
+    }
+  cc->lastcode = data;
+}
+
+
+
+static void
+vbi_event (struct VBIData *data, struct vt_event *ev)
 {
     switch (ev->type)
     {
@@ -1726,24 +1923,50 @@
     }
 }
 
-// for now just european teletext support,
-// should be easy to add US closed captioning
-void NuppelVideoRecorder::doVbiThread(void)
+void
+NuppelVideoRecorder::doVbiThread (void)
 {
     struct vbi *vbi;
+  struct cc *cc;
+  int vbifd;
 
-    if ((vbi = vbi_open(vbidevice.ascii(), 0, 99, -1)) <= 0)
+  switch (vbimode)
+    {
+    case 1:
+      vbi = vbi_open (vbidevice.ascii (), NULL, 99, -1);
+      if (!vbi)
+    {
+        cerr << "Can't open vbi device: " << vbidevice << "\n";
+        return;
+    }
+      vbifd = vbi->fd;
+      break;
+
+    case 2:
+      cc = cc_open (vbidevice.ascii ());
+      if (!cc)
     {
         cerr << "Can't open vbi device: " << vbidevice << "\n";
         return;
     }
+      vbifd = cc->fd;
+      break;
+
+    case 3:
+      cerr << "illegal vbimode\n";
+      return;
+
+    }
+
     cout << "opened vbi device\n";
 
     struct VBIData vbicallbackdata;
 
+  if (vbimode == 1)
+    {
     vbicallbackdata.nvr = this;
-
     vbi_add_handler(vbi, (void*) vbi_event, &vbicallbackdata);
+    }
 
     while (childrenLive) 
     {
@@ -1759,9 +1982,9 @@
         tv.tv_sec = 5;
         tv.tv_usec = 0;
         FD_ZERO(&rdset);
-        FD_SET(vbi->fd, &rdset);
+      FD_SET (vbifd, &rdset);
 
-        switch (select(vbi->fd+1, &rdset, 0, 0, &tv))
+      switch (select (vbifd + 1, &rdset, 0, 0, &tv))
         {
             case -1:
                   perror("vbi select");
@@ -1771,8 +1994,9 @@
                   continue;
         }
 
-        if (vbimode == 1)
+      switch (vbimode)
         {
+	case 1:
             vbicallbackdata.foundteletextpage = false;
             vbi_handler(vbi, vbi->fd);
             if (vbicallbackdata.foundteletextpage)
@@ -1780,26 +2004,40 @@
                 // decode VBI as teletext subtitles
                 FormatTeletextSubtitles(&vbicallbackdata);
             }
-        }
-        else if (vbimode == 2)
-        {
-            char buffer[65536];
-            if (read(vbi->fd, buffer, 65536) < 65536) 
-            {
-                cerr << "Can't read vbi data\n";
-            }
-            // TODO: decode VBI as US close caption
+	  break;
+
+	case 2:
+	  // decode VBI as US close caption
+	  FormatCC (cc, cc_handler (cc));
+	  break;
+
         }
     }
 
+  switch (vbimode)
+    {
+    case 1:
     vbi_del_handler(vbi, (void*) vbi_event, &vbicallbackdata);
-
     vbi_close(vbi);
     cout << "closed vbi device\n";
+      break;
+
+    case 2:
+      cc_close (cc);
+      cout << "closed vbi device\n";
+      break;
+
+    case 3:
+      cerr << "illegal vbimode\n";
+      return;
+
 }
 
+}
 
-void NuppelVideoRecorder::doWriteThread(void)
+
+void
+NuppelVideoRecorder::doWriteThread (void)
 {
     actuallypaused = false;
     while (childrenLive)
@@ -2311,12 +2549,18 @@
     }
     else if (vbimode == 2)
     {
-        // TODO: maybe CC can be stored in the same way,
-        // perhaps the pagenr can be used to store the streamnumber
+      frameheader.comptype = 'C';	// NTSC CC
+      frameheader.packetlength = len;
+
+      ringBuffer->Write (&frameheader, FRAMEHEADERSIZE);
+      ringBuffer->Write (buf, len);
+      printf ((char *) buf);
     }
 }
 
-void NuppelVideoRecorder::GetBlankFrameMap(QMap<long long, int> &blank_frame_map)
+void
+NuppelVideoRecorder::GetBlankFrameMap (QMap < long long,
+				       int >&blank_frame_map)
 {
     QMap<long long, int>::Iterator i;
 
Index: libs/libmythtv/NuppelVideoRecorder.h
===================================================================
RCS file: /var/lib/cvs/MC/libs/libmythtv/NuppelVideoRecorder.h,v
retrieving revision 1.36
diff -u -u -d -w -r1.36 NuppelVideoRecorder.h
--- libs/libmythtv/NuppelVideoRecorder.h	6 Mar 2003 19:18:53 -0000	1.36
+++ libs/libmythtv/NuppelVideoRecorder.h	14 Mar 2003 01:19:44 -0000
@@ -22,6 +22,7 @@
 
 extern "C" {
 #include "../libavcodec/avcodec.h"
+#include "../libvbitext/cc.h"
 #include "filter.h"
 }
 
@@ -129,6 +130,7 @@
     void DoMJPEG();
 
     void FormatTeletextSubtitles(struct VBIData *vbidata);
+    void FormatCC(struct cc *cc, int data);
     
     QString sfilename;
     bool encoding;
@@ -262,6 +264,8 @@
     int hmjpg_maxw;
 
     bool cleartimeonpause;
+    ccsubtitle subtitle;
+
 };
 
 #endif
Index: libs/libmythtv/format.h
===================================================================
RCS file: /var/lib/cvs/MC/libs/libmythtv/format.h,v
retrieving revision 1.10
diff -u -u -d -w -r1.10 format.h
--- libs/libmythtv/format.h	31 Jan 2003 14:35:48 -0000	1.10
+++ libs/libmythtv/format.h	14 Mar 2003 01:19:44 -0000
@@ -160,4 +160,13 @@
     unsigned char len;
 } teletextsubtitle;
 
+typedef struct ccsubtitle
+{
+  unsigned char row;
+  unsigned char rowcount;
+  unsigned char resumedirect;
+  unsigned char resumetext;
+  unsigned char clr; // clear the display
+  unsigned char len; //length of string to follow
+} ccsubtitle;
 #endif
Index: libs/libmythtv/osdtypes.cpp
===================================================================
RCS file: /var/lib/cvs/MC/libs/libmythtv/osdtypes.cpp,v
retrieving revision 1.11
diff -u -u -d -w -r1.11 osdtypes.cpp
--- libs/libmythtv/osdtypes.cpp	22 Feb 2003 04:05:10 -0000	1.11
+++ libs/libmythtv/osdtypes.cpp	14 Mar 2003 01:19:45 -0000
@@ -1116,8 +1116,10 @@
             }
             else
             {
-                x = cc->x;
-                y = cc->y;
+                // position as if we use a fixed size font
+                // on a 18 row / 40 char grid (teletext expects us to)
+                x = cc->y * vid_width / 40;
+                y = cc->x * vid_height / 18;
             }
 
             int maxx = x + textlength;
Index: libs/libvbitext/libvbitext.pro
===================================================================
RCS file: /var/lib/cvs/MC/libs/libvbitext/libvbitext.pro,v
retrieving revision 1.1
diff -u -u -d -w -r1.1 libvbitext.pro
--- libs/libvbitext/libvbitext.pro	31 Jan 2003 14:35:48 -0000	1.1
+++ libs/libvbitext/libvbitext.pro	14 Mar 2003 01:19:45 -0000
@@ -12,6 +12,5 @@
 QMAKE_CFLAGS_DEBUG = -g -DHAVE_AV_CONFIG_H -I.. -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE
 
 # Input
-HEADERS += dllist.h hamm.h lang.h vbi.h vt.h
-SOURCES += vbi.c hamm.c lang.c
+SOURCES += vbi.c hamm.c lang.c cc.c
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cc.c
Type: text/x-c
Size: 5173 bytes
Desc: not available
Url : /pipermail/attachments/20030313/815b195d/cc.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cc.h
Type: text/x-c-header
Size: 396 bytes
Desc: not available
Url : /pipermail/attachments/20030313/815b195d/cc-0001.bin


More information about the mythtv-dev mailing list