1 #include <lib/base/eerror.h>
2 #include <lib/dvb/radiotext.h>
3 #include <lib/dvb/idemux.h>
4 #include <lib/gdi/gpixmap.h>
6 DEFINE_REF(eDVBRdsDecoder);
8 eDVBRdsDecoder::eDVBRdsDecoder(iDVBDemux *demux)
9 :msgPtr(0), bsflag(0), qdar_pos(0), t_ptr(0), qdarmvi_show(0), state(0)
12 setStreamID(0xC0, 0xC0);
14 memset(rass_picture_mask, 0, sizeof(rass_picture_mask));
15 memset(rtp_item, 0, sizeof(rtp_item));
17 if (demux->createPESReader(eApp, m_pes_reader))
18 eDebug("failed to create PES reader!");
20 m_pes_reader->connectRead(slot(*this, &eDVBRdsDecoder::processData), m_read_connection);
21 CONNECT(m_abortTimer.timeout, eDVBRdsDecoder::abortNonAvail);
24 eDVBRdsDecoder::~eDVBRdsDecoder()
26 // delete cached rass slides
27 for (int page=0; page < 10; ++page)
29 unsigned char mask = rass_picture_mask[(page*4)/8];
37 std::string filename = getRassPicture(page, subpage);
38 if (filename.length())
39 remove(filename.c_str());
45 remove("/tmp/RassLast.mvi");
48 #define SWAP(x) ((x<<8)|(x>>8))
49 #define LO(x) (x&0xFF)
51 static inline unsigned short crc_ccitt_byte( unsigned short crc, unsigned char c )
54 crc = crc ^ (LO(crc) >> 4);
55 crc = crc ^ (SWAP(LO(crc)) << 4) ^ (LO(crc) << 5);
59 static int bitrate[3][3][16] = {
62 {-1,8000,16000,24000,32000,40000,48000,56000,64000,80000,96000,112000,128000,144000,160000,0},
64 {-1,8000,16000,24000,32000,40000,48000,56000,64000,80000,96000,112000,128000,144000,160000,0},
66 {-1,32000,48000,56000,64000,80000,96000,112000,128000,144000,160000,176000,192000,224000,256000,0}
70 {-1,32000,40000,48000,56000,64000,80000,96000,112000,128000,160000,192000,224000,256000,320000,0},
72 {-1,32000,48000,56000,64000,80000,96000,112000,128000,160000,192000,224000,256000,320000,384000,0},
74 {-1,32000,64000,96000,128000,160000,192000,224000,256000,288000,320000,352000,384000,416000,448000,0}
78 {-1,6000,8000,10000,12000,16000,20000,24000,28000,320000,40000,48000,56000,64000,80000,0},
80 {-1,6000,8000,10000,12000,16000,20000,24000,28000,320000,40000,48000,56000,64000,80000,0},
82 {-1,8000,12000,16000,20000,24000,32000,40000,48000,560000,64000,80000,96000,112000,128000,0}
86 static int frequency[3][4] = {
87 // MPEG2 - 22.05, 24, 16khz
88 { 22050,24000,16000,0 },
89 // MPEG1 - 44.1, 48, 32khz
90 { 44100,48000,32000,0 },
91 // MPEG2.5 - 11.025, 12, 8khz
92 { 11025,12000,8000,0 }
95 void eDVBRdsDecoder::connectEvent(const Slot1<void, int> &slot, ePtr<eConnection> &connection)
97 connection = new eConnection(this, m_event.connect(slot));
100 void eDVBRdsDecoder::addToPictureMask(int id)
102 int page = id / 1000;
103 int tmp = page > 0 ? id / page : id;
111 int index = (page*4+subpage)/8;
112 int val = (page%2) ? 16 * (1 << subpage) : (1 << subpage);
113 if (rass_picture_mask[index] & val) // already have this picture
115 rass_picture_mask[index] |= val;
116 /* emit */ m_event(RassInteractivePicMaskChanged);
119 void eDVBRdsDecoder::removeFromPictureMask(int id)
121 int page = id / 1000;
122 int tmp = page > 0 ? id / page : id;
130 int index = (page*4)/8;
131 int val = (page%2) ? 16 * (1 << subpage) : (1 << subpage);
132 if (rass_picture_mask[index] & val) // have this picture
134 rass_picture_mask[index] &= ~val;
135 /* emit */ m_event(RassInteractivePicMaskChanged);
139 void eDVBRdsDecoder::processPESPacket(__u8 *data, int len)
141 int pos=9+data[8];// skip pes header
146 if ((0xFF & data[pos]) != 0xFF || (0xF0 & data[pos + 1]) != 0xF0)
149 int padding_bit = (data[pos + 2]>>1) & 1;
150 int mode = (data[pos + 3]>>6) & 3;
151 int channel = mode == 3 ? 1 : 2;
152 int id = (data[pos + 1] >> 3) & 1;
153 int emphasis_bit = data[pos + 3] & 3;
154 int protection_bit = data[pos + 1] & 1;
156 int sample_freq = -1;
159 if (emphasis_bit == 2 && id == 1 )
162 if ((layer = (data[pos + 1]>>1) & 3) < 1)
165 if ((rate = bitrate[id][layer - 1][(data[pos + 2]>>4) & 0xf]) < 1)
168 if ((sample_freq = frequency[id][(data[pos + 2]>>2) & 3]) == 0)
171 if (id == 1 && layer == 2)
173 if (rate / channel < 32000)
175 if (rate / channel > 192000)
179 int frame_size = layer < 3 ?
180 (144 * rate / sample_freq) + padding_bit :
181 ((12 * rate / sample_freq) * 4) + (4 * padding_bit);
186 // eDebug("protection_bit ? %d", protection_bit);
187 // int offs = protection_bit ? pos - 1 : pos - 3;
188 // if (data[offs] != 0xFD)
190 // eDebug("%02x %02x %02x %02x %02x", data[offs-2], data[offs-1], data[offs], data[offs+1], data[offs+2]);
195 if (data[offs] == 0xFD)
198 int ancillary_len = 1 + data[offs - 1];
199 offs -= ancillary_len;
200 gotAncillaryData(data+offs, ancillary_len);
205 void eDVBRdsDecoder::process_qdar(unsigned char *buf)
207 if (buf[0] == 0x40 && buf[1] == 0xDA)
209 unsigned int item,cnt,ctrl,item_type;
210 unsigned long item_length,id,item_no,ptr,tmp;
211 unsigned short crc_qdar,crc_read;
214 item=buf[2]<<8; // Number of Items
217 while ( cnt++ < item ) //read in items
219 id=buf[ptr++]<<8; //QDarID
222 item_no=buf[ptr++]<<8; // Item Number
225 ctrl=buf[ptr++]; //controlbyte
226 item_type=buf[ptr++]; //item type
228 item_length=buf[ptr++]<<24; // Item length
229 item_length|=buf[ptr++]<<16;
230 item_length|=buf[ptr++]<<8;
231 item_length|=buf[ptr++];
233 ptr=ptr+4; // rfu Bytes ... not used
236 while (tmp < ptr+item_length)
237 crc_qdar = crc_ccitt_byte(crc_qdar, buf[tmp++]);
239 crc_read=buf[ptr+item_length]<<8;
240 crc_read|=buf[ptr+item_length+1];
241 //eDebug("[RDS/Rass] CRC read: %04X calculated: %04X",crc_read,crc_qdar^0xFFFF);
243 if (crc_read == (crc_qdar^0xFFFF)) // process item
247 case 0x01: //Stillframe
248 if (ctrl&0x01) // display slide
250 sprintf(fname,"/tmp/RassLast.mvi");
251 FILE *fh=fopen(fname,"wb");
252 fwrite(buf+ptr,1,item_length-2,fh);
254 /*emit*/ m_event(RecvRassSlidePic);
257 if (ctrl&0x02) // save slide for interactive mode
259 if (id == 0 || id >= 1000)
261 sprintf(fname,"/tmp/Rass%04d.mvi",(int)id);
262 FILE *fh=fopen(fname,"wb");
263 fwrite(buf+ptr,1,item_length-2,fh);
265 addToPictureMask(id);
268 eDebug("ignore recv interactive picture id %lu", id);
270 if (ctrl&0x04) // display slide if nothing had been displayed yet
272 if (qdarmvi_show != 1)
274 sprintf(fname,"/tmp/RassLast.mvi");
275 FILE *fh=fopen(fname,"wb");
276 fwrite(buf+ptr,1,item_length-2,fh);
278 /*emit*/ m_event(RecvRassSlidePic);
282 if (ctrl&0x08) // delete slide
284 eDebug("delete slide id %lu, item_no %lu", id, item_no);
285 if (id == 0 || id >= 1000)
287 eDebug("delete %lu", id);
288 removeFromPictureMask(id);
289 sprintf(fname,"/tmp/Rass%04d.mvi",(int)id); // was item_no ? ! ?
293 eDebug("ignore del interactive picture id %lu", id);
296 default: //nothing more yet defined
302 eDebug("[RDS/Rass] CRC error, skip Rass-Qdar-Item");
310 eDebug("[RDS/Rass] No Rass-QDAR archive (%02X %02X) so skipping !\n",buf[0],buf[1]);
314 inline void eDVBRdsDecoder::gotAncillaryData(__u8 *buf, int len)
319 unsigned char c = buf[--len];
321 if (bsflag == 1) // byte stuffing
326 case 0x00: c=0xFD; break;
327 case 0x01: c=0xFE; break;
328 case 0x02: c=0xFF; break;
332 if (c == 0xFD && bsflag ==0)
341 if (( state >= 1 && state < 11 ) || ( state >=26 && state < 36 ))
342 crc = crc_ccitt_byte(crc, c);
347 if ( c==0xFE ) // Startkennung
350 case 1: // 10bit Site Address + 6bit Encoder Address
352 case 3: // Sequence Counter
362 case 0x0A: // Radiotext
365 case 0x46: // Radiotext Plus tags
371 default: // reset to state 0
377 case 6: // Data Set Number ... ignore
378 case 7: // Program Service Number ... ignore
381 case 8: // Message Element Length
383 if ( !text_len || text_len > 65 || text_len > leninfo-4)
392 case 9: // Radio Text Status bit:
393 // 0 = AB-flagcontrol
394 // 1-4 = Transmission-Number
395 // 5-6 = Buffer-Config
396 ++state; // ignore ...
399 // TODO build a complete radiotext charcode to UTF8 conversion table for all character > 0x80
402 case 0 ... 0x7f: break;
403 case 0x8d: c='ß'; break;
404 case 0x91: c='ä'; break;
405 case 0xd1: c='Ä'; break;
406 case 0x97: c='ö'; break;
407 case 0xd7: c='Ö'; break;
408 case 0x99: c='ü'; break;
409 case 0xd9: c='Ü'; break;
410 default: c=' '; break; // convert all unknown to space
425 while(message[msgPtr] == ' ' && msgPtr > 0)
426 message[msgPtr--] = 0;
427 if ( crc16 == (crc^0xFFFF) )
429 eDebug("radiotext: (%s)", message);
430 /*emit*/ m_event(RadioTextChanged);
431 memcpy(lastmessage,message,66);
434 eDebug("invalid radiotext crc (%s)", message);
447 case 27: // SID not used atm
450 case 28: // SID not used atm
453 case 29: // PNR packet number
457 case 30: // PNR packet number
461 case 31: // PNR packet number
465 case 32: // NOP number of packets
469 case 33: // NOP number of packets
473 case 34: // NOP number of packets
478 datamessage[t_ptr++]=c;
490 //eDebug("[RDS/Rass] CRC read: %04X CRC calculated: %04X",crc16,crc^0xFFFF);
492 if ( crc16 == (crc^0xFFFF) )
498 memcpy(qdar+qdar_pos,datamessage,text_len2+1);
499 qdar_pos=qdar_pos+text_len2+1;
500 if (partcnt == parts)
502 process_qdar(qdar); // decode qdar archive
517 eDebug("[RDS/Rass] CRC error, skip Rass-Qdar-Packet");
518 eDebug("[RDS/Rass] CRC read: %04X CRC calculated: %04X",crc16,crc^0xFFFF);
524 // process RT plus tags ...
525 case 38: // Message Element Length
529 case 39: // Application ID
530 case 40: // always 0x4BD7 so we ignore it ;)
531 case 41: // Applicationgroup Typecode/PTY ... ignore
550 case 46: // bit 10#4 = Item Togglebit
551 // bit 10#3 = Item Runningbit
552 // Tag1: bit 10#2..11#5 = Contenttype, 11#4..12#7 = Startmarker, 12#6..12#1 = Length
554 if (lastmessage[0] == 0) // no rds message till now ? quit ...
556 int rtp_typ[2],rtp_start[2],rtp_len[2];
557 rtp_typ[0] = (0x38 & rtp_buf[0]<<3) | rtp_buf[1]>>5;
558 rtp_start[0] = (0x3e & rtp_buf[1]<<1) | rtp_buf[2]>>7;
559 rtp_len[0] = 0x3f & rtp_buf[2]>>1;
560 // Tag2: bit 12#0..13#3 = Contenttype, 13#2..14#5 = Startmarker, 14#4..14#0 = Length(5bit)
561 rtp_typ[1] = (0x20 & rtp_buf[2]<<5) | rtp_buf[3]>>3;
562 rtp_start[1] = (0x38 & rtp_buf[3]<<3) | rtp_buf[4]>>5;
563 rtp_len[1] = 0x1f & rtp_buf[4];
565 unsigned char rtplus_osd_tmp[64];
567 if (rtp_start[0] < 66 && (rtp_len[0]+rtp_start[0]) < 66)
569 memcpy(rtp_item[rtp_typ[0]],lastmessage+rtp_start[0],rtp_len[0]+1);
570 rtp_item[rtp_typ[0]][rtp_len[0]+1]=0;
573 if (rtp_typ[0] != rtp_typ[1])
575 if (rtp_start[1] < 66 && (rtp_len[1]+rtp_start[1]) < 66)
577 memcpy(rtp_item[rtp_typ[1]],lastmessage+rtp_start[1],rtp_len[1]+1);
578 rtp_item[rtp_typ[1]][rtp_len[1]+1]=0;
582 // main RTPlus item_types used by the radio stations:
591 // todo: make a window to display all saved items ...
593 //create RTPlus OSD for title/artist
596 if ( rtp_item[4][0] != 0 )//artist
597 sprintf((char*)rtplus_osd_tmp," (%s)",rtp_item[4]);
599 if ( rtp_item[1][0] != 0 )//title
600 sprintf((char*)rtplus_osd,"%s%s",rtp_item[1],rtplus_osd_tmp);
602 if ( rtplus_osd[0] != 0 )
604 /*emit*/ m_event(RtpTextChanged);
605 eDebug("RTPlus: %s",rtplus_osd);
615 std::string eDVBRdsDecoder::getRassPicture(int page, int subpage)
635 sprintf(fname,"/tmp/Rass%04d.mvi",val);
639 int eDVBRdsDecoder::start(int pid)
642 if (m_pes_reader && !(ret = m_pes_reader->start(pid)))
643 m_abortTimer.startLongTimer(20);
647 void eDVBRdsDecoder::abortNonAvail()
649 eDebug("no ancillary data in audio stream... abort radiotext pes parser");
651 m_pes_reader->stop();
654 ePyObject eDVBRdsDecoder::getRassPictureMask()
656 ePyObject ret = PyTuple_New(5);
657 PyTuple_SET_ITEM(ret, 0, PyInt_FromLong(rass_picture_mask[0]));
658 PyTuple_SET_ITEM(ret, 1, PyInt_FromLong(rass_picture_mask[1]));
659 PyTuple_SET_ITEM(ret, 2, PyInt_FromLong(rass_picture_mask[2]));
660 PyTuple_SET_ITEM(ret, 3, PyInt_FromLong(rass_picture_mask[3]));
661 PyTuple_SET_ITEM(ret, 4, PyInt_FromLong(rass_picture_mask[4]));