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 "vbitext/vt.h"
26 }
27 
28 using namespace std;
29 
30 #include "osd.h"
33 #include "vbilut.h"
34 #include "mythlogging.h"
35 
42 void TeletextDecoder::Decode(const unsigned char *buf, int vbimode)
43 {
44  int err = 0;
45  int latin1 = -1;
46  int pagenum = 0;
47  int subpagenum = 0;
48  int lang = 0;
49  int flags = 0;
50  uint magazine = 0;
51  uint packet = 0;
52  uint header = 0;
53 
54  if (!m_teletextReader)
55  return;
56 
57  m_decoderType = vbimode;
58 
59  switch (vbimode)
60  {
61  case VBI_IVTV:
62  header = hamm16(buf, &err);
63 
64  if (err & 0xf000)
65  return; // error in data header
66 
67  magazine = header & 7;
68  packet = (header >> 3) & 0x1f;
69 
70  buf += 2;
71  break;
72 
73  case VBI_DVB:
74  case VBI_DVB_SUBTITLE:
75  // zahl1 = hamm84(buf,&err) * 16 + hamm84(buf+1,&err);
76 
77  magazine = 0;
78  if (buf[0] & 0x40)
79  magazine += 1;
80  if (buf[0] & 0x10)
81  magazine += 2;
82  if (buf[0] & 0x04)
83  magazine += 4;
84 
85  packet = 0;
86  if (buf[0] & 0x01)
87  packet += 1;
88  if (buf[1] & 0x40)
89  packet += 2;
90  if (buf[1] & 0x10)
91  packet += 4;
92  if (buf[1] & 0x04)
93  packet += 8;
94  if (buf[1] & 0x01)
95  packet += 16;
96 
97  if (err == 1)
98  return; // error in data header
99 
100  buf += 2;
101  break;
102 
103  default:
104  return; // error in vbimode
105  }
106 
107  int b1=0;
108  int b2=0;
109  int b3=0;
110  int b4=0;
111  switch (packet)
112  {
113  case 0: // Page Header
114  switch (vbimode)
115  {
116  case VBI_IVTV:
117  b1 = hamm16(buf, &err);// page number
118  b2 = hamm16(buf+2, &err);// subpage number + flags
119  b3 = hamm16(buf+4, &err);// subpage number + flags
120  b4 = hamm16(buf+6, &err);// language code + more flags
121  if (err & 0xf000)
122  return;
123 
124  break;
125 
126  case VBI_DVB:
127  case VBI_DVB_SUBTITLE:
128  b1 = hamm84(buf+1, &err)*16+hamm84(buf, &err);
129  b2 = hamm84(buf+3, &err)*16+hamm84(buf+2, &err);
130  b3 = hamm84(buf+5, &err)*16+hamm84(buf+4, &err);
131  b4 = hamm84(buf+7, &err)*16+hamm84(buf+6, &err);
132  if (err == 1)
133  return;
134 
135  break;
136 
137  default:
138  return; // error in vbimode
139  }
140 
141  subpagenum= (b2 + b3 * 256) & 0x3f7f;
142  pagenum = (magazine ? magazine : 8)*256 + b1;
143 
144  lang = "\0\4\2\6\1\5\3\7"[b4 >> 5] + (latin1 ? 0 : 8);
145  flags = b4 & 0x1F;
146  flags |= b3 & 0xC0;
147  flags |= (b2 & 0x80) >> 2;
148  m_teletextReader->AddPageHeader(pagenum, subpagenum, buf,
149  vbimode, lang, flags);
150 
151  break;
152 
153  default: // Page Data
154  m_teletextReader->AddTeletextData((magazine ? magazine : 8), packet,
155  buf, vbimode);
156  break;
157  }
158 }
int latin1
Definition: lang.cpp:5
< DVB packet
Definition: vbilut.h:24
vbimode
Definition: vbilut.h:20
int hamm84(const uint8_t *p, int *err)
Definition: vbilut.cpp:333
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 uint8_t *p, int *err)
Definition: vbilut.cpp:343