also use refcounting for eTimers
[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(eTimer::create(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
143         while (pos < len)
144         {
145                 if ((0xFF & data[pos]) != 0xFF || (0xF0 & data[pos + 1]) != 0xF0)
146                         return;
147
148                 int padding_bit = (data[pos + 2]>>1) & 1;
149                 int mode = (data[pos + 3]>>6) & 3;
150                 int channel = mode == 3 ? 1 : 2;
151                 int id = (data[pos + 1] >> 3) & 1;
152                 int emphasis_bit = data[pos + 3] & 3;
153                 //int protection_bit = data[pos + 1] & 1;
154                 int rate = -1;
155                 int sample_freq = -1;
156                 int layer = -1;
157
158                 if (emphasis_bit == 2 && id == 1 )
159                         id = 2;
160
161                 if ((layer = (data[pos + 1]>>1) & 3) < 1)
162                         return;
163
164                 if ((rate = bitrate[id][layer - 1][(data[pos + 2]>>4) & 0xf]) < 1)
165                         return;
166
167                 if ((sample_freq = frequency[id][(data[pos + 2]>>2) & 3]) == 0)
168                         return;
169
170                 if (id == 1 && layer == 2)
171                 {
172                         if (rate / channel < 32000)
173                                 return;
174                         if (rate / channel > 192000)
175                                 return;
176                 }
177
178                 int frame_size = layer < 3 ?
179                         (144 * rate / sample_freq) + padding_bit :
180                         ((12 * rate / sample_freq) * 4) + (4 * padding_bit);
181
182                 pos += frame_size;
183
184 #if 0
185 //              eDebug("protection_bit ? %d", protection_bit);
186 //              int offs = protection_bit ? pos - 1 : pos - 3;
187 //              if (data[offs] != 0xFD)
188 //                      offs += 2;
189 //              eDebug("%02x %02x %02x %02x %02x", data[offs-2], data[offs-1], data[offs], data[offs+1], data[offs+2]);
190 #else
191                 int offs = pos - 1;
192 #endif
193
194                 if (data[offs] == 0xFD)
195                 {
196                         m_abortTimer->stop();
197                         int ancillary_len = 1 + data[offs - 1];
198                         offs -= ancillary_len;
199                         gotAncillaryData(data+offs, ancillary_len);
200                 }
201         }
202 }
203
204 void eDVBRdsDecoder::process_qdar(unsigned char *buf)
205 {
206         if (buf[0] == 0x40 && buf[1] == 0xDA)
207         {
208                 unsigned int item,cnt,ctrl,item_type;
209                 unsigned long item_length,id,item_no,ptr,tmp;
210                 unsigned short crc_qdar,crc_read;
211                 char fname[50];
212                 ptr=4;cnt=0;
213                 item=buf[2]<<8; // Number of Items
214                 item|=buf[3];
215                 
216                 while ( cnt++ < item ) //read in items
217                 {
218                         id=buf[ptr++]<<8; //QDarID
219                         id|=buf[ptr++];
220                         
221                         item_no=buf[ptr++]<<8; // Item Number
222                         item_no|=buf[ptr++];
223                         
224                         ctrl=buf[ptr++]; //controlbyte
225                         item_type=buf[ptr++]; //item type
226                         
227                         item_length=buf[ptr++]<<24; // Item length
228                         item_length|=buf[ptr++]<<16;
229                         item_length|=buf[ptr++]<<8;
230                         item_length|=buf[ptr++];
231                         
232                         ptr=ptr+4; // rfu Bytes ... not used
233                         tmp=ptr; // calc crc
234                         crc_qdar=0xFFFF;
235                         while (tmp < ptr+item_length)
236                                 crc_qdar = crc_ccitt_byte(crc_qdar, buf[tmp++]);
237                 
238                         crc_read=buf[ptr+item_length]<<8;
239                         crc_read|=buf[ptr+item_length+1];
240                         //eDebug("[RDS/Rass] CRC read: %04X calculated: %04X",crc_read,crc_qdar^0xFFFF);
241
242                         if (crc_read == (crc_qdar^0xFFFF)) // process item
243                         {
244                                 switch(item_type)
245                                 {
246                                         case 0x01: //Stillframe
247                                                 if (ctrl&0x01) // display slide
248                                                 {
249                                                         sprintf(fname,"/tmp/RassLast.mvi");
250                                                         FILE *fh=fopen(fname,"wb");
251                                                         fwrite(buf+ptr,1,item_length-2,fh);
252                                                         fclose(fh);
253                                                         /*emit*/ m_event(RecvRassSlidePic);
254                                                         qdarmvi_show=1;
255                                                 }
256                                                 if (ctrl&0x02) // save slide for interactive mode
257                                                 {
258                                                         if (id == 0 || id >= 1000)
259                                                         {
260                                                                 sprintf(fname,"/tmp/Rass%04d.mvi",(int)id);
261                                                                 FILE *fh=fopen(fname,"wb");
262                                                                 fwrite(buf+ptr,1,item_length-2,fh);
263                                                                 fclose(fh);
264                                                                 addToPictureMask(id);
265                                                         }
266                                                         else
267                                                                 eDebug("ignore recv interactive picture id %lu", id);
268                                                 }
269                                                 if (ctrl&0x04) // display slide if nothing had been displayed yet
270                                                 {
271                                                         if (qdarmvi_show != 1)
272                                                         {
273                                                                 sprintf(fname,"/tmp/RassLast.mvi");
274                                                                 FILE *fh=fopen(fname,"wb");
275                                                                 fwrite(buf+ptr,1,item_length-2,fh);
276                                                                 fclose(fh);
277                                                                 /*emit*/ m_event(RecvRassSlidePic);
278                                                                 qdarmvi_show=1;
279                                                         }
280                                                 }
281                                                 if (ctrl&0x08) // delete slide
282                                                 {
283                                                         eDebug("delete slide id %lu, item_no %lu", id, item_no);
284                                                         if (id == 0 || id >= 1000)
285                                                         {
286                                                                 eDebug("delete %lu", id);
287                                                                 removeFromPictureMask(id);
288                                                                 sprintf(fname,"/tmp/Rass%04d.mvi",(int)id); // was item_no ? ! ?
289                                                                 remove(fname);
290                                                         }
291                                                         else
292                                                                 eDebug("ignore del interactive picture id %lu", id);
293                                                 }
294                                                 break;
295                                         default: //nothing more yet defined
296                                                 break;
297                                 }
298                         } 
299                         else
300                         {
301                                 eDebug("[RDS/Rass] CRC error, skip Rass-Qdar-Item");
302                         }
303                         
304                         ptr=+item_length;
305                 }
306         }
307         else
308         {
309                 eDebug("[RDS/Rass] No Rass-QDAR archive (%02X %02X) so skipping !\n",buf[0],buf[1]);
310         }
311 }
312
313 inline void eDVBRdsDecoder::gotAncillaryData(__u8 *buf, int len)
314 {
315         int cnt=buf[--len];
316         while ( cnt-- > 0 )
317         {
318                 unsigned char c = buf[--len];
319         
320                 if (bsflag == 1) // byte stuffing
321                 {
322                         bsflag=2;
323                         switch (c)
324                         {
325                                 case 0x00: c=0xFD; break;
326                                 case 0x01: c=0xFE; break;
327                                 case 0x02: c=0xFF; break;
328                         }
329                 }
330
331                 if (c == 0xFD && bsflag ==0) 
332                         bsflag=1;
333                 else
334                         bsflag=0;
335                                         
336                 if (bsflag == 0) 
337                 {
338                         if ( state == 1 )
339                                 crc=0xFFFF;
340                         if (( state >= 1 && state < 11 ) || ( state >=26 && state < 36 ))
341                                 crc = crc_ccitt_byte(crc, c);
342
343                         switch (state)
344                         {
345                                 case 0:
346                                         if ( c==0xFE )  // Startkennung
347                                                 state=1;
348                                         break;
349                                 case 1: // 10bit Site Address + 6bit Encoder Address
350                                 case 2:
351                                 case 3: // Sequence Counter
352                                         ++state;
353                                         break;
354                                 case 4:
355                                         leninfo=c;
356                                         ++state;
357                                         break;
358                                 case 5:
359                                         switch (c)
360                                         {
361                                                 case 0x0A: // Radiotext
362                                                         ++state;
363                                                         break;
364                                                 case 0x46: // Radiotext Plus tags
365                                                         state=38;
366                                                         break;
367                                                 case 0xDA: // Rass
368                                                         state=26;
369                                                         break;
370                                                 default: // reset to state 0
371                                                         state=0;
372                                         }
373                                         break;
374
375                                         // process Radiotext
376                                 case 6: // Data Set Number ... ignore
377                                 case 7: // Program Service Number ... ignore
378                                         ++state;
379                                         break;
380                                 case 8: // Message Element Length
381                                         text_len=c;
382                                         if ( !text_len || text_len > 65 || text_len > leninfo-4)
383                                                 state=0;
384                                         else
385                                         {
386                                                 ++state;
387                                                 text_len-=2;
388                                                 msgPtr=0;
389                                         }
390                                         break;
391                                 case 9: // Radio Text Status bit:
392                                         // 0   = AB-flagcontrol
393                                         // 1-4 = Transmission-Number
394                                         // 5-6 = Buffer-Config
395                                         ++state; // ignore ...
396                                         break;
397                                 case 10:
398                                         // TODO build a complete radiotext charcode to UTF8 conversion table for all character > 0x80
399                                         switch (c)
400                                         {
401                                                 case 0 ... 0x7f: break;
402                                                 case 0x8d: c='ß'; break;
403                                                 case 0x91: c='ä'; break;
404                                                 case 0xd1: c='Ä'; break;
405                                                 case 0x97: c='ö'; break;
406                                                 case 0xd7: c='Ö'; break;
407                                                 case 0x99: c='ü'; break;
408                                                 case 0xd9: c='Ü'; break;
409                                                 default: c=' '; break;  // convert all unknown to space
410                                         }
411                                         message[msgPtr++]=c;
412                                         if(text_len)
413                                                 --text_len;
414                                         else
415                                                 ++state;
416                                         break;
417                                 case 11:
418                                         crc16=c<<8;
419                                         ++state;
420                                         break;
421                                 case 12:
422                                         crc16|=c;
423                                         message[msgPtr--]=0;
424                                         while(message[msgPtr] == ' ' && msgPtr > 0)
425                                                 message[msgPtr--] = 0;
426                                         if ( crc16 == (crc^0xFFFF) )
427                                         {
428                                                 eDebug("radiotext: (%s)", message);
429                                                 /*emit*/ m_event(RadioTextChanged);
430                                                 memcpy(lastmessage,message,66);
431                                         }
432                                         else
433                                                 eDebug("invalid radiotext crc (%s)", message);
434                                         state=0;
435                                         break;
436
437                                 // process Rass
438                                 case 26: //MEL
439                                         text_len = c;
440                                         text_len2 = c;
441                                         ++state;
442                                         text_len-=9;
443                                         text_len2-=9;
444                                         t_ptr=0;
445                                         break;
446                                 case 27: // SID not used atm
447                                         ++state;
448                                         break;
449                                 case 28: // SID not used atm
450                                         ++state;
451                                         break;
452                                 case 29: // PNR packet number
453                                         part=c<<16;
454                                         ++state;
455                                         break;
456                                 case 30: // PNR packet number
457                                         part|=c<<8;
458                                         ++state;
459                                         break;
460                                 case 31: // PNR packet number
461                                         part|=c;
462                                         ++state;
463                                         break;
464                                 case 32: // NOP number of packets
465                                         parts=c<<16;
466                                         ++state;
467                                         break;
468                                 case 33: // NOP number of packets
469                                         parts|=c<<8;
470                                         ++state;
471                                         break;
472                                 case 34: // NOP number of packets
473                                         parts|=c;
474                                         ++state;
475                                         break;
476                                 case 35:
477                                         datamessage[t_ptr++]=c;
478                                         if(text_len) 
479                                                 --text_len;
480                                         else
481                                                 ++state;
482                                         break;
483                                 case 36:
484                                         crc16=c<<8;
485                                         ++state;
486                                         break;
487                                 case 37:
488                                         crc16|=c;
489                                         //eDebug("[RDS/Rass] CRC read: %04X CRC calculated: %04X",crc16,crc^0xFFFF);
490                                         state=0;
491                                         if ( crc16 == (crc^0xFFFF) ) 
492                                         {
493                                                 if (partcnt == -1) 
494                                                         partcnt=1;
495                                                 if (partcnt == part)
496                                                 {
497                                                         memcpy(qdar+qdar_pos,datamessage,text_len2+1);
498                                                         qdar_pos=qdar_pos+text_len2+1;
499                                                         if (partcnt == parts)
500                                                         {
501                                                                 process_qdar(qdar); // decode qdar archive
502                                                                 qdar_pos=0;
503                                                                 partcnt=-1;
504                                                         }
505                                                         else
506                                                                 ++partcnt;
507                                                 }
508                                                 else
509                                                 {
510                                                         qdar_pos=0;
511                                                         partcnt=-1;
512                                                 }
513                                         }
514                                         else
515                                         {
516                                                 eDebug("[RDS/Rass] CRC error, skip Rass-Qdar-Packet");
517                                                 eDebug("[RDS/Rass] CRC read: %04X CRC calculated: %04X",crc16,crc^0xFFFF);
518                                                 partcnt=-1;
519                                         }
520                                         state=0;
521                                         break;
522
523                                 // process RT plus tags ... 
524                                 case 38: // Message Element Length
525                                         text_len=c;     
526                                         ++state;
527                                         break;
528                                 case 39: // Application ID 
529                                 case 40: // always 0x4BD7 so we ignore it ;)
530                                 case 41: // Applicationgroup Typecode/PTY ... ignore
531                                         ++state;
532                                         break;
533                                 case 42:
534                                         rtp_buf[0]=c;
535                                         ++state;
536                                         break;
537                                 case 43:
538                                         rtp_buf[1]=c;
539                                         ++state;
540                                         break;
541                                 case 44:
542                                         rtp_buf[2]=c;
543                                         ++state;
544                                         break;
545                                 case 45:
546                                         rtp_buf[3]=c;
547                                         ++state;
548                                         break;
549                                 case 46: // bit 10#4 = Item Togglebit
550                                         // bit 10#3 = Item Runningbit
551                                         // Tag1: bit 10#2..11#5 = Contenttype, 11#4..12#7 = Startmarker, 12#6..12#1 = Length
552                                         rtp_buf[4]=c;
553                                         if (lastmessage[0] == 0) // no rds message till now ? quit ...
554                                                 break;
555                                         int rtp_typ[2],rtp_start[2],rtp_len[2];
556                                         rtp_typ[0]   = (0x38 & rtp_buf[0]<<3) | rtp_buf[1]>>5;
557                                         rtp_start[0] = (0x3e & rtp_buf[1]<<1) | rtp_buf[2]>>7;
558                                         rtp_len[0]   = 0x3f & rtp_buf[2]>>1;
559                                         // Tag2: bit 12#0..13#3 = Contenttype, 13#2..14#5 = Startmarker, 14#4..14#0 = Length(5bit)
560                                         rtp_typ[1]   = (0x20 & rtp_buf[2]<<5) | rtp_buf[3]>>3;
561                                         rtp_start[1] = (0x38 & rtp_buf[3]<<3) | rtp_buf[4]>>5;
562                                         rtp_len[1]   = 0x1f & rtp_buf[4];
563                                                                         
564                                         unsigned char rtplus_osd_tmp[64];
565                                         
566                                         if (rtp_start[0] < 66 && (rtp_len[0]+rtp_start[0]) < 66)
567                                         {
568                                                 memcpy(rtp_item[rtp_typ[0]],lastmessage+rtp_start[0],rtp_len[0]+1);
569                                                 rtp_item[rtp_typ[0]][rtp_len[0]+1]=0;
570                                         }
571                                                                         
572                                         if (rtp_typ[0] != rtp_typ[1])
573                                         {
574                                                 if (rtp_start[1] < 66 && (rtp_len[1]+rtp_start[1]) < 66)
575                                                 {
576                                                         memcpy(rtp_item[rtp_typ[1]],lastmessage+rtp_start[1],rtp_len[1]+1);
577                                                         rtp_item[rtp_typ[1]][rtp_len[1]+1]=0;
578                                                 }
579                                         }
580
581                                         // main RTPlus item_types used by the radio stations:
582                                         // 1 title
583                                         // 4 artist
584                                         // 24 info.date_time
585                                         // 31 stationname
586                                         // 32 program.now
587                                         // 39 homepage
588                                         // 41 phone.hotline
589                                         // 46 email.hotline
590                                         // todo: make a window to display all saved items ...
591         
592                                         //create RTPlus OSD for title/artist
593                                         rtplus_osd[0]=0;
594                                                                 
595                                         if ( rtp_item[4][0] != 0 )//artist
596                                                 sprintf((char*)rtplus_osd_tmp," (%s)",rtp_item[4]);
597                                                                 
598                                         if ( rtp_item[1][0] != 0 )//title
599                                                 sprintf((char*)rtplus_osd,"%s%s",rtp_item[1],rtplus_osd_tmp);
600                                                                         
601                                         if ( rtplus_osd[0] != 0 )
602                                         {
603                                                 /*emit*/ m_event(RtpTextChanged);
604                                                 eDebug("RTPlus: %s",rtplus_osd);
605                                         }
606                                                 
607                                         state=0;
608                                         break;
609                         }
610                 }
611         }
612 }
613
614 std::string eDVBRdsDecoder::getRassPicture(int page, int subpage)
615 {
616         int val=0;
617         
618         switch(subpage)
619         {
620                 case 0:
621                         val=page*1000;
622                         break;
623                 case 1:
624                         val=page*1100;
625                         break;
626                 case 2:
627                         val=page*1110;
628                         break;
629                 case 3:
630                         val=page*1111;
631                         break;
632         }
633         char fname[50];
634         sprintf(fname,"/tmp/Rass%04d.mvi",val);
635         return fname;
636 }
637
638 int eDVBRdsDecoder::start(int pid)
639 {
640         int ret = -1;
641         if (m_pes_reader && !(ret = m_pes_reader->start(pid)))
642                 m_abortTimer->startLongTimer(20);
643         return ret;
644 }
645
646 void eDVBRdsDecoder::abortNonAvail()
647 {
648         eDebug("no ancillary data in audio stream... abort radiotext pes parser");
649         if (m_pes_reader)
650                 m_pes_reader->stop();
651 }
652
653 ePyObject eDVBRdsDecoder::getRassPictureMask()
654 {
655         ePyObject ret = PyTuple_New(5);
656         PyTuple_SET_ITEM(ret, 0, PyInt_FromLong(rass_picture_mask[0]));
657         PyTuple_SET_ITEM(ret, 1, PyInt_FromLong(rass_picture_mask[1]));
658         PyTuple_SET_ITEM(ret, 2, PyInt_FromLong(rass_picture_mask[2]));
659         PyTuple_SET_ITEM(ret, 3, PyInt_FromLong(rass_picture_mask[3]));
660         PyTuple_SET_ITEM(ret, 4, PyInt_FromLong(rass_picture_mask[4]));
661         return ret;
662 }