1 /* dvb-sub.c - DVB subtitle decoding
2 * Copyright (C) Mart Raudsepp 2009 <mart.raudsepp@artecdesign.ee>
3 * Copyright (C) 2010 ONELAN Ltd.
5 * Heavily uses code algorithms ported from ffmpeg's libavcodec/dvbsubdec.c,
6 * especially the segment parsers. The original license applies to this
7 * ported code and the whole code in this file as well.
9 * Original copyright information follows:
12 * DVB subtitle decoding for ffmpeg
13 * Copyright (c) 2005 Ian Caulfield
15 * This file is part of FFmpeg.
17 * FFmpeg is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
22 * FFmpeg is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with FFmpeg; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <string.h> /* memset */
33 #include <gst/gstutils.h> /* GST_READ_UINT16_BE */
34 #include <gst/base/gstbitreader.h> /* GstBitReader */
38 GST_DEBUG_CATEGORY_STATIC (dvbsub_debug);
39 #define GST_CAT_DEFAULT dvbsub_debug
41 static void dvb_sub_init (void);
43 /* FIXME: Are we waiting for an acquisition point before trying to do things? */
44 /* FIXME: In the end convert some of the guint8/16 (especially stack variables) back to gint for access efficiency */
48 * @short_description: a DVB subtitle parsing class
49 * @stability: Unstable
51 * The #DvbSub represents an object used for parsing a DVB subpicture,
52 * and signalling the API user for new bitmaps to show on screen.
55 #define AYUV(y,u,v,a) (((a) << 24) | ((y) << 16) | ((u) << 8) | (v))
56 #define RGBA_TO_AYUV(r,g,b,a) (((a) << 24) | ((rgb_to_y(r,g,b)) << 16) | ((rgb_to_u(r,g,b)) << 8) | (rgb_to_v(r,g,b)))
59 typedef struct DVBSubCLUT
61 int id; /* default_clut uses -1 for this, so guint8 isn't fine without adaptations first */
67 struct DVBSubCLUT *next;
70 static DVBSubCLUT default_clut;
72 typedef struct DVBSubObjectDisplay
74 /* FIXME: Use more correct sizes */
84 /* FIXME: Should we use GSList? The relating interaction and pointer assigment is quite complex and perhaps unsuited for a plain GSList anyway */
85 struct DVBSubObjectDisplay *region_list_next;
86 struct DVBSubObjectDisplay *object_list_next;
87 } DVBSubObjectDisplay;
89 typedef struct DVBSubObject
91 /* FIXME: Use more correct sizes */
92 int id; /* FIXME: Use guint8 after checking it's fine in all code using it */
96 /* FIXME: Should we use GSList? */
97 DVBSubObjectDisplay *display_list;
98 struct DVBSubObject *next;
101 typedef struct DVBSubRegionDisplay
102 { /* FIXME: Figure out if this structure is only used temporarily in page_segment parser, or also more */
108 struct DVBSubRegionDisplay *next;
109 } DVBSubRegionDisplay;
111 typedef struct DVBSubRegion
116 guint8 depth; /* If we want to make this a guint8, then need to ensure it isn't wrap around with reserved values in region handling code */
121 /* FIXME: Validate these fields existence and exact types */
125 DVBSubObjectDisplay *display_list;
127 struct DVBSubRegion *next;
132 DvbSubCallbacks callbacks;
135 guint8 page_time_out;
136 DVBSubRegion *region_list;
137 DVBSubCLUT *clut_list;
138 DVBSubObject *object_list;
140 int display_list_size;
141 DVBSubRegionDisplay *display_list;
143 DVBSubtitleWindow display_def;
150 } DvbSubPixelDataSubBlockFieldType;
153 rgb_to_y (gint r, gint g, gint b)
157 ret = (gint) (((19595 * r) >> 16) + ((38470 * g) >> 16) + ((7471 * b) >> 16));
158 ret = CLAMP (ret, 0, 255);
163 rgb_to_u (gint r, gint g, gint b)
168 (gint) (-((11059 * r) >> 16) - ((21709 * g) >> 16) + ((32768 * b) >> 16) +
170 ret = CLAMP (ret, 0, 255);
175 rgb_to_v (gint r, gint g, gint b)
180 (gint) (((32768 * r) >> 16) - ((27439 * g) >> 16) - ((5329 * b) >> 16) +
182 ret = CLAMP (ret, 0, 255);
186 static DVBSubObject *
187 get_object (DvbSub * dvb_sub, guint16 object_id)
189 DVBSubObject *ptr = dvb_sub->object_list;
191 while (ptr && ptr->id != object_id) {
199 get_clut (DvbSub * dvb_sub, gint clut_id)
201 DVBSubCLUT *ptr = dvb_sub->clut_list;
203 while (ptr && ptr->id != clut_id) {
210 static DVBSubRegion *
211 get_region (DvbSub * dvb_sub, guint8 region_id)
213 DVBSubRegion *ptr = dvb_sub->region_list;
215 while (ptr && ptr->id != region_id) {
223 delete_region_display_list (DvbSub * dvb_sub, DVBSubRegion * region)
225 DVBSubObject *object, *obj2;
226 DVBSubObject **obj2_ptr;
227 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
229 while (region->display_list) {
230 display = region->display_list;
232 object = get_object (dvb_sub, display->object_id);
235 obj_disp_ptr = &object->display_list;
236 obj_disp = *obj_disp_ptr;
238 while (obj_disp && obj_disp != display) {
239 obj_disp_ptr = &obj_disp->object_list_next;
240 obj_disp = *obj_disp_ptr;
244 *obj_disp_ptr = obj_disp->object_list_next;
246 if (!object->display_list) {
247 obj2_ptr = (DVBSubObject **) & dvb_sub->object_list; /* FIXME: Evil casting */
250 while (obj2 != object) {
252 obj2_ptr = &obj2->next;
256 *obj2_ptr = obj2->next;
258 g_slice_free (DVBSubObject, obj2);
263 region->display_list = display->region_list_next;
265 g_slice_free (DVBSubObjectDisplay, display);
270 delete_state (DvbSub * dvb_sub)
272 DVBSubRegion *region;
274 while (dvb_sub->region_list) {
275 region = dvb_sub->region_list;
277 dvb_sub->region_list = region->next;
279 delete_region_display_list (dvb_sub, region);
281 g_free (region->pbuf);
283 g_slice_free (DVBSubRegion, region);
286 g_slice_free_chain (DVBSubCLUT, dvb_sub->clut_list, next);
287 dvb_sub->clut_list = NULL;
289 /* Should already be NULL */
290 g_warn_if_fail (dvb_sub->object_list == NULL);
296 int i, r, g, b, a = 0;
298 GST_DEBUG_CATEGORY_INIT (dvbsub_debug, "dvbsub", 0, "dvbsuboverlay parser");
300 /* Initialize the static default_clut structure, from which other clut
301 * structures are initialized from (to start off with default CLUTs
302 * as defined in the specification). */
303 default_clut.id = -1;
305 default_clut.clut4[0] = RGBA_TO_AYUV (0, 0, 0, 0);
306 default_clut.clut4[1] = RGBA_TO_AYUV (255, 255, 255, 255);
307 default_clut.clut4[2] = RGBA_TO_AYUV (0, 0, 0, 255);
308 default_clut.clut4[3] = RGBA_TO_AYUV (127, 127, 127, 255);
310 default_clut.clut16[0] = RGBA_TO_AYUV (0, 0, 0, 0);
311 for (i = 1; i < 16; i++) {
313 r = (i & 1) ? 255 : 0;
314 g = (i & 2) ? 255 : 0;
315 b = (i & 4) ? 255 : 0;
317 r = (i & 1) ? 127 : 0;
318 g = (i & 2) ? 127 : 0;
319 b = (i & 4) ? 127 : 0;
321 default_clut.clut16[i] = RGBA_TO_AYUV (r, g, b, 255);
324 default_clut.clut256[0] = RGBA_TO_AYUV (0, 0, 0, 0);
325 for (i = 1; i < 256; i++) {
327 r = (i & 1) ? 255 : 0;
328 g = (i & 2) ? 255 : 0;
329 b = (i & 4) ? 255 : 0;
334 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
335 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
336 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
340 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
341 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
342 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
346 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
347 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
348 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
352 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
353 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
354 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
359 default_clut.clut256[i] = RGBA_TO_AYUV (r, g, b, a);
364 _dvb_sub_parse_page_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
366 { /* FIXME: Use guint for buf_size here and in many other places? */
367 DVBSubRegionDisplay *display;
368 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
370 const guint8 *buf_end = buf + buf_size;
377 dvb_sub->page_time_out = *buf++;
378 page_state = ((*buf++) >> 2) & 3;
380 #ifndef GST_DISABLE_GST_DEBUG
382 static const gchar *page_state_str[4] = {
383 "Normal case", "ACQUISITION POINT", "Mode Change", "RESERVED"
386 GST_DEBUG ("PAGE: page_id = %u, length = %d, page_time_out = %u secs, "
387 "page_state = %s", page_id, buf_size, dvb_sub->page_time_out,
388 page_state_str[page_state]);
392 if (page_state == 2) { /* Mode change */
393 delete_state (dvb_sub);
396 tmp_display_list = dvb_sub->display_list;
397 dvb_sub->display_list = NULL;
398 dvb_sub->display_list_size = 0;
400 while (buf + 5 < buf_end) {
404 display = tmp_display_list;
405 tmp_ptr = &tmp_display_list;
407 while (display && display->region_id != region_id) {
408 tmp_ptr = &display->next;
409 display = display->next;
413 display = g_slice_new0 (DVBSubRegionDisplay);
415 display->region_id = region_id;
417 display->x_pos = GST_READ_UINT16_BE (buf);
419 display->y_pos = GST_READ_UINT16_BE (buf);
422 *tmp_ptr = display->next;
424 display->next = dvb_sub->display_list;
425 dvb_sub->display_list = display;
426 dvb_sub->display_list_size++;
428 GST_LOG ("PAGE: REGION information: ID = %u, address = %ux%u", region_id,
429 display->x_pos, display->y_pos);
432 while (tmp_display_list) {
433 display = tmp_display_list;
435 tmp_display_list = display->next;
437 g_slice_free (DVBSubRegionDisplay, display);
442 _dvb_sub_parse_region_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
445 const guint8 *buf_end = buf + buf_size;
448 DVBSubRegion *region;
449 DVBSubObject *object;
450 DVBSubObjectDisplay *object_display;
458 region = get_region (dvb_sub, region_id);
460 if (!region) { /* Create a new region */
461 region = g_slice_new0 (DVBSubRegion);
462 region->id = region_id;
463 region->next = dvb_sub->region_list;
464 dvb_sub->region_list = region;
467 fill = ((*buf++) >> 3) & 1;
469 region->width = GST_READ_UINT16_BE (buf);
471 region->height = GST_READ_UINT16_BE (buf);
474 if (region->width * region->height != region->buf_size) { /* FIXME: Read closer from spec what happens when dimensions change */
476 g_free (region->pbuf);
478 region->buf_size = region->width * region->height;
480 region->pbuf = g_malloc (region->buf_size); /* TODO: We can probably use GSlice here if careful about freeing while buf_size still records the correct size */
482 fill = 1; /* FIXME: Validate from spec that fill is forced on (in the following codes context) when dimensions change */
485 region->depth = 1 << (((*buf++) >> 2) & 7);
486 if (region->depth < 2 || region->depth > 8) {
487 GST_WARNING ("region depth %d is invalid", region->depth);
488 region->depth = 4; /* FIXME: Check from spec this is the default? */
491 region->clut = *buf++;
493 if (region->depth == 8)
494 region->bgcolor = *buf++;
498 if (region->depth == 4)
499 region->bgcolor = (((*buf++) >> 4) & 15);
501 region->bgcolor = (((*buf++) >> 2) & 3);
504 GST_DEBUG ("REGION: id = %u, (%ux%u)@%u-bit", region_id, region->width,
505 region->height, region->depth);
508 memset (region->pbuf, region->bgcolor, region->buf_size);
509 GST_DEBUG ("REGION: filling region (%u) with bgcolor = %u", region->id,
513 delete_region_display_list (dvb_sub, region); /* Delete the region display list for current region - FIXME: why? */
515 while (buf + 6 <= buf_end) {
516 object_id = GST_READ_UINT16_BE (buf);
519 object = get_object (dvb_sub, object_id);
522 object = g_slice_new0 (DVBSubObject);
524 object->id = object_id;
526 object->next = dvb_sub->object_list;
527 dvb_sub->object_list = object;
530 object->type = (*buf) >> 6;
532 object_display = g_slice_new0 (DVBSubObjectDisplay);
534 object_display->object_id = object_id;
535 object_display->region_id = region_id;
537 object_display->x_pos = GST_READ_UINT16_BE (buf) & 0xfff;
539 object_display->y_pos = GST_READ_UINT16_BE (buf) & 0xfff;
542 if ((object->type == 1 || object->type == 2) && buf + 2 <= buf_end) {
543 object_display->fgcolor = *buf++;
544 object_display->bgcolor = *buf++;
547 object_display->region_list_next = region->display_list;
548 region->display_list = object_display;
550 object_display->object_list_next = object->display_list;
551 object->display_list = object_display;
553 GST_DEBUG ("REGION DATA: object_id = %u, region_id = %u, pos = %ux%u, "
554 "obj_type = %u", object->id, region->id, object_display->x_pos,
555 object_display->y_pos, object->type);
557 if (object->type == 1 || object->type == 2) {
558 GST_DEBUG ("REGION DATA: fgcolor = %u, bgcolor = %u",
559 object_display->fgcolor, object_display->bgcolor);
565 _dvb_sub_parse_clut_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
568 const guint8 *buf_end = buf + buf_size;
571 int entry_id, depth, full_range;
572 int y, cr, cb, alpha;
574 GST_MEMDUMP ("DVB clut packet", buf, buf_size);
579 clut = get_clut (dvb_sub, clut_id);
582 clut = g_slice_new (DVBSubCLUT);
584 memcpy (clut, &default_clut, sizeof (DVBSubCLUT));
588 clut->next = dvb_sub->clut_list;
589 dvb_sub->clut_list = clut;
592 while (buf + 4 < buf_end) {
595 depth = (*buf) & 0xe0;
598 GST_WARNING ("Invalid clut depth 0x%x!", *buf);
602 full_range = (*buf++) & 1;
611 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
612 cb = (buf[1] << 2) & 0xf0;
613 alpha = (buf[1] << 6) & 0xc0;
621 GST_DEBUG ("CLUT DEFINITION: clut %d := (%d,%d,%d,%d)", entry_id, y, cb, cr,
625 clut->clut4[entry_id] = AYUV (y, cb, cr, 255 - alpha);
627 clut->clut16[entry_id] = AYUV (y, cb, cr, 255 - alpha);
629 clut->clut256[entry_id] = AYUV (y, cb, cr, 255 - alpha);
633 // FFMPEG-FIXME: The same code in ffmpeg is much more complex, it could use the same
634 // FFMPEG-FIXME: refactoring as done here
636 _dvb_sub_read_2bit_string (guint8 * destbuf, gint dbuf_len,
637 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
639 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
640 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
642 gboolean stop_parsing = FALSE;
644 guint32 pixels_read = 0;
646 static gboolean warning_shown = FALSE;
647 if (!warning_shown) {
648 g_warning ("Parsing 2bit color DVB sub-picture. This is not tested at all. "
649 "If you see this message, please provide the developers with sample "
650 "media with these subtitles, if possible.");
651 warning_shown = TRUE;
654 GST_TRACE ("dbuf_len = %d", dbuf_len);
656 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
657 guint run_length = 0, clut_index = 0;
659 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
661 if (bits) { /* 2-bit_pixel-code */
664 } else { /* 2-bit_zero */
665 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
666 if (bits == 1) { /* switch_1 == '1' */
667 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 3);
669 clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
670 } else { /* switch_1 == '0' */
671 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
672 if (bits == 1) { /* switch_2 == '1' */
673 run_length = 1; /* 1x pseudo-colour '00' */
674 } else { /* switch_2 == '0' */
675 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
676 switch (bits) { /* switch_3 */
677 case 0x0: /* end of 2-bit/pixel_code_string */
680 case 0x1: /* two pixels shall be set to pseudo colour (entry) '00' */
683 case 0x2: /* the following 6 bits contain run length coded pixel data */
684 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
686 clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
688 case 0x3: /* the following 10 bits contain run length coded pixel data */
689 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 8);
691 clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
698 /* If run_length is zero, continue. Only case happening is when
699 * stop_parsing is TRUE too, so next cycle shouldn't run */
703 /* Trim the run_length to not go beyond the line end and consume
704 * it from remaining length of dest line */
705 run_length = MIN (run_length, dbuf_len);
706 dbuf_len -= run_length;
708 /* Make clut_index refer to the index into the desired bit depths
709 * CLUT definition table */
711 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
713 /* Now we can simply memset run_length count of destination bytes
714 * to clut_index, but only if not non_modifying */
715 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer, "
716 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
718 if (!(non_mod == 1 && bits == 1))
719 memset (destbuf, clut_index, run_length);
721 destbuf += run_length;
722 pixels_read += run_length;
725 // FIXME: Test skip_to_byte instead of adding 7 bits, once everything else is working good
726 //gst_bit_reader_skip_to_byte (&gb);
727 *srcbuf += (gst_bit_reader_get_pos (&gb) + 7) >> 3;
729 GST_TRACE ("PIXEL: returning, read %u pixels", pixels_read);
730 // FIXME: Shouldn't need this variable if tracking things in the loop better
734 // FFMPEG-FIXME: The same code in ffmpeg is much more complex, it could use the same
735 // FFMPEG-FIXME: refactoring as done here, explained in commit 895296c3
737 _dvb_sub_read_4bit_string (guint8 * destbuf, gint dbuf_len,
738 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
740 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
741 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
742 gboolean stop_parsing = FALSE;
744 guint32 pixels_read = 0;
746 GST_TRACE ("RUNLEN: srcbuf position %p, buf_size = %d; destination buffer "
747 "size is %d @ %p", *srcbuf, buf_size, dbuf_len, destbuf);
749 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
750 guint run_length = 0, clut_index = 0;
752 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
758 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
759 if (bits == 0) { /* switch_1 == '0' */
760 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 3);
766 } else { /* switch_1 == '1' */
767 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
768 if (bits == 0) { /* switch_2 == '0' */
769 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
771 clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
772 } else { /* switch_2 == '1' */
773 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
775 case 0x0: /* switch_3 == '00' */
776 run_length = 1; /* 1 pixel of pseudo-color 0 */
778 case 0x1: /* switch_3 == '01' */
779 run_length = 2; /* 2 pixels of pseudo-color 0 */
781 case 0x2: /* switch_3 == '10' */
782 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
784 clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
786 case 0x3: /* switch_3 == '11' */
787 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 8);
789 clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
796 /* If run_length is zero, continue. Only case happening is when
797 * stop_parsing is TRUE too, so next cycle shouldn't run */
801 /* Trim the run_length to not go beyond the line end and consume
802 * it from remaining length of dest line */
803 run_length = MIN (run_length, dbuf_len);
804 dbuf_len -= run_length;
806 /* Make clut_index refer to the index into the desired bit depths
807 * CLUT definition table */
809 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
811 /* Now we can simply memset run_length count of destination bytes
812 * to clut_index, but only if not non_modifying */
813 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer; "
814 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
816 if (!(non_mod == 1 && bits == 1))
817 memset (destbuf, clut_index, run_length);
819 destbuf += run_length;
820 pixels_read += run_length;
823 // FIXME: Test skip_to_byte instead of adding 7 bits, once everything else is working good
824 //gst_bit_reader_skip_to_byte (&gb);
825 *srcbuf += (gst_bit_reader_get_pos (&gb) + 7) >> 3;
827 GST_LOG ("Returning with %u pixels read", pixels_read);
829 // FIXME: Shouldn't need this variable if tracking things in the loop better
834 _dvb_sub_read_8bit_string (guint8 * destbuf, gint dbuf_len,
835 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
837 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
838 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
840 gboolean stop_parsing = FALSE;
842 guint32 pixels_read = 0;
844 static gboolean warning_shown = FALSE;
845 if (!warning_shown) {
847 ("Parsing 8bit color DVB sub-picture. This is not tested at all. If you see this message, "
848 "please provide the developers with sample media with these subtitles, if possible.");
849 warning_shown = TRUE;
852 GST_LOG ("dbuf_len = %d", dbuf_len);
854 /* FFMPEG-FIXME: ffmpeg uses a manual byte walking algorithm, which might be more performant,
855 * FFMPEG-FIXME: but it does almost absolutely no buffer length checking, so could walk over
856 * FFMPEG-FIXME: memory boundaries. While we don't check gst_bit_reader_get_bits_uint32
857 * FFMPEG-FIXME: return values either and therefore might get some pixels corrupted, we at
858 * FFMPEG-FIXME: lest have no chance of reading memory we don't own and visual corruption
859 * FFMPEG-FIXME: is guaranteed anyway when not all bytes are present */
860 /* Rephrased - it's better to work with bytes with default value '0' instead of reading from memory we don't own. */
861 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
862 guint run_length = 0, clut_index = 0;
863 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 8);
865 if (bits) { /* 8-bit_pixel-code */
868 } else { /* 8-bit_zero */
869 bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
870 if (bits == 0) { /* switch_1 == '0' */
871 /* run_length_1-127 for pseudo-colour _entry) '0x00' */
872 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 7);
873 if (run_length == 0) { /* end_of_string_signal */
876 } else { /* switch_1 == '1' */
877 /* run_length_3-127 */
878 run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 7);
879 clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 8);
881 if (run_length < 3) {
882 GST_WARNING ("runlength value was %u, but the spec requires it "
883 "must be >=3", run_length);
888 /* If run_length is zero, continue. Only case happening is when
889 * stop_parsing is TRUE too, so next cycle shouldn't run */
893 /* Trim the run_length to not go beyond the line end and consume
894 * it from remaining length of dest line */
895 run_length = MIN (run_length, dbuf_len);
896 dbuf_len -= run_length;
898 /* Make clut_index refer to the index into the desired bit depths
899 * CLUT definition table */
901 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
903 /* Now we can simply memset run_length count of destination bytes
904 * to clut_index, but only if not non_modifying */
905 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer; "
906 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
908 if (!(non_mod == 1 && bits == 1))
909 memset (destbuf, clut_index, run_length);
911 destbuf += run_length;
912 pixels_read += run_length;
915 GST_LOG ("Returning with %u pixels read", pixels_read);
917 // FIXME: Shouldn't need this variable if tracking things in the loop better
922 _dvb_sub_parse_pixel_data_block (DvbSub * dvb_sub,
923 DVBSubObjectDisplay * display, const guint8 * buf, gint buf_size,
924 DvbSubPixelDataSubBlockFieldType top_bottom, guint8 non_mod)
926 DVBSubRegion *region = get_region (dvb_sub, display->region_id);
927 const guint8 *buf_end = buf + buf_size;
931 gboolean dest_buf_filled = FALSE;
933 guint8 map2to4[] = { 0x0, 0x7, 0x8, 0xf };
934 guint8 map2to8[] = { 0x00, 0x77, 0x88, 0xff };
935 guint8 map4to8[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
936 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
940 GST_LOG ("DVB pixel block size %d, %s field:", buf_size,
941 top_bottom ? "bottom" : "top");
943 GST_MEMDUMP ("packet", buf, buf_size);
945 if (region == NULL) {
946 GST_LOG ("Region is NULL, returning");
952 x_pos = display->x_pos;
953 y_pos = display->y_pos;
955 if ((y_pos & 1) != top_bottom)
958 while (buf < buf_end) {
959 GST_LOG ("Iteration start, %u bytes missing from end; buf = %p, "
960 "buf_end = %p; Region is number %u, with a dimension of %dx%d; "
961 "We are at position %dx%d", (guint) (buf_end - buf), buf, buf_end,
962 region->id, region->width, region->height, x_pos, y_pos);
964 // FFMPEG-FIXME: ffmpeg doesn't check for equality and so can overflow destination buffer later on with bad input data
965 // FFMPEG-FIXME: However that makes it warn on end_of_object_line and map tables as well, so we add the dest_buf_filled tracking
966 // FIXME: Removed x_pos checking here, because we don't want to turn dest_buf_filled to TRUE permanently in that case
967 // FIXME: We assume that region->width - x_pos as dbuf_len to read_nbit_string will take care of that case nicely;
968 // FIXME: That is, that read_nbit_string never scribbles anything if dbuf_len passed to it is zero due to this.
969 if (y_pos >= region->height) {
970 dest_buf_filled = TRUE;
975 if (dest_buf_filled) {
976 /* FIXME: Be more verbose */
977 GST_WARNING ("Invalid object location for data_type 0x%x!",
979 GST_MEMDUMP ("Remaining data after invalid object location:", buf,
980 (guint) (buf_end - buf));
984 if (region->depth == 8)
986 else if (region->depth == 4)
991 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
992 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
994 _dvb_sub_read_2bit_string (pbuf + (y_pos * region->width) + x_pos,
995 region->width - x_pos, &buf, buf_end - buf, non_mod, map_table);
998 if (dest_buf_filled) {
999 /* FIXME: Be more verbose */
1000 GST_WARNING ("Invalid object location for data_type 0x%x!",
1002 GST_MEMDUMP ("Remaining data after invalid object location:", buf,
1004 return; // FIXME: Perhaps tell read_nbit_string that dbuf_len is zero and let it walk the bytes regardless? (Same FIXME for 2bit and 8bit)
1007 if (region->depth < 4) {
1008 GST_WARNING ("4-bit pixel string in %d-bit region!", region->depth);
1012 if (region->depth == 8)
1013 map_table = map4to8;
1017 GST_LOG ("READ_4BIT_STRING: String data into position %dx%d; "
1018 "buf before is %p", x_pos, y_pos, buf);
1019 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
1020 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
1022 _dvb_sub_read_4bit_string (pbuf + (y_pos * region->width) + x_pos,
1023 region->width - x_pos, &buf, buf_end - buf, non_mod, map_table);
1024 GST_DEBUG ("READ_4BIT_STRING finished: buf pointer now %p", buf);
1027 if (dest_buf_filled) {
1028 /* FIXME: Be more verbose */
1029 GST_WARNING ("Invalid object location for data_type 0x%x!",
1031 GST_MEMDUMP ("Remaining data after invalid object location:",
1032 buf, (guint) (buf_end - buf));
1036 if (region->depth < 8) {
1037 GST_WARNING ("8-bit pixel string in %d-bit region!", region->depth);
1040 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
1041 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
1043 _dvb_sub_read_8bit_string (pbuf + (y_pos * region->width) + x_pos,
1044 region->width - x_pos, &buf, buf_end - buf, non_mod, NULL);
1048 GST_DEBUG ("handling map2to4 table data");
1049 /* FIXME: I don't see any guards about buffer size here - buf++ happens with the switch, but
1050 * FIXME: buffer is walked without length checks? Same deal in other map table cases */
1051 map2to4[0] = (*buf) >> 4;
1052 map2to4[1] = (*buf++) & 0xf;
1053 map2to4[2] = (*buf) >> 4;
1054 map2to4[3] = (*buf++) & 0xf;
1057 GST_DEBUG ("handling map2to8 table data");
1058 for (i = 0; i < 4; i++)
1059 map2to8[i] = *buf++;
1062 GST_DEBUG ("handling map4to8 table data");
1063 for (i = 0; i < 16; i++)
1064 map4to8[i] = *buf++;
1067 GST_DEBUG ("end of object line code encountered");
1068 x_pos = display->x_pos;
1072 /* FIXME: Do we consume word align stuffing byte that could follow top/bottom data? */
1073 GST_WARNING ("Unknown/unsupported pixel block 0x%x", *(buf - 1));
1079 _dvb_sub_parse_object_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
1082 const guint8 *buf_end = buf + buf_size;
1084 DVBSubObject *object;
1086 guint8 coding_method, non_modifying_color;
1088 object_id = GST_READ_UINT16_BE (buf);
1091 object = get_object (dvb_sub, object_id);
1093 GST_DEBUG ("OBJECT: a new object segment has occurred for object_id = %u",
1097 GST_WARNING ("Nothing known about object with ID %u yet, bailing out",
1102 coding_method = ((*buf) >> 2) & 3;
1103 non_modifying_color = ((*buf++) >> 1) & 1;
1105 if (coding_method == 0) {
1106 const guint8 *block;
1107 DVBSubObjectDisplay *display;
1108 guint16 top_field_len, bottom_field_len;
1110 top_field_len = GST_READ_UINT16_BE (buf);
1112 bottom_field_len = GST_READ_UINT16_BE (buf);
1115 if (buf + top_field_len + bottom_field_len > buf_end) {
1116 GST_WARNING ("Field data size too large");
1120 /* FIXME: Potential optimization opportunity here - parse the object pixmap only once, and copy it to all the
1121 * FIXME: regions that need it. One object being in multiple regions is a rare occurrence in real life, however */
1122 for (display = object->display_list; display;
1123 display = display->object_list_next) {
1126 GST_DEBUG ("OBJECT: parsing top and bottom part of object id %d; "
1127 "top_field_len = %u, bottom_field_len = %u",
1128 display->object_id, top_field_len, bottom_field_len);
1130 _dvb_sub_parse_pixel_data_block (dvb_sub, display, block, top_field_len,
1131 TOP_FIELD, non_modifying_color);
1133 if (bottom_field_len > 0)
1134 block = buf + top_field_len;
1136 bottom_field_len = top_field_len;
1138 _dvb_sub_parse_pixel_data_block (dvb_sub, display, block,
1139 bottom_field_len, BOTTOM_FIELD, non_modifying_color);
1142 } else if (coding_method == 1) {
1143 GST_FIXME ("'a string of characters' coding method not supported yet!");
1145 GST_WARNING ("Unknown object coding 0x%x", coding_method);
1150 _dvb_sub_parse_display_definition_segment (DvbSub * dvb_sub, guint8 * buf,
1153 int dds_version, info_byte;
1159 dds_version = info_byte >> 4;
1161 if (dvb_sub->display_def.version == dds_version)
1162 return 0; /* already have this display definition version */
1164 dvb_sub->display_def.version = dds_version;
1165 dvb_sub->display_def.display_width = GST_READ_UINT16_BE (buf) + 1;
1167 dvb_sub->display_def.display_height = GST_READ_UINT16_BE (buf) + 1;
1170 dvb_sub->display_def.window_flag = info_byte & 1 << 3;
1172 if (buf_size >= 13 && dvb_sub->display_def.window_flag) {
1173 dvb_sub->display_def.window_x = GST_READ_UINT16_BE (buf);
1175 dvb_sub->display_def.window_y = GST_READ_UINT16_BE (buf);
1177 dvb_sub->display_def.window_width =
1178 GST_READ_UINT16_BE (buf) - dvb_sub->display_def.window_x + 1;
1180 dvb_sub->display_def.window_height =
1181 GST_READ_UINT16_BE (buf) - dvb_sub->display_def.window_y + 1;
1189 _dvb_sub_parse_end_of_display_set (DvbSub * dvb_sub, guint16 page_id,
1190 guint8 * buf, gint buf_size, guint64 pts)
1192 DVBSubRegionDisplay *display;
1195 guint32 *clut_table;
1198 GST_DEBUG ("DISPLAY SET END: page_id = %u, length = %d", page_id, buf_size);
1200 sub = g_slice_new0 (DVBSubtitles);
1202 #if 0 /* FIXME: PTS stuff not figured out yet */
1203 sub->start_display_time = 0;
1204 sub->end_display_time = priv->page_time_out * 1000;
1205 sub->format = 0; /* 0 = graphics */
1208 /* N.B. g_new0() will return NULL if num_rects is 0 */
1209 sub->num_rects = dvb_sub->display_list_size;
1210 sub->rects = g_new0 (DVBSubtitleRect, sub->num_rects);
1214 /* copy subtitle display and window information */
1215 sub->display_def = dvb_sub->display_def;
1217 for (display = dvb_sub->display_list; display; display = display->next) {
1218 DVBSubtitleRect *rect;
1219 DVBSubRegion *region;
1221 region = get_region (dvb_sub, display->region_id);
1226 rect = &sub->rects[i];
1227 rect->x = display->x_pos;
1228 rect->y = display->y_pos;
1229 rect->w = region->width;
1230 rect->h = region->height;
1231 #if 0 /* FIXME: Don't think we need to save the number of colors in the palette when we are saving as RGBA? */
1232 rect->nb_colors = 16;
1234 #if 0 /* FIXME: Needed to be specified once we support strings of characters based subtitles */
1235 rect->type = SUBTITLE_BITMAP;
1237 rect->pict.rowstride = region->width;
1238 rect->pict.palette_bits_count = region->depth;
1240 clut = get_clut (dvb_sub, region->clut);
1243 clut = &default_clut;
1245 switch (region->depth) {
1247 clut_table = clut->clut4;
1250 clut_table = clut->clut256;
1254 clut_table = clut->clut16;
1258 /* FIXME: Tweak this to be saved in a format most suitable for Qt and GStreamer instead.
1259 * Currently kept in AVPicture for quick save_display_set testing */
1260 rect->pict.palette = g_malloc ((1 << region->depth) * sizeof (guint32)); /* FIXME: Can we use GSlice here? */
1261 memcpy (rect->pict.palette, clut_table,
1262 (1 << region->depth) * sizeof (guint32));
1264 GST_MEMDUMP ("rect->pict.data.palette content",
1265 (guint8 *) rect->pict.palette, (1 << region->depth) * sizeof (guint32));
1267 rect->pict.data = g_malloc (region->buf_size); /* FIXME: Can we use GSlice here? */
1268 memcpy (rect->pict.data, region->pbuf, region->buf_size);
1270 GST_DEBUG ("DISPLAY: an object rect created: iteration %u, "
1271 "pos: %d:%d, size: %dx%d", i, rect->x, rect->y, rect->w, rect->h);
1273 GST_MEMDUMP ("rect->pict.data content", rect->pict.data, region->buf_size);
1279 sub->page_time_out = dvb_sub->page_time_out;
1282 if (dvb_sub->callbacks.new_data) {
1283 dvb_sub->callbacks.new_data (dvb_sub, sub, dvb_sub->user_data);
1285 /* No-one responsible to clean up memory, so do it ourselves */
1286 /* FIXME: Just don't bother with all this palette image creation in the first place then... */
1287 dvb_subtitles_free (sub);
1290 return 1; /* FIXME: The caller of this function is probably supposed to do something with the return value */
1294 dvb_subtitles_free (DVBSubtitles * sub)
1301 /* Now free up all the temporary memory we allocated */
1302 for (i = 0; i < sub->num_rects; ++i) {
1303 g_free (sub->rects[i].pict.palette);
1304 g_free (sub->rects[i].pict.data);
1306 g_free (sub->rects);
1307 g_slice_free (DVBSubtitles, sub);
1313 static gsize inited = 0;
1316 if (g_once_init_enter (&inited)) {
1318 g_once_init_leave (&inited, TRUE);
1321 sub = g_slice_new0 (DvbSub);
1323 /* TODO: Add initialization code here */
1324 /* FIXME: Do we have a reason to initiate the members to zero, or are we guaranteed that anyway? */
1325 sub->region_list = NULL;
1326 sub->object_list = NULL;
1327 sub->page_time_out = 0; /* FIXME: Maybe 255 instead? */
1328 sub->pes_buffer = g_string_new (NULL);
1330 /* display/window information */
1331 sub->display_def.version = -1;
1332 sub->display_def.window_flag = 0;
1333 sub->display_def.display_width = 720;
1334 sub->display_def.display_height = 576;
1340 dvb_sub_free (DvbSub * sub)
1342 /* TODO: Add deinitalization code here */
1343 /* FIXME: Clear up region_list contents */
1345 while (sub->display_list) {
1346 DVBSubRegionDisplay *tmp = sub->display_list->next;
1347 g_slice_free (DVBSubRegionDisplay, sub->display_list);
1348 sub->display_list = tmp;
1350 g_string_free (sub->pes_buffer, TRUE);
1351 g_slice_free (DvbSub, sub);
1354 #define DVB_SUB_SEGMENT_PAGE_COMPOSITION 0x10
1355 #define DVB_SUB_SEGMENT_REGION_COMPOSITION 0x11
1356 #define DVB_SUB_SEGMENT_CLUT_DEFINITION 0x12
1357 #define DVB_SUB_SEGMENT_OBJECT_DATA 0x13
1358 #define DVB_SUB_SEGMENT_DISPLAY_DEFINITION 0x14
1359 #define DVB_SUB_SEGMENT_END_OF_DISPLAY_SET 0x80
1360 #define DVB_SUB_SEGMENT_STUFFING 0xFF
1362 #define DVB_SUB_SYNC_BYTE 0x0f
1364 * dvb_sub_feed_with_pts:
1365 * @dvb_sub: a #DvbSub
1366 * @pts: The PTS of the data
1367 * @data: The data to feed to the parser
1368 * @len: Length of the data
1370 * Feeds the DvbSub parser with new binary data to parse,
1371 * with an associated PTS value. E.g, data left after PES
1372 * packet header has been already parsed, which contains
1373 * the PTS information).
1375 * Return value: -1 if data was unhandled (e.g, not a subtitle packet),
1376 * -2 if data parsing was unsuccesful (e.g, length was invalid),
1377 * 0 or positive if data was handled. If positive, then amount of data consumed on success. FIXME: List the positive return values.
1380 dvb_sub_feed_with_pts (DvbSub * dvb_sub, guint64 pts, guint8 * data, gint len)
1382 unsigned int pos = 0;
1383 guint8 segment_type;
1384 guint16 segment_len;
1387 GST_DEBUG ("pts=%" G_GUINT64_FORMAT " and length %d", pts, len);
1389 g_return_val_if_fail (data != NULL, -1);
1391 if (len <= 3) { /* len(0x20 0x00 end_of_PES_data_field_marker) */
1392 GST_WARNING ("Data length too short");
1396 if (data[pos++] != 0x20) {
1397 GST_WARNING ("Tried to handle a PES packet private data that isn't a "
1398 "subtitle packet (does not start with 0x20)");
1402 if (data[pos++] != 0x00) {
1403 GST_WARNING ("'Subtitle stream in this PES packet' was not 0x00, so this "
1404 "is in theory not a DVB subtitle stream (but some other subtitle "
1405 "standard?); bailing out");
1409 while (data[pos++] == DVB_SUB_SYNC_BYTE) {
1410 if ((len - pos) < (2 * 2 + 1)) {
1411 GST_WARNING ("Data after SYNC BYTE too short, less than needed to "
1412 "even get to segment_length");
1415 segment_type = data[pos++];
1416 GST_DEBUG ("=== Segment type is 0x%x", segment_type);
1417 page_id = (data[pos] << 8) | data[pos + 1];
1418 GST_DEBUG ("page_id is 0x%x", page_id);
1420 segment_len = (data[pos] << 8) | data[pos + 1];
1421 GST_DEBUG ("segment_length is %d (0x%x 0x%x)", segment_len, data[pos],
1424 if ((len - pos) < segment_len) {
1425 GST_WARNING ("segment_length was told to be %u, but we only have "
1426 "%d bytes left", segment_len, len - pos);
1429 // TODO: Parse the segment per type (this is probably a leftover TODO that is now done?)
1430 /* FIXME: Handle differing PTS values - all segments of a given display set must be with the same PTS,
1431 * FIXME: but we let it slip and just take it for granted in end_of_display_set */
1432 switch (segment_type) {
1433 case DVB_SUB_SEGMENT_PAGE_COMPOSITION:
1434 GST_DEBUG ("Page composition segment at buffer pos %u", pos);
1435 _dvb_sub_parse_page_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1437 case DVB_SUB_SEGMENT_REGION_COMPOSITION:
1438 GST_DEBUG ("Region composition segment at buffer pos %u", pos);
1439 _dvb_sub_parse_region_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1441 case DVB_SUB_SEGMENT_CLUT_DEFINITION:
1442 GST_DEBUG ("CLUT definition segment at buffer pos %u", pos);
1443 _dvb_sub_parse_clut_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1445 case DVB_SUB_SEGMENT_OBJECT_DATA:
1446 GST_DEBUG ("Object data segment at buffer pos %u", pos);
1447 _dvb_sub_parse_object_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1449 case DVB_SUB_SEGMENT_DISPLAY_DEFINITION:
1450 GST_DEBUG ("display definition segment at buffer pos %u", pos);
1451 _dvb_sub_parse_display_definition_segment (dvb_sub, data + pos,
1454 case DVB_SUB_SEGMENT_END_OF_DISPLAY_SET:
1455 GST_DEBUG ("End of display set at buffer pos %u", pos);
1456 _dvb_sub_parse_end_of_display_set (dvb_sub, page_id, data + pos, segment_len, pts); /* FIXME: Not sure about args */
1459 GST_FIXME ("Unhandled segment type 0x%x", segment_type);
1466 GST_WARNING ("Data ended without a PES data end marker");
1471 GST_LOG ("Processed %d bytes out of %d", pos, len);
1476 * dvb_sub_set_callbacks:
1477 * @dvb_sub: a #DvbSub
1478 * @callbacks: the callbacks to install
1479 * @user_data: a user_data argument for the callback
1481 * Set callback which will be executed when new subpictures are available.
1484 dvb_sub_set_callbacks (DvbSub * dvb_sub, DvbSubCallbacks * callbacks,
1487 g_return_if_fail (dvb_sub != NULL);
1488 g_return_if_fail (callbacks != NULL);
1490 dvb_sub->callbacks = *callbacks;
1491 dvb_sub->user_data = user_data;