1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * libdvbsub - DVB subtitle decoding
4 * Copyright (C) Mart Raudsepp 2009 <mart.raudsepp@artecdesign.ee>
6 * Heavily uses code algorithms ported from ffmpeg's libavcodec/dvbsubdec.c,
7 * especially the segment parsers. The original license applies to this
8 * ported code and the whole code in this file as well.
10 * Original copyright information follows:
13 * DVB subtitle decoding for ffmpeg
14 * Copyright (c) 2005 Ian Caulfield
16 * This file is part of FFmpeg.
18 * FFmpeg is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
23 * FFmpeg is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with FFmpeg; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <string.h> /* memset */
34 #include <gst/gstutils.h> /* GST_READ_UINT16_BE */
35 #include <gst/base/gstbitreader.h> /* GstBitReader */
36 #include "ffmpeg-colorspace.h" /* YUV_TO_RGB1_CCIR */ /* FIXME: Just give YUV data to gstreamer then? */
40 GST_DEBUG_CATEGORY_STATIC (dvbsub_debug);
41 #define GST_CAT_DEFAULT dvbsub_debug
43 static void dvb_sub_init (void);
45 /* FIXME: Are we waiting for an acquisition point before trying to do things? */
46 /* FIXME: In the end convert some of the guint8/16 (especially stack variables) back to gint for access efficiency */
50 * @short_description: a DVB subtitle parsing class
51 * @stability: Unstable
53 * The #DvbSub represents an object used for parsing a DVB subpicture,
54 * and signalling the API user for new bitmaps to show on screen.
57 #define MAX_NEG_CROP 1024
58 static guint8 ff_cropTbl[256 + 2 * MAX_NEG_CROP] = { 0, };
60 #define cm (ff_cropTbl + MAX_NEG_CROP)
62 /* FIXME: This is really ARGB... We might need this configurable for performant
63 * FIXME: use in GStreamer as well if that likes RGBA more (Qt prefers ARGB) */
64 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
66 typedef struct DVBSubCLUT
68 int id; /* default_clut uses -1 for this, so guint8 isn't fine without adaptations first */
74 struct DVBSubCLUT *next;
77 static DVBSubCLUT default_clut;
79 typedef struct DVBSubObjectDisplay
81 /* FIXME: Use more correct sizes */
91 /* FIXME: Should we use GSList? The relating interaction and pointer assigment is quite complex and perhaps unsuited for a plain GSList anyway */
92 struct DVBSubObjectDisplay *region_list_next;
93 struct DVBSubObjectDisplay *object_list_next;
94 } DVBSubObjectDisplay;
96 typedef struct DVBSubObject
98 /* FIXME: Use more correct sizes */
99 int id; /* FIXME: Use guint8 after checking it's fine in all code using it */
103 /* FIXME: Should we use GSList? */
104 DVBSubObjectDisplay *display_list;
105 struct DVBSubObject *next;
108 typedef struct DVBSubRegionDisplay
109 { /* FIXME: Figure out if this structure is only used temporarily in page_segment parser, or also more */
115 struct DVBSubRegionDisplay *next;
116 } DVBSubRegionDisplay;
118 typedef struct DVBSubRegion
123 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 */
128 /* FIXME: Validate these fields existence and exact types */
132 DVBSubObjectDisplay *display_list;
134 struct DVBSubRegion *next;
139 DvbSubCallbacks callbacks;
142 guint8 page_time_out;
143 DVBSubRegion *region_list;
144 DVBSubCLUT *clut_list;
145 DVBSubObject *object_list;
147 int display_list_size;
148 DVBSubRegionDisplay *display_list;
150 DVBSubtitleWindow display_def;
157 } DvbSubPixelDataSubBlockFieldType;
159 static DVBSubObject *
160 get_object (DvbSub * dvb_sub, guint16 object_id)
162 DVBSubObject *ptr = dvb_sub->object_list;
164 while (ptr && ptr->id != object_id) {
172 get_clut (DvbSub * dvb_sub, gint clut_id)
174 DVBSubCLUT *ptr = dvb_sub->clut_list;
176 while (ptr && ptr->id != clut_id) {
183 static DVBSubRegion *
184 get_region (DvbSub * dvb_sub, guint8 region_id)
186 DVBSubRegion *ptr = dvb_sub->region_list;
188 while (ptr && ptr->id != region_id) {
196 delete_region_display_list (DvbSub * dvb_sub, DVBSubRegion * region)
198 DVBSubObject *object, *obj2;
199 DVBSubObject **obj2_ptr;
200 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
202 while (region->display_list) {
203 display = region->display_list;
205 object = get_object (dvb_sub, display->object_id);
208 obj_disp_ptr = &object->display_list;
209 obj_disp = *obj_disp_ptr;
211 while (obj_disp && obj_disp != display) {
212 obj_disp_ptr = &obj_disp->object_list_next;
213 obj_disp = *obj_disp_ptr;
217 *obj_disp_ptr = obj_disp->object_list_next;
219 if (!object->display_list) {
220 obj2_ptr = (DVBSubObject **) & dvb_sub->object_list; /* FIXME: Evil casting */
223 while (obj2 != object) {
225 obj2_ptr = &obj2->next;
229 *obj2_ptr = obj2->next;
231 g_slice_free (DVBSubObject, obj2);
236 region->display_list = display->region_list_next;
238 g_slice_free (DVBSubObjectDisplay, display);
243 delete_state (DvbSub * dvb_sub)
245 DVBSubRegion *region;
247 while (dvb_sub->region_list) {
248 region = dvb_sub->region_list;
250 dvb_sub->region_list = region->next;
252 delete_region_display_list (dvb_sub, region);
254 g_free (region->pbuf);
256 g_slice_free (DVBSubRegion, region);
259 g_slice_free_chain (DVBSubCLUT, dvb_sub->clut_list, next);
260 dvb_sub->clut_list = NULL;
262 /* Should already be NULL */
263 g_warn_if_fail (dvb_sub->object_list == NULL);
266 /* init static data necessary for ffmpeg-colorspace conversion */
268 dsputil_static_init (void)
272 for (i = 0; i < 256; i++)
273 ff_cropTbl[i + MAX_NEG_CROP] = i;
274 for (i = 0; i < MAX_NEG_CROP; i++) {
276 ff_cropTbl[i + MAX_NEG_CROP + 256] = 255;
283 int i, r, g, b, a = 0;
285 GST_DEBUG_CATEGORY_INIT (dvbsub_debug, "dvbsub", 0, "dvbsuboverlay parser");
287 dsputil_static_init (); /* Initializes ff_cropTbl table, used in YUV_TO_RGB conversion */
289 /* Initialize the static default_clut structure, from which other clut
290 * structures are initialized from (to start off with default CLUTs
291 * as defined in the specification). */
292 default_clut.id = -1;
294 default_clut.clut4[0] = RGBA (0, 0, 0, 0);
295 default_clut.clut4[1] = RGBA (255, 255, 255, 255);
296 default_clut.clut4[2] = RGBA (0, 0, 0, 255);
297 default_clut.clut4[3] = RGBA (127, 127, 127, 255);
299 default_clut.clut16[0] = RGBA (0, 0, 0, 0);
300 for (i = 1; i < 16; i++) {
302 r = (i & 1) ? 255 : 0;
303 g = (i & 2) ? 255 : 0;
304 b = (i & 4) ? 255 : 0;
306 r = (i & 1) ? 127 : 0;
307 g = (i & 2) ? 127 : 0;
308 b = (i & 4) ? 127 : 0;
310 default_clut.clut16[i] = RGBA (r, g, b, 255);
313 default_clut.clut256[0] = RGBA (0, 0, 0, 0);
314 for (i = 1; i < 256; i++) {
316 r = (i & 1) ? 255 : 0;
317 g = (i & 2) ? 255 : 0;
318 b = (i & 4) ? 255 : 0;
323 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
324 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
325 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
329 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
330 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
331 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
335 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
336 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
337 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
341 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
342 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
343 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
348 default_clut.clut256[i] = RGBA (r, g, b, a);
353 _dvb_sub_parse_page_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
355 { /* FIXME: Use guint for buf_size here and in many other places? */
356 DVBSubRegionDisplay *display;
357 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
359 const guint8 *buf_end = buf + buf_size;
363 #ifndef GST_DISABLE_GST_DEBUG
364 static int counter = 0; /* FIXME: static counter? I think not.. */
370 dvb_sub->page_time_out = *buf++;
371 page_state = ((*buf++) >> 2) & 3;
373 #ifndef GST_DISABLE_GST_DEBUG
375 static const gchar *page_state_str[4] = {
376 "Normal case", "ACQUISITION POINT", "Mode Change", "RESERVED"
380 GST_DEBUG ("PAGE: %d: page_id = %u, length = %d, page_time_out = %u secs, "
381 "page_state = %s", counter, page_id, buf_size, dvb_sub->page_time_out,
382 page_state_str[page_state]);
386 if (page_state == 2) { /* Mode change */
387 delete_state (dvb_sub);
390 tmp_display_list = dvb_sub->display_list;
391 dvb_sub->display_list = NULL;
392 dvb_sub->display_list_size = 0;
394 while (buf + 5 < buf_end) {
398 display = tmp_display_list;
399 tmp_ptr = &tmp_display_list;
401 while (display && display->region_id != region_id) {
402 tmp_ptr = &display->next;
403 display = display->next;
407 display = g_slice_new0 (DVBSubRegionDisplay);
409 display->region_id = region_id;
411 display->x_pos = GST_READ_UINT16_BE (buf);
413 display->y_pos = GST_READ_UINT16_BE (buf);
416 *tmp_ptr = display->next;
418 display->next = dvb_sub->display_list;
419 dvb_sub->display_list = display;
420 dvb_sub->display_list_size++;
422 GST_LOG ("PAGE %d: REGION information: ID = %u, address = %ux%u",
423 counter, region_id, display->x_pos, display->y_pos);
426 while (tmp_display_list) {
427 display = tmp_display_list;
429 tmp_display_list = display->next;
431 g_slice_free (DVBSubRegionDisplay, display);
436 _dvb_sub_parse_region_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
439 const guint8 *buf_end = buf + buf_size;
442 DVBSubRegion *region;
443 DVBSubObject *object;
444 DVBSubObjectDisplay *object_display;
452 region = get_region (dvb_sub, region_id);
454 if (!region) { /* Create a new region */
455 region = g_slice_new0 (DVBSubRegion);
456 region->id = region_id;
457 region->next = dvb_sub->region_list;
458 dvb_sub->region_list = region;
461 fill = ((*buf++) >> 3) & 1;
463 region->width = GST_READ_UINT16_BE (buf);
465 region->height = GST_READ_UINT16_BE (buf);
468 if (region->width * region->height != region->buf_size) { /* FIXME: Read closer from spec what happens when dimensions change */
470 g_free (region->pbuf);
472 region->buf_size = region->width * region->height;
474 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 */
476 fill = 1; /* FIXME: Validate from spec that fill is forced on (in the following codes context) when dimensions change */
479 region->depth = 1 << (((*buf++) >> 2) & 7);
480 if (region->depth < 2 || region->depth > 8) {
481 GST_WARNING ("region depth %d is invalid", region->depth);
482 region->depth = 4; /* FIXME: Check from spec this is the default? */
485 region->clut = *buf++;
487 if (region->depth == 8)
488 region->bgcolor = *buf++;
492 if (region->depth == 4)
493 region->bgcolor = (((*buf++) >> 4) & 15);
495 region->bgcolor = (((*buf++) >> 2) & 3);
498 GST_DEBUG ("REGION: id = %u, (%ux%u)@%u-bit", region_id, region->width,
499 region->height, region->depth);
502 memset (region->pbuf, region->bgcolor, region->buf_size);
503 GST_DEBUG ("REGION: filling region (%u) with bgcolor = %u", region->id,
507 delete_region_display_list (dvb_sub, region); /* Delete the region display list for current region - FIXME: why? */
509 while (buf + 6 <= buf_end) {
510 object_id = GST_READ_UINT16_BE (buf);
513 object = get_object (dvb_sub, object_id);
516 object = g_slice_new0 (DVBSubObject);
518 object->id = object_id;
520 object->next = dvb_sub->object_list;
521 dvb_sub->object_list = object;
524 object->type = (*buf) >> 6;
526 object_display = g_slice_new0 (DVBSubObjectDisplay);
528 object_display->object_id = object_id;
529 object_display->region_id = region_id;
531 object_display->x_pos = GST_READ_UINT16_BE (buf) & 0xfff;
533 object_display->y_pos = GST_READ_UINT16_BE (buf) & 0xfff;
536 if ((object->type == 1 || object->type == 2) && buf + 2 <= buf_end) {
537 object_display->fgcolor = *buf++;
538 object_display->bgcolor = *buf++;
541 object_display->region_list_next = region->display_list;
542 region->display_list = object_display;
544 object_display->object_list_next = object->display_list;
545 object->display_list = object_display;
547 GST_DEBUG ("REGION DATA: object_id = %u, region_id = %u, pos = %ux%u, "
548 "obj_type = %u", object->id, region->id, object_display->x_pos,
549 object_display->y_pos, object->type);
551 if (object->type == 1 || object->type == 2) {
552 GST_DEBUG ("REGION DATA: fgcolor = %u, bgcolor = %u",
553 object_display->fgcolor, object_display->bgcolor);
559 _dvb_sub_parse_clut_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
562 const guint8 *buf_end = buf + buf_size;
565 int entry_id, depth, full_range;
566 int y, cr, cb, alpha;
567 int r, g, b, r_add, g_add, b_add;
569 GST_MEMDUMP ("DVB clut packet", buf, buf_size);
574 clut = get_clut (dvb_sub, clut_id);
577 clut = g_slice_new (DVBSubCLUT); /* FIXME-MEMORY-LEAK: This seems to leak per valgrind */
579 memcpy (clut, &default_clut, sizeof (DVBSubCLUT));
583 clut->next = dvb_sub->clut_list;
584 dvb_sub->clut_list = clut;
587 while (buf + 4 < buf_end) {
590 depth = (*buf) & 0xe0;
593 GST_WARNING ("Invalid clut depth 0x%x!", *buf);
597 full_range = (*buf++) & 1;
606 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
607 cb = (buf[1] << 2) & 0xf0;
608 alpha = (buf[1] << 6) & 0xc0;
616 YUV_TO_RGB1_CCIR (cb, cr);
617 YUV_TO_RGB2_CCIR (r, g, b, y);
619 GST_DEBUG ("CLUT DEFINITION: clut %d := (%d,%d,%d,%d)", entry_id, r, g, b,
623 clut->clut4[entry_id] = RGBA (r, g, b, 255 - alpha);
625 clut->clut16[entry_id] = RGBA (r, g, b, 255 - alpha);
627 clut->clut256[entry_id] = RGBA (r, g, b, 255 - alpha);
631 // FFMPEG-FIXME: The same code in ffmpeg is much more complex, it could use the same
632 // FFMPEG-FIXME: refactoring as done here
634 _dvb_sub_read_2bit_string (guint8 * destbuf, gint dbuf_len,
635 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
637 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
638 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
640 gboolean stop_parsing = FALSE;
642 guint32 pixels_read = 0;
644 static gboolean warning_shown = FALSE;
645 if (!warning_shown) {
646 g_warning ("Parsing 2bit color DVB sub-picture. This is not tested at all. "
647 "If you see this message, please provide the developers with sample "
648 "media with these subtitles, if possible.");
649 warning_shown = TRUE;
652 GST_TRACE ("dbuf_len = %d", dbuf_len);
654 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
655 guint run_length = 0, clut_index = 0;
656 gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
658 if (bits) { /* 2-bit_pixel-code */
661 } else { /* 2-bit_zero */
662 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
663 if (bits == 1) { /* switch_1 == '1' */
664 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 3);
666 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
667 } else { /* switch_1 == '0' */
668 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
669 if (bits == 1) { /* switch_2 == '1' */
670 run_length = 1; /* 1x pseudo-colour '00' */
671 } else { /* switch_2 == '0' */
672 gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
673 switch (bits) { /* switch_3 */
674 case 0x0: /* end of 2-bit/pixel_code_string */
677 case 0x1: /* two pixels shall be set to pseudo colour (entry) '00' */
680 case 0x2: /* the following 6 bits contain run length coded pixel data */
681 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 4);
683 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
685 case 0x3: /* the following 10 bits contain run length coded pixel data */
686 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 8);
688 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
695 /* If run_length is zero, continue. Only case happening is when
696 * stop_parsing is TRUE too, so next cycle shouldn't run */
700 /* Trim the run_length to not go beyond the line end and consume
701 * it from remaining length of dest line */
702 run_length = MIN (run_length, dbuf_len);
703 dbuf_len -= run_length;
705 /* Make clut_index refer to the index into the desired bit depths
706 * CLUT definition table */
708 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
710 /* Now we can simply memset run_length count of destination bytes
711 * to clut_index, but only if not non_modifying */
712 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer, "
713 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
715 if (!(non_mod == 1 && bits == 1))
716 memset (destbuf, clut_index, run_length);
718 destbuf += run_length;
719 pixels_read += run_length;
722 // FIXME: Test skip_to_byte instead of adding 7 bits, once everything else is working good
723 //gst_bit_reader_skip_to_byte (&gb);
724 *srcbuf += (gst_bit_reader_get_pos (&gb) + 7) >> 3;
726 GST_TRACE ("PIXEL: returning, read %u pixels", pixels_read);
727 // FIXME: Shouldn't need this variable if tracking things in the loop better
731 // FFMPEG-FIXME: The same code in ffmpeg is much more complex, it could use the same
732 // FFMPEG-FIXME: refactoring as done here, explained in commit 895296c3
734 _dvb_sub_read_4bit_string (guint8 * destbuf, gint dbuf_len,
735 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
737 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
738 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
739 gboolean stop_parsing = FALSE;
741 guint32 pixels_read = 0;
743 GST_TRACE ("RUNLEN: srcbuf position %p, buf_size = %d; destination buffer "
744 "size is %d @ %p", *srcbuf, buf_size, dbuf_len, destbuf);
746 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
747 guint run_length = 0, clut_index = 0;
748 gst_bit_reader_get_bits_uint32 (&gb, &bits, 4);
754 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
755 if (bits == 0) { /* switch_1 == '0' */
756 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 3);
762 } else { /* switch_1 == '1' */
763 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
764 if (bits == 0) { /* switch_2 == '0' */
765 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 2);
767 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
768 } else { /* switch_2 == '1' */
769 gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
771 case 0x0: /* switch_3 == '00' */
772 run_length = 1; /* 1 pixel of pseudo-color 0 */
774 case 0x1: /* switch_3 == '01' */
775 run_length = 2; /* 2 pixels of pseudo-color 0 */
777 case 0x2: /* switch_3 == '10' */
778 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 4);
780 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
782 case 0x3: /* switch_3 == '11' */
783 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 8);
785 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
792 /* If run_length is zero, continue. Only case happening is when
793 * stop_parsing is TRUE too, so next cycle shouldn't run */
797 /* Trim the run_length to not go beyond the line end and consume
798 * it from remaining length of dest line */
799 run_length = MIN (run_length, dbuf_len);
800 dbuf_len -= run_length;
802 /* Make clut_index refer to the index into the desired bit depths
803 * CLUT definition table */
805 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
807 /* Now we can simply memset run_length count of destination bytes
808 * to clut_index, but only if not non_modifying */
809 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer; "
810 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
812 if (!(non_mod == 1 && bits == 1))
813 memset (destbuf, clut_index, run_length);
815 destbuf += run_length;
816 pixels_read += run_length;
819 // FIXME: Test skip_to_byte instead of adding 7 bits, once everything else is working good
820 //gst_bit_reader_skip_to_byte (&gb);
821 *srcbuf += (gst_bit_reader_get_pos (&gb) + 7) >> 3;
823 GST_LOG ("Returning with %u pixels read", pixels_read);
825 // FIXME: Shouldn't need this variable if tracking things in the loop better
830 _dvb_sub_read_8bit_string (guint8 * destbuf, gint dbuf_len,
831 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
833 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
834 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
836 gboolean stop_parsing = FALSE;
838 guint32 pixels_read = 0;
840 static gboolean warning_shown = FALSE;
841 if (!warning_shown) {
843 ("Parsing 8bit color DVB sub-picture. This is not tested at all. If you see this message, "
844 "please provide the developers with sample media with these subtitles, if possible.");
845 warning_shown = TRUE;
848 GST_LOG ("dbuf_len = %d", dbuf_len);
850 /* FFMPEG-FIXME: ffmpeg uses a manual byte walking algorithm, which might be more performant,
851 * FFMPEG-FIXME: but it does almost absolutely no buffer length checking, so could walk over
852 * FFMPEG-FIXME: memory boundaries. While we don't check gst_bit_reader_get_bits_uint32
853 * FFMPEG-FIXME: return values either and therefore might get some pixels corrupted, we at
854 * FFMPEG-FIXME: lest have no chance of reading memory we don't own and visual corruption
855 * FFMPEG-FIXME: is guaranteed anyway when not all bytes are present */
856 /* Rephrased - it's better to work with bytes with default value '0' instead of reading from memory we don't own. */
857 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
858 guint run_length = 0, clut_index = 0;
859 gst_bit_reader_get_bits_uint32 (&gb, &bits, 8);
861 if (bits) { /* 8-bit_pixel-code */
864 } else { /* 8-bit_zero */
865 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
866 if (bits == 0) { /* switch_1 == '0' */
867 /* run_length_1-127 for pseudo-colour _entry) '0x00' */
868 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 7);
869 if (run_length == 0) { /* end_of_string_signal */
872 } else { /* switch_1 == '1' */
873 /* run_length_3-127 */
874 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 7);
875 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 8);
877 if (run_length < 3) {
878 GST_WARNING ("runlength value was %u, but the spec requires it "
879 "must be >=3", run_length);
884 /* If run_length is zero, continue. Only case happening is when
885 * stop_parsing is TRUE too, so next cycle shouldn't run */
889 /* Trim the run_length to not go beyond the line end and consume
890 * it from remaining length of dest line */
891 run_length = MIN (run_length, dbuf_len);
892 dbuf_len -= run_length;
894 /* Make clut_index refer to the index into the desired bit depths
895 * CLUT definition table */
897 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
899 /* Now we can simply memset run_length count of destination bytes
900 * to clut_index, but only if not non_modifying */
901 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer; "
902 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
904 if (!(non_mod == 1 && bits == 1))
905 memset (destbuf, clut_index, run_length);
907 destbuf += run_length;
908 pixels_read += run_length;
911 GST_LOG ("Returning with %u pixels read", pixels_read);
913 // FIXME: Shouldn't need this variable if tracking things in the loop better
918 _dvb_sub_parse_pixel_data_block (DvbSub * dvb_sub,
919 DVBSubObjectDisplay * display, const guint8 * buf, gint buf_size,
920 DvbSubPixelDataSubBlockFieldType top_bottom, guint8 non_mod)
922 DVBSubRegion *region = get_region (dvb_sub, display->region_id);
923 const guint8 *buf_end = buf + buf_size;
927 gboolean dest_buf_filled = FALSE;
929 guint8 map2to4[] = { 0x0, 0x7, 0x8, 0xf };
930 guint8 map2to8[] = { 0x00, 0x77, 0x88, 0xff };
931 guint8 map4to8[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
932 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
936 GST_LOG ("DVB pixel block size %d, %s field:", buf_size,
937 top_bottom ? "bottom" : "top");
939 GST_MEMDUMP ("packet", buf, buf_size);
941 if (region == NULL) {
942 GST_LOG ("Region is NULL, returning");
948 x_pos = display->x_pos;
949 y_pos = display->y_pos;
951 if ((y_pos & 1) != top_bottom)
954 while (buf < buf_end) {
955 GST_LOG ("Iteration start, %u bytes missing from end; buf = %p, "
956 "buf_end = %p; Region is number %u, with a dimension of %dx%d; "
957 "We are at position %dx%d", (guint) (buf_end - buf), buf, buf_end,
958 region->id, region->width, region->height, x_pos, y_pos);
960 // FFMPEG-FIXME: ffmpeg doesn't check for equality and so can overflow destination buffer later on with bad input data
961 // 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
962 // FIXME: Removed x_pos checking here, because we don't want to turn dest_buf_filled to TRUE permanently in that case
963 // FIXME: We assume that region->width - x_pos as dbuf_len to read_nbit_string will take care of that case nicely;
964 // FIXME: That is, that read_nbit_string never scribbles anything if dbuf_len passed to it is zero due to this.
965 if (y_pos >= region->height) {
966 dest_buf_filled = TRUE;
971 if (dest_buf_filled) {
972 /* FIXME: Be more verbose */
973 GST_WARNING ("Invalid object location for data_type 0x%x!",
975 GST_MEMDUMP ("Remaining data after invalid object location:", buf,
976 (guint) (buf_end - buf));
980 if (region->depth == 8)
982 else if (region->depth == 4)
987 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
988 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
990 _dvb_sub_read_2bit_string (pbuf + (y_pos * region->width) + x_pos,
991 region->width - x_pos, &buf, buf_end - buf, non_mod, map_table);
994 if (dest_buf_filled) {
995 /* FIXME: Be more verbose */
996 GST_WARNING ("Invalid object location for data_type 0x%x!",
998 GST_MEMDUMP ("Remaining data after invalid object location:", buf,
1000 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)
1003 if (region->depth < 4) {
1004 GST_WARNING ("4-bit pixel string in %d-bit region!", region->depth);
1008 if (region->depth == 8)
1009 map_table = map4to8;
1013 GST_LOG ("READ_4BIT_STRING: String data into position %dx%d; "
1014 "buf before is %p", x_pos, y_pos, buf);
1015 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
1016 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
1018 _dvb_sub_read_4bit_string (pbuf + (y_pos * region->width) + x_pos,
1019 region->width - x_pos, &buf, buf_end - buf, non_mod, map_table);
1020 GST_DEBUG ("READ_4BIT_STRING finished: buf pointer now %p", buf);
1023 if (dest_buf_filled) {
1024 /* FIXME: Be more verbose */
1025 GST_WARNING ("Invalid object location for data_type 0x%x!",
1027 GST_MEMDUMP ("Remaining data after invalid object location:",
1028 buf, (guint) (buf_end - buf));
1032 if (region->depth < 8) {
1033 GST_WARNING ("8-bit pixel string in %d-bit region!", region->depth);
1036 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
1037 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
1039 _dvb_sub_read_8bit_string (pbuf + (y_pos * region->width) + x_pos,
1040 region->width - x_pos, &buf, buf_end - buf, non_mod, NULL);
1044 GST_DEBUG ("handling map2to4 table data");
1045 /* FIXME: I don't see any guards about buffer size here - buf++ happens with the switch, but
1046 * FIXME: buffer is walked without length checks? Same deal in other map table cases */
1047 map2to4[0] = (*buf) >> 4;
1048 map2to4[1] = (*buf++) & 0xf;
1049 map2to4[2] = (*buf) >> 4;
1050 map2to4[3] = (*buf++) & 0xf;
1053 GST_DEBUG ("handling map2to8 table data");
1054 for (i = 0; i < 4; i++)
1055 map2to8[i] = *buf++;
1058 GST_DEBUG ("handling map4to8 table data");
1059 for (i = 0; i < 16; i++)
1060 map4to8[i] = *buf++;
1063 GST_DEBUG ("end of object line code encountered");
1064 x_pos = display->x_pos;
1068 /* FIXME: Do we consume word align stuffing byte that could follow top/bottom data? */
1069 GST_WARNING ("Unknown/unsupported pixel block 0x%x", *(buf - 1));
1075 _dvb_sub_parse_object_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
1078 const guint8 *buf_end = buf + buf_size;
1080 DVBSubObject *object;
1082 guint8 coding_method, non_modifying_color;
1084 object_id = GST_READ_UINT16_BE (buf);
1087 object = get_object (dvb_sub, object_id);
1089 GST_DEBUG ("OBJECT: a new object segment has occurred for object_id = %u",
1093 GST_WARNING ("Nothing known about object with ID %u yet, bailing out",
1098 coding_method = ((*buf) >> 2) & 3;
1099 non_modifying_color = ((*buf++) >> 1) & 1;
1101 if (coding_method == 0) {
1102 const guint8 *block;
1103 DVBSubObjectDisplay *display;
1104 guint16 top_field_len, bottom_field_len;
1106 top_field_len = GST_READ_UINT16_BE (buf);
1108 bottom_field_len = GST_READ_UINT16_BE (buf);
1111 if (buf + top_field_len + bottom_field_len > buf_end) {
1112 GST_WARNING ("Field data size too large");
1116 /* FIXME: Potential optimization opportunity here - parse the object pixmap only once, and copy it to all the
1117 * FIXME: regions that need it. One object being in multiple regions is a rare occurrence in real life, however */
1118 for (display = object->display_list; display;
1119 display = display->object_list_next) {
1122 GST_DEBUG ("OBJECT: parsing top and bottom part of object id %d; "
1123 "top_field_len = %u, bottom_field_len = %u",
1124 display->object_id, top_field_len, bottom_field_len);
1126 _dvb_sub_parse_pixel_data_block (dvb_sub, display, block, top_field_len,
1127 TOP_FIELD, non_modifying_color);
1129 if (bottom_field_len > 0)
1130 block = buf + top_field_len;
1132 bottom_field_len = top_field_len;
1134 _dvb_sub_parse_pixel_data_block (dvb_sub, display, block,
1135 bottom_field_len, BOTTOM_FIELD, non_modifying_color);
1138 } else if (coding_method == 1) {
1139 GST_FIXME ("'a string of characters' coding method not supported yet!");
1141 GST_WARNING ("Unknown object coding 0x%x", coding_method);
1146 _dvb_sub_parse_display_definition_segment (DvbSub * dvb_sub, guint8 * buf,
1149 int dds_version, info_byte;
1155 dds_version = info_byte >> 4;
1157 if (dvb_sub->display_def.version == dds_version)
1158 return 0; /* already have this display definition version */
1160 dvb_sub->display_def.version = dds_version;
1161 dvb_sub->display_def.display_width = GST_READ_UINT16_BE (buf) + 1;
1163 dvb_sub->display_def.display_height = GST_READ_UINT16_BE (buf) + 1;
1166 dvb_sub->display_def.window_flag = info_byte & 1 << 3;
1168 if (buf_size >= 13 && dvb_sub->display_def.window_flag) {
1169 dvb_sub->display_def.window_x = GST_READ_UINT16_BE (buf);
1171 dvb_sub->display_def.window_y = GST_READ_UINT16_BE (buf);
1173 dvb_sub->display_def.window_width =
1174 GST_READ_UINT16_BE (buf) - dvb_sub->display_def.window_x + 1;
1176 dvb_sub->display_def.window_height =
1177 GST_READ_UINT16_BE (buf) - dvb_sub->display_def.window_y + 1;
1185 _dvb_sub_parse_end_of_display_set (DvbSub * dvb_sub, guint16 page_id,
1186 guint8 * buf, gint buf_size, guint64 pts)
1188 DVBSubtitles *sub = g_slice_new0 (DVBSubtitles);
1190 DVBSubRegion *region;
1191 DVBSubRegionDisplay *display;
1192 DVBSubtitleRect *rect;
1194 guint32 *clut_table;
1197 static unsigned counter = 0; /* DEBUG use only *//* FIXME: get rid */
1199 GST_DEBUG ("DISPLAY SET END: page_id = %u, length = %d", page_id, buf_size);
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 sub->num_rects = dvb_sub->display_list_size;
1210 if (sub->num_rects > 0) {
1211 // FIXME-MEMORY-LEAK: This structure is not freed up yet
1212 sub->rects = g_malloc0 (sizeof (*sub->rects) * sub->num_rects); /* GSlice? */
1213 for (i = 0; i < sub->num_rects; i++)
1214 sub->rects[i] = g_malloc0 (sizeof (*sub->rects[i])); /* GSlice? */
1219 /* copy subtitle display and window information */
1220 sub->display_def = dvb_sub->display_def;
1222 for (display = dvb_sub->display_list; display; display = display->next) {
1223 region = get_region (dvb_sub, display->region_id);
1224 rect = sub->rects[i];
1229 rect->x = display->x_pos;
1230 rect->y = display->y_pos;
1231 rect->w = region->width;
1232 rect->h = region->height;
1233 #if 0 /* FIXME: Don't think we need to save the number of colors in the palette when we are saving as RGBA? */
1234 rect->nb_colors = 16;
1236 #if 0 /* FIXME: Needed to be specified once we support strings of characters based subtitles */
1237 rect->type = SUBTITLE_BITMAP;
1239 rect->pict.rowstride = region->width;
1240 rect->pict.palette_bits_count = region->depth;
1242 clut = get_clut (dvb_sub, region->clut);
1245 clut = &default_clut;
1247 switch (region->depth) {
1249 clut_table = clut->clut4;
1252 clut_table = clut->clut256;
1256 clut_table = clut->clut16;
1260 /* FIXME: Tweak this to be saved in a format most suitable for Qt and GStreamer instead.
1261 * Currently kept in AVPicture for quick save_display_set testing */
1262 rect->pict.palette = g_malloc ((1 << region->depth) * sizeof (guint32)); /* FIXME: Can we use GSlice here? */
1263 memcpy (rect->pict.palette, clut_table,
1264 (1 << region->depth) * sizeof (guint32));
1266 GST_MEMDUMP ("rect->pict.data.palette content",
1267 (guint8 *) rect->pict.palette, (1 << region->depth) * sizeof (guint32));
1269 rect->pict.data = g_malloc (region->buf_size); /* FIXME: Can we use GSlice here? */
1270 memcpy (rect->pict.data, region->pbuf, region->buf_size);
1273 GST_DEBUG ("DISPLAY: an object rect created: number %u, iteration %u, "
1274 "pos: %d:%d, size: %dx%d", counter, i, rect->x, rect->y, rect->w,
1277 GST_MEMDUMP ("rect->pict.data content", rect->pict.data, region->buf_size);
1283 sub->page_time_out = dvb_sub->page_time_out;
1286 if (dvb_sub->callbacks.new_data) {
1287 dvb_sub->callbacks.new_data (dvb_sub, sub, dvb_sub->user_data);
1289 /* No-one responsible to clean up memory, so do it ourselves */
1290 /* FIXME: Just don't bother with all this palette image creation in the first place then... */
1291 dvb_subtitles_free (sub);
1294 return 1; /* FIXME: The caller of this function is probably supposed to do something with the return value */
1298 dvb_subtitles_free (DVBSubtitles * sub)
1301 DVBSubtitleRect *rect;
1306 /* Now free up all the temporary memory we allocated */
1307 for (i = 0; i < sub->num_rects; ++i) {
1308 rect = sub->rects[i];
1310 g_free (rect->pict.palette);
1311 g_free (rect->pict.data);
1314 g_free (sub->rects);
1315 g_slice_free (DVBSubtitles, sub);
1321 static gsize inited = 0;
1324 if (g_once_init_enter (&inited)) {
1326 g_once_init_leave (&inited, TRUE);
1329 sub = g_slice_new0 (DvbSub);
1331 /* TODO: Add initialization code here */
1332 /* FIXME: Do we have a reason to initiate the members to zero, or are we guaranteed that anyway? */
1333 sub->region_list = NULL;
1334 sub->object_list = NULL;
1335 sub->page_time_out = 0; /* FIXME: Maybe 255 instead? */
1336 sub->pes_buffer = g_string_new (NULL);
1338 /* display/window information */
1339 sub->display_def.version = -1;
1340 sub->display_def.window_flag = 0;
1341 sub->display_def.display_width = 720;
1342 sub->display_def.display_height = 576;
1348 dvb_sub_free (DvbSub * sub)
1350 /* TODO: Add deinitalization code here */
1351 /* FIXME: Clear up region_list contents */
1353 g_string_free (sub->pes_buffer, TRUE);
1354 g_slice_free (DvbSub, sub);
1357 #define DVB_SUB_SEGMENT_PAGE_COMPOSITION 0x10
1358 #define DVB_SUB_SEGMENT_REGION_COMPOSITION 0x11
1359 #define DVB_SUB_SEGMENT_CLUT_DEFINITION 0x12
1360 #define DVB_SUB_SEGMENT_OBJECT_DATA 0x13
1361 #define DVB_SUB_SEGMENT_DISPLAY_DEFINITION 0x14
1362 #define DVB_SUB_SEGMENT_END_OF_DISPLAY_SET 0x80
1363 #define DVB_SUB_SEGMENT_STUFFING 0xFF
1365 #define DVB_SUB_SYNC_BYTE 0x0f
1367 * dvb_sub_feed_with_pts:
1368 * @dvb_sub: a #DvbSub
1369 * @pts: The PTS of the data
1370 * @data: The data to feed to the parser
1371 * @len: Length of the data
1373 * Feeds the DvbSub parser with new binary data to parse,
1374 * with an associated PTS value. E.g, data left after PES
1375 * packet header has been already parsed, which contains
1376 * the PTS information).
1378 * Return value: -1 if data was unhandled (e.g, not a subtitle packet),
1379 * -2 if data parsing was unsuccesful (e.g, length was invalid),
1380 * 0 or positive if data was handled. If positive, then amount of data consumed on success. FIXME: List the positive return values.
1383 dvb_sub_feed_with_pts (DvbSub * dvb_sub, guint64 pts, guint8 * data, gint len)
1385 unsigned int pos = 0;
1386 guint8 segment_type;
1387 guint16 segment_len;
1390 GST_DEBUG ("pts=%" G_GUINT64_FORMAT " and length %d", pts, len);
1392 g_return_val_if_fail (data != NULL, -1);
1394 if (len <= 3) { /* len(0x20 0x00 end_of_PES_data_field_marker) */
1395 GST_WARNING ("Data length too short");
1399 if (data[pos++] != 0x20) {
1400 GST_WARNING ("Tried to handle a PES packet private data that isn't a "
1401 "subtitle packet (does not start with 0x20)");
1405 if (data[pos++] != 0x00) {
1406 GST_WARNING ("'Subtitle stream in this PES packet' was not 0x00, so this "
1407 "is in theory not a DVB subtitle stream (but some other subtitle "
1408 "standard?); bailing out");
1412 while (data[pos++] == DVB_SUB_SYNC_BYTE) {
1413 if ((len - pos) < (2 * 2 + 1)) {
1414 GST_WARNING ("Data after SYNC BYTE too short, less than needed to "
1415 "even get to segment_length");
1418 segment_type = data[pos++];
1419 GST_DEBUG ("=== Segment type is 0x%x", segment_type);
1420 page_id = (data[pos] << 8) | data[pos + 1];
1421 GST_DEBUG ("page_id is 0x%x", page_id);
1423 segment_len = (data[pos] << 8) | data[pos + 1];
1424 GST_DEBUG ("segment_length is %d (0x%x 0x%x)", segment_len, data[pos],
1427 if ((len - pos) < segment_len) {
1428 GST_WARNING ("segment_length was told to be %u, but we only have "
1429 "%d bytes left", segment_len, len - pos);
1432 // TODO: Parse the segment per type (this is probably a leftover TODO that is now done?)
1433 /* FIXME: Handle differing PTS values - all segments of a given display set must be with the same PTS,
1434 * FIXME: but we let it slip and just take it for granted in end_of_display_set */
1435 switch (segment_type) {
1436 case DVB_SUB_SEGMENT_PAGE_COMPOSITION:
1437 GST_DEBUG ("Page composition segment at buffer pos %u", pos);
1438 _dvb_sub_parse_page_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1440 case DVB_SUB_SEGMENT_REGION_COMPOSITION:
1441 GST_DEBUG ("Region composition segment at buffer pos %u", pos);
1442 _dvb_sub_parse_region_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1444 case DVB_SUB_SEGMENT_CLUT_DEFINITION:
1445 GST_DEBUG ("CLUT definition segment at buffer pos %u", pos);
1446 _dvb_sub_parse_clut_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1448 case DVB_SUB_SEGMENT_OBJECT_DATA:
1449 GST_DEBUG ("Object data segment at buffer pos %u", pos);
1450 _dvb_sub_parse_object_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1452 case DVB_SUB_SEGMENT_DISPLAY_DEFINITION:
1453 GST_DEBUG ("display definition segment at buffer pos %u", pos);
1454 _dvb_sub_parse_display_definition_segment (dvb_sub, data + pos,
1457 case DVB_SUB_SEGMENT_END_OF_DISPLAY_SET:
1458 GST_DEBUG ("End of display set at buffer pos %u", pos);
1459 _dvb_sub_parse_end_of_display_set (dvb_sub, page_id, data + pos, segment_len, pts); /* FIXME: Not sure about args */
1462 GST_FIXME ("Unhandled segment type 0x%x", segment_type);
1469 GST_WARNING ("Data ended without a PES data end marker");
1474 GST_LOG ("Processed %d bytes out of %d", pos, len);
1479 * dvb_sub_set_callbacks:
1480 * @dvb_sub: a #DvbSub
1481 * @callbacks: the callbacks to install
1482 * @user_data: a user_data argument for the callback
1484 * Set callback which will be executed when new subpictures are available.
1487 dvb_sub_set_callbacks (DvbSub * dvb_sub, DvbSubCallbacks * callbacks,
1490 g_return_if_fail (dvb_sub != NULL);
1491 g_return_if_fail (callbacks != NULL);
1493 dvb_sub->callbacks = *callbacks;
1494 dvb_sub->user_data = user_data;