MythTV  master
teletextdecoder.cpp
Go to the documentation of this file.
1 /* =============================================================
2  * File : vbidecoder.cpp
3  * Author: Frank Muenchow <beebof@gmx.de>
4  * Martin Barnasconi
5  * Date : 2005-10-25
6  *
7  * This program is free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General
9  * Public License as published by the Free Software Foundation;
10  * either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * ============================================================= */
19 #include <cctype>
20 #include <cstdint>
21 #include <cstring>
22 
23 extern "C" {
24 #include <cinttypes>
25 #include "ivtv_myth.h"
26 #include "vbitext/vt.h"
27 }
28 
29 using namespace std;
30 
31 #include "osd.h"
32 #include "teletextdecoder.h"
33 #include "teletextreader.h"
34 #include "vbilut.h"
35 #include "mythlogging.h"
36 
43 void TeletextDecoder::Decode(const unsigned char *buf, int vbimode)
44 {
45  int err = 0, latin1 = -1, pagenum, subpagenum, lang, flags;
46  uint magazine, packet, header;
47 
48  if (!m_teletext_reader)
49  return;
50 
51  m_decodertype = vbimode;
52 
53  switch (vbimode)
54  {
55  case VBI_IVTV:
56  header = hamm16(buf, &err);
57 
58  if (err & 0xf000)
59  return; // error in data header
60 
61  magazine = header & 7;
62  packet = (header >> 3) & 0x1f;
63 
64  buf += 2;
65  break;
66 
67  case VBI_DVB:
68  case VBI_DVB_SUBTITLE:
69  // zahl1 = hamm84(buf,&err) * 16 + hamm84(buf+1,&err);
70 
71  magazine = 0;
72  if (buf[0] & 0x40)
73  magazine += 1;
74  if (buf[0] & 0x10)
75  magazine += 2;
76  if (buf[0] & 0x04)
77  magazine += 4;
78 
79  packet = 0;
80  if (buf[0] & 0x01)
81  packet += 1;
82  if (buf[1] & 0x40)
83  packet += 2;
84  if (buf[1] & 0x10)
85  packet += 4;
86  if (buf[1] & 0x04)
87  packet += 8;
88  if (buf[1] & 0x01)
89  packet += 16;
90 
91  if (err == 1)
92  return; // error in data header
93 
94  buf += 2;
95  break;
96 
97  default:
98  return; // error in vbimode
99  }
100 
101  switch (packet)
102  {
103  case 0: // Page Header
104  int b1, b2, b3, b4;
105  switch (vbimode)
106  {
107  case VBI_IVTV:
108  b1 = hamm16(buf, &err);// page number
109  b2 = hamm16(buf+2, &err);// subpage number + flags
110  b3 = hamm16(buf+4, &err);// subpage number + flags
111  b4 = hamm16(buf+6, &err);// language code + more flags
112  if (err & 0xf000)
113  return;
114 
115  break;
116 
117  case VBI_DVB:
118  case VBI_DVB_SUBTITLE:
119  b1 = hamm84(buf+1, &err)*16+hamm84(buf, &err);
120  b2 = hamm84(buf+3, &err)*16+hamm84(buf+2, &err);
121  b3 = hamm84(buf+5, &err)*16+hamm84(buf+4, &err);
122  b4 = hamm84(buf+7, &err)*16+hamm84(buf+6, &err);
123  if (err == 1)
124  return;
125 
126  break;
127 
128  default:
129  return; // error in vbimode
130  }
131 
132  subpagenum= (b2 + b3 * 256) & 0x3f7f;
133  pagenum = (magazine ? magazine : 8)*256 + b1;
134 
135  lang = "\0\4\2\6\1\5\3\7"[b4 >> 5] + (latin1 ? 0 : 8);
136  flags = b4 & 0x1F;
137  flags |= b3 & 0xC0;
138  flags |= (b2 & 0x80) >> 2;
139  m_teletext_reader->AddPageHeader(pagenum, subpagenum, buf,
140  vbimode, lang, flags);
141 
142  break;
143 
144  default: // Page Data
145  m_teletext_reader->AddTeletextData((magazine ? magazine : 8), packet,
146  buf, vbimode);
147  break;
148  }
149 }
int latin1
Definition: lang.c:5
< DVB packet
Definition: vbilut.h:24
vbimode
Definition: vbilut.h:20
int hamm84(const uint8_t *p, int *err)
Definition: vbilut.cpp:335
void Decode(const unsigned char *buf, int vbimode)
Decodes teletext data.
unsigned int uint
Definition: compat.h:140
< IVTV packet
Definition: vbilut.h:23
int hamm16(const unsigned char *p, int *err)
Definition: hamm.c:207