some dvb subtitles fixes
[enigma2.git] / lib / dvb / subtitle.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <memory.h>
5 #include <time.h>
6
7 #include <asm/types.h>
8 #include <lib/dvb/idemux.h>
9 #include <lib/dvb/subtitle.h>
10 #include <lib/base/smartptr.h>
11 #include <lib/base/eerror.h>
12 #include <lib/gdi/gpixmap.h>
13
14 void bitstream_init(bitstream *bit, const void *buffer, int size)
15 {
16         bit->data = (__u8*) buffer;
17         bit->size = size;
18         bit->avail = 8;
19         bit->consumed = 0;
20 }
21
22 int bitstream_get(bitstream *bit)
23 {
24         int val;
25         bit->avail -= bit->size;
26         val = ((*bit->data) >> bit->avail) & ((1<<bit->size) - 1);
27         if (!bit->avail)
28         {
29                 bit->data++;
30                 bit->consumed++;
31                 bit->avail = 8;
32         }
33         return val;
34 }
35
36 static int extract_pts(pts_t &pts, __u8 *pkt)
37 {
38         pkt += 7;
39         int flags = *pkt++;
40
41         pkt++; // header length
42
43         if (flags & 0x80) /* PTS present? */
44         {
45                         /* damn gcc bug */
46                 pts  = ((unsigned long long)(((pkt[0] >> 1) & 7))) << 30;
47                 pts |=   pkt[1] << 22;
48                 pts |=  (pkt[2]>>1) << 15;
49                 pts |=   pkt[3] << 7;
50                 pts |=  (pkt[5]>>1);
51
52                 return 0;
53         } else
54                 return -1;
55 }
56
57 void eDVBSubtitleParser::subtitle_process_line(subtitle_page *page, int object_id, int line, __u8 *data, int len)
58 {
59         subtitle_region *region = page->regions;
60 //      eDebug("line for %d:%d", page->page_id, object_id);
61         while (region)
62         {
63                 subtitle_region_object *object = region->region_objects;
64                 while (object)
65                 {
66                         if (object->object_id == object_id)
67                         {
68                                 int x = object->object_horizontal_position;
69                                 int y = object->object_vertical_position + line;
70                                 if (x + len > region->region_width)
71                                 {
72                                         //eDebug("[SUB] !!!! XCLIP %d + %d > %d", x, len, region->region_width);
73                                         len = region->region_width - x;
74                                 }
75                                 if (len < 0)
76                                         break;
77                                 if (y >= region->region_height)
78                                 {
79                                         //eDebug("[SUB] !!!! YCLIP %d >= %d", y, region->region_height);
80                                         break;
81                                 }
82 //                              //eDebug("inserting %d bytes (into region %d)", len, region->region_id);
83 //                              eDebug("put data to region_buffer %p", &(*region->region_buffer));
84                                 memcpy((__u8*)region->region_buffer->surface->data + region->region_width * y + x, data, len);
85                         }
86                         object = object->next;
87                 }
88                 region = region->next;
89         }
90 }
91
92 int eDVBSubtitleParser::subtitle_process_pixel_data(subtitle_page *page, int object_id, int *linenr, int *linep, __u8 *data)
93 {
94         int data_type = *data++;
95         static __u8 line[720];
96
97         bitstream bit;
98         bit.size=0;
99         switch (data_type)
100         {
101         case 0x10: // 2bit pixel data
102                 bitstream_init(&bit, data, 2);
103                 while (1)
104                 {
105                         int len=0, col=0;
106                         int code = bitstream_get(&bit);
107                         if (code)
108                         {
109                                 col = code;
110                                 len = 1;
111                         } else
112                         {
113                                 code = bitstream_get(&bit);
114                                 if (!code)
115                                 {
116                                         code = bitstream_get(&bit);
117                                         if (code == 1)
118                                         {
119                                                 col = 0;
120                                                 len = 2;
121                                         } else if (code == 2)
122                                         {
123                                                 len = bitstream_get(&bit) << 2;
124                                                 len |= bitstream_get(&bit);
125                                                 len += 12;
126                                                 col = bitstream_get(&bit);
127                                         } else if (code == 3)
128                                         {
129                                                 len = bitstream_get(&bit) << 6;
130                                                 len |= bitstream_get(&bit) << 4;
131                                                 len |= bitstream_get(&bit) << 2;
132                                                 len |= bitstream_get(&bit);
133                                                 len += 29;
134                                                 col = bitstream_get(&bit);
135                                         } else
136                                                 break;
137                                 } else if (code==1)
138                                 {
139                                         col = 0;
140                                         len = 1;
141                                 } else if (code&2)
142                                 {
143                                         if (code&1)
144                                                 len = 3 + 4 + bitstream_get(&bit);
145                                         else
146                                                 len = 3 + bitstream_get(&bit);
147                                         col = bitstream_get(&bit);
148                                 }
149                         }
150                         while (len && ((*linep) < 720))
151                         {
152                                 line[(*linep)++] = col;
153                                 len--;
154                         }
155                 }
156                 while (bit.avail != 8)
157                         bitstream_get(&bit);
158                 return bit.consumed + 1;
159         case 0x11: // 4bit pixel data
160                 bitstream_init(&bit, data, 4);
161                 while (1)
162                 {
163                         int len=0, col=0;
164                         int code = bitstream_get(&bit);
165                         if (code)
166                         {
167                                 col = code;
168                                 len = 1;
169                         } else
170                         {
171                                 code = bitstream_get(&bit);
172                                 if (!code)
173                                         break;
174                                 else if (code == 0xC)
175                                 {
176                                         col = 0;
177                                         len = 1;
178                                 } else if (code == 0xD)
179                                 {
180                                         col = 0;
181                                         len = 2;
182                                 } else if (code < 8)
183                                 {
184                                         col = 0;
185                                         len = (code & 7) + 2;
186                                 } else if ((code & 0xC) == 0x8)
187                                 {
188                                         col = bitstream_get(&bit);
189                                         len = (code & 3) + 4;
190                                 } else if (code == 0xE)
191                                 {
192                                         len = bitstream_get(&bit) + 9;
193                                         col = bitstream_get(&bit);
194                                 } else if (code == 0xF)
195                                 {
196                                         len  = bitstream_get(&bit) << 4;
197                                         len |= bitstream_get(&bit);
198                                         len += 25;
199                                         col  = bitstream_get(&bit);
200                                 }
201                         }
202                         while (len && ((*linep) < 720))
203                         {
204                                 line[(*linep)++] = col;
205                                 len--;
206                         }
207                 }
208                 while (bit.avail != 8)
209                         bitstream_get(&bit);
210                 return bit.consumed + 1;
211         case 0x12: // 8bit pixel data
212                 bitstream_init(&bit, data, 8);
213                 while(1)
214                 {
215                         int len=0, col=0;
216                         int code = bitstream_get(&bit);
217                         if (code)
218                         {
219                                 col = code;
220                                 len = 1;
221                         } else
222                         {
223                                 code = bitstream_get(&bit);
224                                 if ((code & 0x80) == 0x80)
225                                 {
226                                         len = code&0x7F;
227                                         col = bitstream_get(&bit);
228                                 } else if (code&0x7F)
229                                 {
230                                         len = code&0x7F;
231                                         col = 0;
232                                 } else
233                                         break;
234                         }
235                         while (len && ((*linep) < 720))
236                         {
237                                 line[(*linep)++] = col;
238                                 len--;
239                         }
240                 }
241                 return bit.consumed + 1;
242         case 0x20:  // ignore 2 -> 4bit map table
243                 bitstream_init(&bit, data, 4);
244                 for ( int i=0; i < 4; ++i )
245                         bitstream_get(&bit);
246                 break;
247         case 0x21:  // ignore 2 -> 8bit map table
248                 bitstream_init(&bit, data, 8);
249                 for ( int i=0; i < 4; ++i )
250                         bitstream_get(&bit);
251                 break;
252         case 0x22:  // ignore 4 -> 8bit map table
253                 bitstream_init(&bit, data, 8);
254                 for ( int i=0; i < 16; ++i )
255                         bitstream_get(&bit);
256                 break;
257         case 0xF0:
258                 subtitle_process_line(page, object_id, *linenr, line, *linep);
259 /*              {
260                         int i;
261                         for (i=0; i<720; ++i)
262                                 //eDebugNoNewLine("%d ", line[i]);
263                         //eDebug("");
264                 } */
265                 (*linenr)+=2; // interlaced
266                 *linep = 0;
267 //              //eDebug("[SUB] EOL");
268                 return 1;
269         default:
270                 eDebug("subtitle_process_pixel_data: invalid data_type %02x", data_type);
271                 return -1;
272         }
273         return 0;
274 }
275
276 int eDVBSubtitleParser::subtitle_process_segment(__u8 *segment)
277 {
278         int segment_type, page_id, segment_length, processed_length;
279         if (*segment++ !=  0x0F)
280         {
281                 eDebug("out of sync.");
282                 return -1;
283         }
284         segment_type = *segment++;
285         page_id  = *segment++ << 8;
286         page_id |= *segment++;
287         segment_length  = *segment++ << 8;
288         segment_length |= *segment++;
289         if (segment_type == 0xFF)
290                 return segment_length + 6;
291         if (page_id != m_composition_page_id && page_id != m_ancillary_page_id)
292                 return segment_length + 6;
293         //eDebug("have %d bytes of segment data", segment_length);
294
295         //eDebug("page_id %d, segtype %02x", page_id, segment_type);
296
297         subtitle_page *page, **ppage;
298
299         page = m_pages; ppage = &m_pages;
300
301         while (page)
302         {
303                 if (page->page_id == page_id)
304                         break;
305                 ppage = &page->next;
306                 page = page->next;
307         }
308
309         processed_length = 0;
310
311         switch (segment_type)
312         {
313         case 0x10: // page composition segment
314         {
315                 int page_time_out = *segment++; processed_length++;
316                 int page_version_number = *segment >> 4;
317                 int page_state = (*segment >> 2) & 0x3;
318                 //eDebug("pcs with %d bytes data (%d:%d:%d)", segment_length, page_id, page_version_number, page_state);
319                 segment++;
320                 processed_length++;
321
322                 //eDebug("page time out: %d", page_time_out);
323                 //eDebug("page_version_number: %d" ,page_version_number);
324                 //eDebug("page_state: %d", page_state);
325
326                 if (!page)
327                 {
328                         //eDebug("page not found");
329                         page = new subtitle_page;
330                         page->page_regions = 0;
331                         page->regions = 0;
332                         page->page_id = page_id;
333                         page->cluts = 0;
334                         page->next = 0;
335                         *ppage = page;
336                 } else
337                 {
338                         if (page->pcs_size != segment_length)
339                                 page->page_version_number = -1;
340                                 // if no update, just skip this data.
341                         if (page->page_version_number == page_version_number)
342                         {
343 //                              eDebug("skip data... ");
344                                 break;
345                         }
346                 }
347
348                 //eDebug("page updated: old: %d, new: %d", page->page_version_number, page_version_number);
349                         // when acquisition point or mode change: remove all displayed pages.
350                 if ((page_state == 1) || (page_state == 2))
351                 {
352                         while (page->page_regions)
353                         {
354                                 subtitle_page_region *p = page->page_regions->next;
355                                 //eDebug("delete page_region %d", page->page_regions->region_id);
356                                 delete page->page_regions;
357                                 page->page_regions = p;
358                         }
359                         while (page->regions)
360                         {
361                                 subtitle_region *p = page->regions->next;
362                                 //eDebug("delete region %d", page->regions->region_id);
363                                 delete page->regions;
364                                 page->regions = p;
365                         }
366
367                 }
368
369                 //eDebug("new page.. (%d)", page_state);
370
371                 page->page_time_out = page_time_out;
372
373                 page->page_version_number = page_version_number;
374
375                 subtitle_page_region **r = &page->page_regions;
376
377                 //eDebug("%d  / %d data left", processed_length, segment_length);
378
379                         // go to last entry
380                 while (*r)
381                         r = &(*r)->next;
382
383                 if (processed_length == segment_length && !page->page_regions)
384                 {
385                         //eDebug("no regions in page.. clear screen!!");
386                         subtitle_redraw(page->page_id);
387                 }
388
389                 while (processed_length < segment_length)
390                 {
391                         subtitle_page_region *pr;
392
393                                 // append new entry to list
394                         pr = new subtitle_page_region;
395                         pr->next = 0;
396                         *r = pr;
397                         r = &pr->next;
398
399                         pr->region_id = *segment++; processed_length++;
400                         segment++; processed_length++;
401
402                         pr->region_horizontal_address  = *segment++ << 8;
403                         pr->region_horizontal_address |= *segment++;
404                         processed_length += 2;
405
406                         pr->region_vertical_address  = *segment++ << 8;
407                         pr->region_vertical_address |= *segment++;
408                         processed_length += 2;
409
410                         //eDebug("appended active region");
411                 }
412
413                 if (processed_length != segment_length)
414                         eDebug("%d != %d", processed_length, segment_length);
415                 break;
416         }
417         case 0x11: // region composition segment
418         {
419                 int region_id = *segment++; processed_length++;
420                 int region_version_number = *segment >> 4;
421                 int region_fill_flag = (*segment >> 3) & 1;
422                 segment++; processed_length++;
423
424                         // if we didn't yet received the pcs for this page, drop the region
425                 if (!page)
426                 {
427                         eDebug("ignoring region %x, since page %02x doesn't yet exist.", region_id, page_id);
428                         break;
429                 }
430
431                 subtitle_region *region, **pregion;
432
433                 region = page->regions; pregion = &page->regions;
434
435                 while (region)
436                 {
437                         fflush(stdout);
438                         if (region->region_id == region_id)
439                                 break;
440                         pregion = &region->next;
441                         region = region->next;
442                 }
443
444                 if (!region)
445                 {
446                         //eDebug("create region !!!!!!!!!!");
447                         *pregion = region = new subtitle_region;
448                         region->next = 0;
449                         region->committed = false;
450                 }
451                 else if (region->region_version_number != region_version_number)
452                 {
453                         subtitle_region_object *objects = region->region_objects;
454                         while (objects)
455                         {
456                                 subtitle_region_object *n = objects->next;
457                                 delete objects;
458                                 objects = n;
459                         }
460                         if (region->region_buffer)
461                         {
462                                 //eDebug("no more need of region_buffer %p", &(*region->region_buffer));
463                                 region->region_buffer=0;
464                         }
465                         region->committed = false;
466                 }
467                 else
468                         break;
469
470                 //eDebug("region %d:%d update", page_id, region_id);
471
472                 region->region_id = region_id;
473                 region->region_version_number = region_version_number;
474
475                 region->region_width  = *segment++ << 8;
476                 region->region_width |= *segment++;
477                 processed_length += 2;
478
479                 region->region_height  = *segment++ << 8;
480                 region->region_height |= *segment++;
481                 processed_length += 2;
482
483                 region->region_buffer = new gPixmap(eSize(region->region_width, region->region_height), 8);
484                 //eDebug("new region_buffer %p", &(*region->region_buffer));
485
486                 int region_level_of_compatibility, region_depth;
487
488                 region_level_of_compatibility = (*segment >> 5) & 7;
489                 region_depth = (*segment++ >> 2) & 7;
490                 region->region_depth = (subtitle_region::depth) region_depth;
491                 processed_length++;
492
493                 int CLUT_id = *segment++; processed_length++;
494
495                 region->clut_id = CLUT_id;
496
497                 int region_8bit_pixel_code, region_4bit_pixel_code, region_2bit_pixel_code;
498                 region_8bit_pixel_code = *segment++; processed_length++;
499                 region_4bit_pixel_code = *segment >> 4;
500                 region_2bit_pixel_code = (*segment++ >> 2) & 3;
501                 processed_length++;
502
503                 if (!region_fill_flag)
504                 {
505                         region_2bit_pixel_code = region_4bit_pixel_code = region_8bit_pixel_code = 0;
506                         region_fill_flag = 1;
507                 }
508
509                 if (region_fill_flag)
510                 {
511                         //eDebug("region fill region_buffer %p", &(*region->region_buffer));
512                         if (region_depth == 1)
513                                 memset(region->region_buffer->surface->data, region_2bit_pixel_code, region->region_height * region->region_width);
514                         else if (region_depth == 2)
515                                 memset(region->region_buffer->surface->data, region_4bit_pixel_code, region->region_height * region->region_width);
516                         else if (region_depth == 3)
517                                 memset(region->region_buffer->surface->data, region_8bit_pixel_code, region->region_height * region->region_width);
518                         else
519                                 eDebug("!!!! invalid depth");
520                 }
521
522                 //eDebug("region %02x, version %d, %dx%d", region->region_id, region->region_version_number, region->region_width, region->region_height);
523
524                 region->region_objects = 0;
525                 subtitle_region_object **pobject = &region->region_objects;
526
527                 while (processed_length < segment_length)
528                 {
529                         subtitle_region_object *object;
530
531                         object = new subtitle_region_object;
532
533                         *pobject = object;
534                         object->next = 0;
535                         pobject = &object->next;
536
537                         object->object_id  = *segment++ << 8;
538                         object->object_id |= *segment++; processed_length += 2;
539
540                         object->object_type = *segment >> 6;
541                         object->object_provider_flag = (*segment >> 4) & 3;
542                         object->object_horizontal_position  = (*segment++ & 0xF) << 8;
543                         object->object_horizontal_position |= *segment++;
544                         processed_length += 2;
545
546                         object->object_vertical_position  = *segment++ << 8;
547                         object->object_vertical_position |= *segment++ ;
548                         processed_length += 2;
549
550                         if ((object->object_type == 1) || (object->object_type == 2))
551                         {
552                                 object->foreground_pixel_value = *segment++;
553                                 object->background_pixel_value = *segment++;
554                                 processed_length += 2;
555                         }
556                 }
557
558                 if (processed_length != segment_length)
559                         eDebug("too less data! (%d < %d)", segment_length, processed_length);
560
561                 break;
562         }
563         case 0x12: // CLUT definition segment
564         {
565                 int CLUT_id, CLUT_version_number;
566                 subtitle_clut *clut, **pclut;
567
568                 if (!page)
569                         break;
570
571                 //eDebug("CLUT: %02x", *segment);
572                 CLUT_id = *segment++;
573
574                 CLUT_version_number = *segment++ >> 4;
575                 processed_length += 2;
576
577                 //eDebug("page %d, CLUT %02x, version %d", page->page_id, CLUT_id, CLUT_version_number);
578
579                 clut = page->cluts; pclut = &page->cluts;
580
581                 while (clut)
582                 {
583                         if (clut->clut_id == CLUT_id)
584                                 break;
585                         pclut = &clut->next;
586                         clut = clut->next;
587                 }
588
589                 if (!clut)
590                 {
591                         *pclut = clut = new subtitle_clut;
592                         clut->next = 0;
593                         clut->clut_id = CLUT_id;
594                 }
595                 else if (clut->CLUT_version_number == CLUT_version_number)
596                         break;
597
598                 clut->CLUT_version_number=CLUT_version_number;
599
600                 memset(clut->entries_2bit, 0, sizeof(clut->entries_2bit));
601                 memset(clut->entries_4bit, 0, sizeof(clut->entries_4bit));
602                 memset(clut->entries_8bit, 0, sizeof(clut->entries_8bit));
603
604                 //eDebug("new clut");
605                 while (processed_length < segment_length)
606                 {
607                         int CLUT_entry_id, entry_CLUT_flag, full_range;
608                         int v_Y, v_Cr, v_Cb, v_T;
609
610                         CLUT_entry_id = *segment++;
611                         full_range = *segment & 1;
612                         entry_CLUT_flag = (*segment++ & 0xE0) >> 5;
613                         processed_length += 2;
614
615                         if (full_range)
616                         {
617                                 v_Y  = *segment++;
618                                 v_Cr = *segment++;
619                                 v_Cb = *segment++;
620                                 v_T  = *segment++;
621                                 processed_length += 4;
622                         } else
623                         {
624                                 v_Y   = *segment & 0xFC;
625                                 v_Cr  = (*segment++ & 3) << 6;
626                                 v_Cr |= (*segment & 0xC0) >> 2;
627                                 v_Cb  = (*segment & 0x3C) << 2;
628                                 v_T   = (*segment++ & 3) << 6;
629                                 processed_length += 2;
630                         }
631
632                         if (entry_CLUT_flag & 1) // 8bit
633                         {
634                                 ASSERT(CLUT_entry_id < 256);
635                                 clut->entries_8bit[CLUT_entry_id].Y = v_Y;
636                                 clut->entries_8bit[CLUT_entry_id].Cr = v_Cr;
637                                 clut->entries_8bit[CLUT_entry_id].Cb = v_Cb;
638                                 clut->entries_8bit[CLUT_entry_id].T = v_T;
639                                 clut->entries_8bit[CLUT_entry_id].valid = 1;
640                         }
641                         if (entry_CLUT_flag & 2) // 4bit
642                         {
643                                 ASSERT(CLUT_entry_id < 16);
644                                 clut->entries_4bit[CLUT_entry_id].Y = v_Y;
645                                 clut->entries_4bit[CLUT_entry_id].Cr = v_Cr;
646                                 clut->entries_4bit[CLUT_entry_id].Cb = v_Cb;
647                                 clut->entries_4bit[CLUT_entry_id].T = v_T;
648                                 clut->entries_4bit[CLUT_entry_id].valid = 1;
649                         }
650                         if (entry_CLUT_flag & 4) // 2bit
651                         {
652                                 ASSERT(CLUT_entry_id < 4);
653                                 clut->entries_2bit[CLUT_entry_id].Y = v_Y;
654                                 clut->entries_2bit[CLUT_entry_id].Cr = v_Cr;
655                                 clut->entries_2bit[CLUT_entry_id].Cb = v_Cb;
656                                 clut->entries_2bit[CLUT_entry_id].T = v_T;
657                                 clut->entries_2bit[CLUT_entry_id].valid = 1;
658                         }
659                         //eDebug("  %04x %02x %02x %02x %02x", CLUT_entry_id, v_Y, v_Cb, v_Cr, v_T);
660                 }
661                 break;
662         }
663         case 0x13: // object data segment
664         {
665                 int object_id, object_version_number, object_coding_method, non_modifying_color_flag;
666
667                 object_id  = *segment++ << 8;
668                 object_id |= *segment++;
669                 processed_length += 2;
670
671                 object_version_number = *segment >> 4;
672                 object_coding_method  = (*segment >> 2) & 3;
673                 non_modifying_color_flag = (*segment++ >> 1) & 1;
674                 processed_length++;
675
676                 //eDebug("object id %04x, version %d, object_coding_method %d (page_id %d)", object_id, object_version_number, object_coding_method, page_id);
677
678                 if (object_coding_method == 0)
679                 {
680                         int top_field_data_blocklength, bottom_field_data_blocklength;
681                         int i, line, linep;
682
683                         top_field_data_blocklength  = *segment++ << 8;
684                         top_field_data_blocklength |= *segment++;
685
686                         bottom_field_data_blocklength  = *segment++ << 8;
687                         bottom_field_data_blocklength |= *segment++;
688                         eDebug("%d / %d bytes", top_field_data_blocklength, bottom_field_data_blocklength);
689                         processed_length += 4;
690
691                         i = 0;
692                         line = 0;
693                         linep = 0;
694                         while (i < top_field_data_blocklength)
695                         {
696                                 int len;
697                                 len = subtitle_process_pixel_data(page, object_id, &line, &linep, segment);
698                                 if (len < 0)
699                                         return -1;
700                                 segment += len;
701                                 processed_length += len;
702                                 i += len;
703                         }
704
705                         line = 1;
706                         linep = 0;
707
708                         if (bottom_field_data_blocklength)
709                         {
710                                 i = 0;
711                                 while (i < bottom_field_data_blocklength)
712                                 {
713                                         int len;
714                                         len = subtitle_process_pixel_data(page, object_id, &line, &linep, segment);
715                                         if (len < 0)
716                                                 return -1;
717                                         segment += len;
718                                         processed_length += len;
719                                         i += len;
720                                 }
721                         }
722                         else if (top_field_data_blocklength)
723                                 eDebug("!!!! unimplemented: no bottom field! (%d : %d)", top_field_data_blocklength, bottom_field_data_blocklength);
724
725                         if ((top_field_data_blocklength + bottom_field_data_blocklength) & 1)
726                         {
727                                 segment++; processed_length++;
728                         }
729                 }
730                 else if (object_coding_method == 1)
731                         eDebug("---- object_coding_method 1 unsupported!");
732
733                 break;
734         }
735         case 0x80: // end of display set segment
736         {
737                 //eDebug("end of display set segment");
738                 subtitle_redraw_all();
739         }
740         case 0xFF: // stuffing
741                 break;
742         default:
743                 eDebug("unhandled segment type %02x", segment_type);
744         }
745
746         return segment_length + 6;
747 }
748
749 void eDVBSubtitleParser::subtitle_process_pes(__u8 *pkt, int len)
750 {
751         //eDebugNoNewLine("subtitle_process_pes");
752         if (!extract_pts(m_show_time, pkt))
753         {
754                 //eDebug(" %lld", m_show_time);
755                 pkt += 6; len -= 6;
756                 // skip PES header
757                 pkt++; len--;
758                 pkt++; len--;
759
760                 int hdr_len = *pkt++; len--;
761
762                 pkt+=hdr_len; len-=hdr_len;
763
764                 if (*pkt != 0x20)
765                 {
766                         //eDebug("data identifier is 0x%02x, but not 0x20", *pkt);
767                         return;
768                 }
769                 pkt++; len--; // data identifier
770                 *pkt++; len--; // stream id;
771
772                 if (len <= 0)
773                 {
774                         //eDebug("no data left (%d)", len);
775                         return;
776                 }
777
778                 while (len && *pkt == 0x0F)
779                 {
780                         int l = subtitle_process_segment(pkt);
781                         if (l < 0)
782                                 break;
783                         pkt += l;
784                         len -= l;
785                 }
786                 if (len && *pkt != 0xFF)
787                         eDebug("strange data at the end");
788         }
789         //else
790                 //eDebug("\ndvb subtitle packet without PTS.. ignore!!");
791 }
792
793 void eDVBSubtitleParser::subtitle_redraw_all()
794 {
795 #if 1
796         subtitle_page *page = m_pages;
797         while(page)
798         {
799                 subtitle_redraw(page->page_id);
800                 page = page->next;
801         }
802 #else
803         subtitle_page *page = m_pages;
804         eDebug("----------- end of display set");
805         eDebug("active pages:");
806         while (page)
807         {
808                 eDebug("  page_id %02x", page->page_id);
809                 eDebug("  page_version_number: %d", page->page_version_number);
810                 eDebug("  active regions:");
811                 {
812                         subtitle_page_region *region = page->page_regions;
813                         while (region)
814                         {
815                                 eDebug("    region_id: %04x", region->region_id);
816                                 eDebug("    region_horizontal_address: %d", region->region_horizontal_address);
817                                 eDebug("    region_vertical_address: %d", region->region_vertical_address);
818
819                                 region = region->next;
820                         }
821                 }
822
823                 subtitle_redraw(page->page_id);
824                 eDebug("defined regions:");
825                 subtitle_region *region = page->regions;
826                 while (region)
827                 {
828                         eDebug("  region_id %04x, version %d, %dx%d", region->region_id, region->region_version_number, region->region_width, region->region_height);
829
830                         subtitle_region_object *object = region->region_objects;
831                         while (object)
832                         {
833                                 eDebug("  object %02x, type %d, %d:%d", object->object_id, object->object_type, object->object_horizontal_position, object->object_vertical_position);
834                                 object = object->next;
835                         }
836                         region = region->next;
837                 }
838                 page = page->next;
839         }
840 #endif
841 }
842
843 void eDVBSubtitleParser::subtitle_reset()
844 {
845         while (subtitle_page *page = m_pages)
846         {
847                         /* free page regions */
848                 while (page->page_regions)
849                 {
850                         subtitle_page_region *p = page->page_regions->next;
851                         delete page->page_regions;
852                         page->page_regions = p;
853                 }
854                         /* free regions */
855                 while (page->regions)
856                 {
857                         subtitle_region *region = page->regions;
858
859                         while (region->region_objects)
860                         {
861                                 subtitle_region_object *obj = region->region_objects;
862                                 region->region_objects = obj->next;
863                                 delete obj;
864                         }
865
866                         if (region->region_buffer)
867                         {
868 //                              eDebug("no more need of region_buffer 2 %p", &(*region->region_buffer));
869                                 region->region_buffer=0;
870                         }
871
872                         page->regions = region->next;
873                         delete region;
874                 }
875
876                         /* free CLUTs */
877                 while (page->cluts)
878                 {
879                         subtitle_clut *clut = page->cluts;
880                         page->cluts = clut->next;
881                         delete clut;
882                 }
883
884                 m_pages = page->next;
885                 delete page;
886         }
887 }
888
889 void eDVBSubtitleParser::subtitle_redraw(int page_id)
890 {
891         subtitle_page *page = m_pages;
892
893 //      eDebug("displaying page id %d", page_id);
894
895         while (page)
896         {
897                 if (page->page_id == page_id)
898                         break;
899                 page = page->next;
900         }
901         if (!page)
902         {
903                 //eDebug("page not found");
904                 return;
905         }
906
907         //eDebug("iterating regions..");
908                 /* iterate all regions in this pcs */
909         subtitle_page_region *region = page->page_regions;
910
911         eDVBSubtitlePage Page;
912         Page.m_show_time = m_show_time;
913         for (; region; region=region->next)
914         {
915 //              eDebug("region %d", region->region_id);
916                         /* find corresponding region */
917                 subtitle_region *reg = page->regions;
918                 while (reg)
919                 {
920                         if (reg->region_id == region->region_id)
921                                 break;
922                         reg = reg->next;
923                 }
924                 if (reg)
925                 {
926                         if (reg->committed)
927                                 continue;
928 //                      eDebug("copy region %d to %d, %d", region->region_id, region->region_horizontal_address, region->region_vertical_address);
929
930                         int x0 = region->region_horizontal_address;
931                         int y0 = region->region_vertical_address;
932
933                         if ((x0 < 0) || (y0 < 0))
934                         {
935 //                              eDebug("x0 %d, y0 %d",
936 //                                      x0, y0);
937                                 continue;
938                         }
939
940                         /* find corresponding clut */
941                         subtitle_clut *clut = page->cluts;
942                         while (clut)
943                         {
944                         //eDebug("have %d, want %d", clut->clut_id, main_clut_id);
945                                 if (clut->clut_id == reg->clut_id)
946                                         break;
947                                 clut = clut->next;
948                         }
949
950                         int clut_size = reg->region_buffer->surface->clut.colors = reg->region_depth == subtitle_region::bpp2 ?
951                                 4 : reg->region_depth == subtitle_region::bpp4 ? 16 : 256;
952
953                         reg->region_buffer->surface->clut.data = new gRGB[clut_size];
954 //                      eDebug("create clut data for region_buffer %p", &(*reg->region_buffer));
955
956                         gRGB *palette = reg->region_buffer->surface->clut.data;
957
958                         subtitle_clut_entry *entries=0;
959                         switch(reg->region_depth)
960                         {
961                                 case subtitle_region::bpp2:
962 //                                      eDebug("2BPP");
963                                         entries = clut->entries_2bit;
964                                         memset(palette, 0, 4*sizeof(gRGB));
965                                         palette[0].a = 0xFF;
966                                         palette[2].r = palette[2].g = palette[2].b = 0xFF;
967                                         palette[3].r = palette[3].g = palette[3].b = 0x80;
968                                         break;
969                                 case subtitle_region::bpp4:
970 //                                      eDebug("4BPP");
971                                         entries = clut->entries_4bit;
972                                         memset(palette, 0, 16*sizeof(gRGB));
973                                         for (int i=0; i < 16; ++i)
974                                         {
975                                                 if (!i)
976                                                         palette[i].a = 0xFF;
977                                                 else if (i & 1)
978                                                 {
979                                                         if (i & 8)
980                                                                 palette[i].r = 0x80;
981                                                         if (i & 4)
982                                                                 palette[i].g = 0x80;
983                                                         if (i & 2)
984                                                                 palette[i].b = 0x80;
985                                                 }
986                                                 else
987                                                 {
988                                                         if (i & 8)
989                                                                 palette[i].r = 0xFF;
990                                                         if (i & 4)
991                                                                 palette[i].g = 0xFF;
992                                                         if (i & 2)
993                                                                 palette[i].b = 0xFF;
994                                                 }
995                                         }
996                                         break;
997                                 case subtitle_region::bpp8:
998 //                                      eDebug("8BPP");
999                                         entries = clut->entries_8bit;
1000                                         memset(palette, 0, 16*sizeof(gRGB));
1001                                         for (int i=0; i < 256; ++i)
1002                                         {
1003                                                 switch (i & 17)
1004                                                 {
1005                                                 case 0: // b1 == 0 && b5 == 0
1006                                                         if (!(i & 14)) // b2 == 0 && b3 == 0 && b4 == 0
1007                                                         {
1008                                                                 if (!(i & 224)) // b6 == 0 && b7 == 0 && b8 == 0
1009                                                                         palette[i].a = 0xFF;
1010                                                                 else
1011                                                                 {
1012                                                                         if (i & 128) // R = 100% x b8
1013                                                                                 palette[i].r = 0xFF;
1014                                                                         if (i & 64) // G = 100% x b7
1015                                                                                 palette[i].g = 0xFF;
1016                                                                         if (i & 32) // B = 100% x b6
1017                                                                                 palette[i].b = 0xFF;
1018                                                                         palette[i].a = 0xBF; // T = 75%
1019                                                                 }
1020                                                                 break;
1021                                                         }
1022                                                         // fallthrough !!
1023                                                 case 16: // b1 == 0 && b5 == 1
1024                                                         if (i & 128) // R = 33% x b8
1025                                                                 palette[i].r = 0x55;
1026                                                         if (i & 64) // G = 33% x b7
1027                                                                 palette[i].g = 0x55;
1028                                                         if (i & 32) // B = 33% x b6
1029                                                                 palette[i].b = 0x55;
1030                                                         if (i & 8) // R + 66,7% x b4
1031                                                                 palette[i].r += 0xAA;
1032                                                         if (i & 4) // G + 66,7% x b3
1033                                                                 palette[i].g += 0xAA;
1034                                                         if (i & 2) // B + 66,7% x b2
1035                                                                 palette[i].b += 0xAA;
1036                                                         if (i & 16) // needed for fall through from case 0!!
1037                                                                 palette[i].a = 0x80; // T = 50%
1038                                                         break;
1039                                                 case 1: // b1 == 1 && b5 == 0
1040                                                         palette[i].r =
1041                                                         palette[i].g =
1042                                                         palette[i].b = 0x80; // 50%
1043                                                         // fall through!!
1044                                                 case 17: // b1 == 1 && b5 == 1
1045                                                         if (i & 128) // R += 16.7% x b8
1046                                                                 palette[i].r += 0x2A;
1047                                                         if (i & 64) // G += 16.7% x b7
1048                                                                 palette[i].g += 0x2A;
1049                                                         if (i & 32) // B += 16.7% x b6
1050                                                                 palette[i].b += 0x2A;
1051                                                         if (i & 8) // R += 33% x b4
1052                                                                 palette[i].r += 0x55;
1053                                                         if (i & 4) // G += 33% x b3
1054                                                                 palette[i].g += 0x55;
1055                                                         if (i & 2) // B += 33% x b2
1056                                                                 palette[i].b += 0x55;
1057                                                         break;
1058                                                 }
1059                                         }
1060                                         break;
1061                         }
1062
1063                         if (clut)
1064                         {
1065                                 for (int i=0; i<clut_size; ++i)
1066                                 {
1067                                         if (entries[i].valid)
1068                                         {
1069                                                 int y = entries[i].Y,
1070                                                         cr = entries[i].Cr,
1071                                                         cb = entries[i].Cb;
1072                                                 if (y > 0)
1073                                                 {
1074                                                         y -= 16;
1075                                                         cr -= 128;
1076                                                         cb -= 128;
1077                                                         palette[i].r = MAX(MIN(((298 * y            + 460 * cr) / 256), 255), 0);
1078                                                         palette[i].g = MAX(MIN(((298 * y -  55 * cb - 137 * cr) / 256), 255), 0);
1079                                                         palette[i].b = MAX(MIN(((298 * y + 543 * cb           ) / 256), 255), 0);
1080                                                         palette[i].a = (entries[i].T) & 0xFF;
1081 //                                                      eDebug("override clut entry %d RGBA %02x%02x%02x%02x", i,
1082 //                                                              palette[i].r, palette[i].g, palette[i].b, palette[i].a);
1083                                                 }
1084                                                 else
1085                                                 {
1086 //                                                      eDebug("mist %d", i);
1087                                                         palette[i].r = 0;
1088                                                         palette[i].g = 0;
1089                                                         palette[i].b = 0;
1090                                                         palette[i].a = 0xFF;
1091                                                 }
1092                                         }
1093                                 }
1094                         }
1095
1096 //                      eDebug("commit region_buffer %p", &(*reg->region_buffer));
1097                         eDVBSubtitleRegion Region;
1098                         Region.m_pixmap = reg->region_buffer;
1099                         Region.m_position.setX(x0);
1100                         Region.m_position.setY(y0);
1101                         Page.m_regions.push_back(Region);
1102                         reg->committed = true;
1103                 }
1104                 else
1105                         eDebug("region not found");
1106         }
1107         m_new_subtitle_page(Page);
1108         Page.m_regions.clear();
1109 //      eDebug("page timeout is %d", page->page_time_out);
1110 //      Page.m_show_time += (page->page_time_out * 90000);
1111 //      m_new_subtitle_page(Page);
1112         //eDebug("schon gut.");
1113 }
1114
1115 DEFINE_REF(eDVBSubtitleParser);
1116
1117 eDVBSubtitleParser::eDVBSubtitleParser(iDVBDemux *demux)
1118         :m_pages(0)
1119 {
1120         setStreamID(0xBD);
1121
1122         if (demux->createPESReader(eApp, m_pes_reader))
1123                 eDebug("failed to create dvb subtitle PES reader!");
1124         else
1125                 m_pes_reader->connectRead(slot(*this, &eDVBSubtitleParser::processData), m_read_connection);
1126 }
1127
1128 eDVBSubtitleParser::~eDVBSubtitleParser()
1129 {
1130         subtitle_reset();
1131 }
1132
1133 int eDVBSubtitleParser::stop()
1134 {
1135         if (m_pes_reader)
1136         {
1137                 eDebug("disable dvb subtitles");
1138                 return m_pes_reader->stop();
1139         }
1140         return -1;
1141 }
1142
1143 int eDVBSubtitleParser::start(int pid, int composition_page_id, int ancillary_page_id)
1144 {
1145         if (m_pes_reader)
1146         {
1147                 eDebug("start dvb subtitles on pid 0x%04x with composition_page_id %d and ancillary_page_id %d",
1148                         pid, composition_page_id, ancillary_page_id);
1149                 m_composition_page_id = composition_page_id;
1150                 m_ancillary_page_id = ancillary_page_id;
1151                 return m_pes_reader->start(pid);
1152         }
1153         return -1;
1154 }
1155
1156 void eDVBSubtitleParser::connectNewPage(const Slot1<void, const eDVBSubtitlePage&> &slot, ePtr<eConnection> &connection)
1157 {
1158         connection = new eConnection(this, m_new_subtitle_page.connect(slot));
1159 }