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