initialize array
[enigma2.git] / lib / dvb / radiotext.cpp
1 #include <lib/base/eerror.h>
2 #include <lib/dvb/radiotext.h>
3 #include <lib/dvb/idemux.h>
4 #include <lib/gdi/gpixmap.h>
5
6 DEFINE_REF(eDVBRdsDecoder);
7
8 eDVBRdsDecoder::eDVBRdsDecoder(iDVBDemux *demux)
9         :msgPtr(0), bsflag(0), qdar_pos(0), t_ptr(0), qdarmvi_show(0), state(0)
10         ,m_abortTimer(eApp)
11 {
12         setStreamID(0xC0, 0xC0);
13
14         memset(rass_picture_mask, 0, sizeof(rass_picture_mask));
15         memset(rtp_item, 0, sizeof(rtp_item));
16
17         if (demux->createPESReader(eApp, m_pes_reader))
18                 eDebug("failed to create PES reader!");
19         else
20                 m_pes_reader->connectRead(slot(*this, &eDVBRdsDecoder::processData), m_read_connection);
21         CONNECT(m_abortTimer.timeout, eDVBRdsDecoder::abortNonAvail);
22 }
23
24 eDVBRdsDecoder::~eDVBRdsDecoder()
25 {
26         // delete cached rass slides
27         for (int page=0; page < 10; ++page)
28         {
29                 unsigned char mask = rass_picture_mask[(page*4)/8];
30                 if (page % 2)
31                         mask >>= 4;
32                 int subpage=0;
33                 while(mask)
34                 {
35                         if (mask & 1)
36                         {
37                                 std::string filename = getRassPicture(page, subpage);
38                                 if (filename.length())
39                                         remove(filename.c_str());
40                         }
41                         mask >>= 1;
42                         ++subpage;
43                 }
44         }
45         remove("/tmp/RassLast.mvi");
46 }
47
48 #define SWAP(x) ((x<<8)|(x>>8))
49 #define LO(x)   (x&0xFF)
50
51 static inline unsigned short crc_ccitt_byte( unsigned short crc, unsigned char c )
52 {
53         crc = SWAP(crc) ^ c;
54         crc = crc ^ (LO(crc) >> 4);
55         crc = crc ^ (SWAP(LO(crc)) << 4) ^ (LO(crc) << 5);
56         return crc;
57 }
58
59 static int bitrate[3][3][16] = {
60         {
61                 // MPEG-2, L3
62                 {-1,8000,16000,24000,32000,40000,48000,56000,64000,80000,96000,112000,128000,144000,160000,0}, 
63                 // MPEG-2, L2
64                 {-1,8000,16000,24000,32000,40000,48000,56000,64000,80000,96000,112000,128000,144000,160000,0},
65                 // MPEG-2, L1
66                 {-1,32000,48000,56000,64000,80000,96000,112000,128000,144000,160000,176000,192000,224000,256000,0}
67         },
68         {
69                 // MPEG-1, L3
70                 {-1,32000,40000,48000,56000,64000,80000,96000,112000,128000,160000,192000,224000,256000,320000,0}, 
71                 // MPEG-1, L2
72                 {-1,32000,48000,56000,64000,80000,96000,112000,128000,160000,192000,224000,256000,320000,384000,0},
73                 // MPEG-1, L1
74                 {-1,32000,64000,96000,128000,160000,192000,224000,256000,288000,320000,352000,384000,416000,448000,0}
75         },
76         {
77                 //MPEG-2.5, L3??
78                 {-1,6000,8000,10000,12000,16000,20000,24000,28000,320000,40000,48000,56000,64000,80000,0},
79                 //MPEG-2.5, L2
80                 {-1,6000,8000,10000,12000,16000,20000,24000,28000,320000,40000,48000,56000,64000,80000,0},
81                 //MPEG-2.5, L1
82                 {-1,8000,12000,16000,20000,24000,32000,40000,48000,560000,64000,80000,96000,112000,128000,0}
83         }
84 };
85
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 }
93 };
94
95 void eDVBRdsDecoder::connectEvent(const Slot1<void, int> &slot, ePtr<eConnection> &connection)
96 {
97         connection = new eConnection(this, m_event.connect(slot));
98 }
99
100 void eDVBRdsDecoder::addToPictureMask(int id)
101 {
102         int page = id / 1000;
103         int tmp = page > 0 ? id / page : id;
104         int subpage = 0;
105         while(tmp > 1000)
106         {
107                 ++subpage;
108                 tmp -= 1000;
109                 tmp *= 10;
110         }
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
114                 return;
115         rass_picture_mask[index] |= val;
116         /* emit */ m_event(RassInteractivePicMaskChanged);
117 }
118
119 void eDVBRdsDecoder::removeFromPictureMask(int id)
120 {
121         int page = id / 1000;
122         int tmp = page > 0 ? id / page : id;
123         int subpage = 0;
124         while(tmp > 1000)
125         {
126                 ++subpage;
127                 tmp -= 1000;
128                 tmp *= 10;
129         }
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
133         {
134                 rass_picture_mask[index] &= ~val;
135                 /* emit */ m_event(RassInteractivePicMaskChanged);
136         }
137 }
138
139 void eDVBRdsDecoder::processPESPacket(__u8 *data, int len)
140 {
141         int pos=9+data[8];// skip pes header
142         int cnt=0;
143
144         while (pos < len)
145         {
146                 if ((0xFF & data[pos]) != 0xFF || (0xF0 & data[pos + 1]) != 0xF0)
147                         return;
148
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;
155                 int rate = -1;
156                 int sample_freq = -1;
157                 int layer = -1;
158
159                 if (emphasis_bit == 2 && id == 1 )
160                         id = 2;
161
162                 if ((layer = (data[pos + 1]>>1) & 3) < 1)
163                         return;
164
165                 if ((rate = bitrate[id][layer - 1][(data[pos + 2]>>4) & 0xf]) < 1)
166                         return;
167
168                 if ((sample_freq = frequency[id][(data[pos + 2]>>2) & 3]) == 0)
169                         return;
170
171                 if (id == 1 && layer == 2)
172                 {
173                         if (rate / channel < 32000)
174                                 return;
175                         if (rate / channel > 192000)
176                                 return;
177                 }
178
179                 int frame_size = layer < 3 ?
180                         (144 * rate / sample_freq) + padding_bit :
181                         ((12 * rate / sample_freq) * 4) + (4 * padding_bit);
182
183                 pos += frame_size;
184
185 #if 0
186 //              eDebug("protection_bit ? %d", protection_bit);
187 //              int offs = protection_bit ? pos - 1 : pos - 3;
188 //              if (data[offs] != 0xFD)
189 //                      offs += 2;
190 //              eDebug("%02x %02x %02x %02x %02x", data[offs-2], data[offs-1], data[offs], data[offs+1], data[offs+2]);
191 #else
192                 int offs = pos - 1;
193 #endif
194
195                 if (data[offs] == 0xFD)
196                 {
197                         m_abortTimer.stop();
198                         int ancillary_len = 1 + data[offs - 1];
199                         offs -= ancillary_len;
200                         gotAncillaryData(data+offs, ancillary_len);
201                 }
202         }
203 }
204
205 void eDVBRdsDecoder::process_qdar(unsigned char *buf)
206 {
207         if (buf[0] == 0x40 && buf[1] == 0xDA)
208         {
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;
212                 char fname[50];
213                 ptr=4;cnt=0;
214                 item=buf[2]<<8; // Number of Items
215                 item|=buf[3];
216                 
217                 while ( cnt++ < item ) //read in items
218                 {
219                         id=buf[ptr++]<<8; //QDarID
220                         id|=buf[ptr++];
221                         
222                         item_no=buf[ptr++]<<8; // Item Number
223                         item_no|=buf[ptr++];
224                         
225                         ctrl=buf[ptr++]; //controlbyte
226                         item_type=buf[ptr++]; //item type
227                         
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++];
232                         
233                         ptr=ptr+4; // rfu Bytes ... not used
234                         tmp=ptr; // calc crc
235                         crc_qdar=0xFFFF;
236                         while (tmp < ptr+item_length)
237                                 crc_qdar = crc_ccitt_byte(crc_qdar, buf[tmp++]);
238                 
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);
242
243                         if (crc_read == (crc_qdar^0xFFFF)) // process item
244                         {
245                                 switch(item_type)
246                                 {
247                                         case 0x01: //Stillframe
248                                                 if (ctrl&0x01) // display slide
249                                                 {
250                                                         sprintf(fname,"/tmp/RassLast.mvi");
251                                                         FILE *fh=fopen(fname,"wb");
252                                                         fwrite(buf+ptr,1,item_length-2,fh);
253                                                         fclose(fh);
254                                                         /*emit*/ m_event(RecvRassSlidePic);
255                                                         qdarmvi_show=1;
256                                                 }
257                                                 if (ctrl&0x02) // save slide for interactive mode
258                                                 {
259                                                         if (id == 0 || id >= 1000)
260                                                         {
261                                                                 sprintf(fname,"/tmp/Rass%04d.mvi",(int)id);
262                                                                 FILE *fh=fopen(fname,"wb");
263                                                                 fwrite(buf+ptr,1,item_length-2,fh);
264                                                                 fclose(fh);
265                                                                 addToPictureMask(id);
266                                                         }
267                                                         else
268                                                                 eDebug("ignore recv interactive picture id %lu", id);
269                                                 }
270                                                 if (ctrl&0x04) // display slide if nothing had been displayed yet
271                                                 {
272                                                         if (qdarmvi_show != 1)
273                                                         {
274                                                                 sprintf(fname,"/tmp/RassLast.mvi");
275                                                                 FILE *fh=fopen(fname,"wb");
276                                                                 fwrite(buf+ptr,1,item_length-2,fh);
277                                                                 fclose(fh);
278                                                                 /*emit*/ m_event(RecvRassSlidePic);
279                                                                 qdarmvi_show=1;
280                                                         }
281                                                 }
282                                                 if (ctrl&0x08) // delete slide
283                                                 {
284                                                         eDebug("delete slide id %lu, item_no %lu", id, item_no);
285                                                         if (id == 0 || id >= 1000)
286                                                         {
287                                                                 eDebug("delete %lu", id);
288                                                                 removeFromPictureMask(id);
289                                                                 sprintf(fname,"/tmp/Rass%04d.mvi",(int)id); // was item_no ? ! ?
290                                                                 remove(fname);
291                                                         }
292                                                         else
293                                                                 eDebug("ignore del interactive picture id %lu", id);
294                                                 }
295                                                 break;
296                                         default: //nothing more yet defined
297                                                 break;
298                                 }
299                         } 
300                         else
301                         {
302                                 eDebug("[RDS/Rass] CRC error, skip Rass-Qdar-Item");
303                         }
304                         
305                         ptr=+item_length;
306                 }
307         }
308         else
309         {
310                 eDebug("[RDS/Rass] No Rass-QDAR archive (%02X %02X) so skipping !\n",buf[0],buf[1]);
311         }
312 }
313
314 inline void eDVBRdsDecoder::gotAncillaryData(__u8 *buf, int len)
315 {
316         int cnt=buf[--len];
317         while ( cnt-- > 0 )
318         {
319                 unsigned char c = buf[--len];
320         
321                 if (bsflag == 1) // byte stuffing
322                 {
323                         bsflag=2;
324                         switch (c)
325                         {
326                                 case 0x00: c=0xFD; break;
327                                 case 0x01: c=0xFE; break;
328                                 case 0x02: c=0xFF; break;
329                         }
330                 }
331
332                 if (c == 0xFD && bsflag ==0) 
333                         bsflag=1;
334                 else
335                         bsflag=0;
336                                         
337                 if (bsflag == 0) 
338                 {
339                         if ( state == 1 )
340                                 crc=0xFFFF;
341                         if (( state >= 1 && state < 11 ) || ( state >=26 && state < 36 ))
342                                 crc = crc_ccitt_byte(crc, c);
343
344                         switch (state)
345                         {
346                                 case 0:
347                                         if ( c==0xFE )  // Startkennung
348                                                 state=1;
349                                         break;
350                                 case 1: // 10bit Site Address + 6bit Encoder Address
351                                 case 2:
352                                 case 3: // Sequence Counter
353                                         ++state;
354                                         break;
355                                 case 4:
356                                         leninfo=c;
357                                         ++state;
358                                         break;
359                                 case 5:
360                                         switch (c)
361                                         {
362                                                 case 0x0A: // Radiotext
363                                                         ++state;
364                                                         break;
365                                                 case 0x46: // Radiotext Plus tags
366                                                         state=38;
367                                                         break;
368                                                 case 0xDA: // Rass
369                                                         state=26;
370                                                         break;
371                                                 default: // reset to state 0
372                                                         state=0;
373                                         }
374                                         break;
375
376                                         // process Radiotext
377                                 case 6: // Data Set Number ... ignore
378                                 case 7: // Program Service Number ... ignore
379                                         ++state;
380                                         break;
381                                 case 8: // Message Element Length
382                                         text_len=c;
383                                         if ( !text_len || text_len > 65 || text_len > leninfo-4)
384                                                 state=0;
385                                         else
386                                         {
387                                                 ++state;
388                                                 text_len-=2;
389                                                 msgPtr=0;
390                                         }
391                                         break;
392                                 case 9: // Radio Text Status bit:
393                                         // 0   = AB-flagcontrol
394                                         // 1-4 = Transmission-Number
395                                         // 5-6 = Buffer-Config
396                                         ++state; // ignore ...
397                                         break;
398                                 case 10:
399                                         // TODO build a complete radiotext charcode to UTF8 conversion table for all character > 0x80
400                                         switch (c)
401                                         {
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
411                                         }
412                                         message[msgPtr++]=c;
413                                         if(text_len)
414                                                 --text_len;
415                                         else
416                                                 ++state;
417                                         break;
418                                 case 11:
419                                         crc16=c<<8;
420                                         ++state;
421                                         break;
422                                 case 12:
423                                         crc16|=c;
424                                         message[msgPtr--]=0;
425                                         while(message[msgPtr] == ' ' && msgPtr > 0)
426                                                 message[msgPtr--] = 0;
427                                         if ( crc16 == (crc^0xFFFF) )
428                                         {
429                                                 eDebug("radiotext: (%s)", message);
430                                                 /*emit*/ m_event(RadioTextChanged);
431                                                 memcpy(lastmessage,message,66);
432                                         }
433                                         else
434                                                 eDebug("invalid radiotext crc (%s)", message);
435                                         state=0;
436                                         break;
437
438                                 // process Rass
439                                 case 26: //MEL
440                                         text_len = c;
441                                         text_len2 = c;
442                                         ++state;
443                                         text_len-=9;
444                                         text_len2-=9;
445                                         t_ptr=0;
446                                         break;
447                                 case 27: // SID not used atm
448                                         ++state;
449                                         break;
450                                 case 28: // SID not used atm
451                                         ++state;
452                                         break;
453                                 case 29: // PNR packet number
454                                         part=c<<16;
455                                         ++state;
456                                         break;
457                                 case 30: // PNR packet number
458                                         part|=c<<8;
459                                         ++state;
460                                         break;
461                                 case 31: // PNR packet number
462                                         part|=c;
463                                         ++state;
464                                         break;
465                                 case 32: // NOP number of packets
466                                         parts=c<<16;
467                                         ++state;
468                                         break;
469                                 case 33: // NOP number of packets
470                                         parts|=c<<8;
471                                         ++state;
472                                         break;
473                                 case 34: // NOP number of packets
474                                         parts|=c;
475                                         ++state;
476                                         break;
477                                 case 35:
478                                         datamessage[t_ptr++]=c;
479                                         if(text_len) 
480                                                 --text_len;
481                                         else
482                                                 ++state;
483                                         break;
484                                 case 36:
485                                         crc16=c<<8;
486                                         ++state;
487                                         break;
488                                 case 37:
489                                         crc16|=c;
490                                         //eDebug("[RDS/Rass] CRC read: %04X CRC calculated: %04X",crc16,crc^0xFFFF);
491                                         state=0;
492                                         if ( crc16 == (crc^0xFFFF) ) 
493                                         {
494                                                 if (partcnt == -1) 
495                                                         partcnt=1;
496                                                 if (partcnt == part)
497                                                 {
498                                                         memcpy(qdar+qdar_pos,datamessage,text_len2+1);
499                                                         qdar_pos=qdar_pos+text_len2+1;
500                                                         if (partcnt == parts)
501                                                         {
502                                                                 process_qdar(qdar); // decode qdar archive
503                                                                 qdar_pos=0;
504                                                                 partcnt=-1;
505                                                         }
506                                                         else
507                                                                 ++partcnt;
508                                                 }
509                                                 else
510                                                 {
511                                                         qdar_pos=0;
512                                                         partcnt=-1;
513                                                 }
514                                         }
515                                         else
516                                         {
517                                                 eDebug("[RDS/Rass] CRC error, skip Rass-Qdar-Packet");
518                                                 eDebug("[RDS/Rass] CRC read: %04X CRC calculated: %04X",crc16,crc^0xFFFF);
519                                                 partcnt=-1;
520                                         }
521                                         state=0;
522                                         break;
523
524                                 // process RT plus tags ... 
525                                 case 38: // Message Element Length
526                                         text_len=c;     
527                                         ++state;
528                                         break;
529                                 case 39: // Application ID 
530                                 case 40: // always 0x4BD7 so we ignore it ;)
531                                 case 41: // Applicationgroup Typecode/PTY ... ignore
532                                         ++state;
533                                         break;
534                                 case 42:
535                                         rtp_buf[0]=c;
536                                         ++state;
537                                         break;
538                                 case 43:
539                                         rtp_buf[1]=c;
540                                         ++state;
541                                         break;
542                                 case 44:
543                                         rtp_buf[2]=c;
544                                         ++state;
545                                         break;
546                                 case 45:
547                                         rtp_buf[3]=c;
548                                         ++state;
549                                         break;
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
553                                         rtp_buf[4]=c;
554                                         if (lastmessage[0] == 0) // no rds message till now ? quit ...
555                                                 break;
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];
564                                                                         
565                                         unsigned char rtplus_osd_tmp[64];
566                                         
567                                         if (rtp_start[0] < 66 && (rtp_len[0]+rtp_start[0]) < 66)
568                                         {
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;
571                                         }
572                                                                         
573                                         if (rtp_typ[0] != rtp_typ[1])
574                                         {
575                                                 if (rtp_start[1] < 66 && (rtp_len[1]+rtp_start[1]) < 66)
576                                                 {
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;
579                                                 }
580                                         }
581
582                                         // main RTPlus item_types used by the radio stations:
583                                         // 1 title
584                                         // 4 artist
585                                         // 24 info.date_time
586                                         // 31 stationname
587                                         // 32 program.now
588                                         // 39 homepage
589                                         // 41 phone.hotline
590                                         // 46 email.hotline
591                                         // todo: make a window to display all saved items ...
592         
593                                         //create RTPlus OSD for title/artist
594                                         rtplus_osd[0]=0;
595                                                                 
596                                         if ( rtp_item[4][0] != 0 )//artist
597                                                 sprintf((char*)rtplus_osd_tmp," (%s)",rtp_item[4]);
598                                                                 
599                                         if ( rtp_item[1][0] != 0 )//title
600                                                 sprintf((char*)rtplus_osd,"%s%s",rtp_item[1],rtplus_osd_tmp);
601                                                                         
602                                         if ( rtplus_osd[0] != 0 )
603                                         {
604                                                 /*emit*/ m_event(RtpTextChanged);
605                                                 eDebug("RTPlus: %s",rtplus_osd);
606                                         }
607                                                 
608                                         state=0;
609                                         break;
610                         }
611                 }
612         }
613 }
614
615 std::string eDVBRdsDecoder::getRassPicture(int page, int subpage)
616 {
617         int val=0;
618         
619         switch(subpage)
620         {
621                 case 0:
622                         val=page*1000;
623                         break;
624                 case 1:
625                         val=page*1100;
626                         break;
627                 case 2:
628                         val=page*1110;
629                         break;
630                 case 3:
631                         val=page*1111;
632                         break;
633         }
634         char fname[50];
635         sprintf(fname,"/tmp/Rass%04d.mvi",val);
636         return fname;
637 }
638
639 int eDVBRdsDecoder::start(int pid)
640 {
641         int ret = -1;
642         if (m_pes_reader && !(ret = m_pes_reader->start(pid)))
643                 m_abortTimer.startLongTimer(20);
644         return ret;
645 }
646
647 void eDVBRdsDecoder::abortNonAvail()
648 {
649         eDebug("no ancillary data in audio stream... abort radiotext pes parser");
650         if (m_pes_reader)
651                 m_pes_reader->stop();
652 }
653
654 ePyObject eDVBRdsDecoder::getRassPictureMask()
655 {
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]));
662         return ret;
663 }