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 */
39 GST_DEBUG_CATEGORY_STATIC (dvbsub_debug);
40 #define GST_CAT_DEFAULT dvbsub_debug
42 static void dvb_sub_init (void);
44 /* FIXME: Are we waiting for an acquisition point before trying to do things? */
45 /* FIXME: In the end convert some of the guint8/16 (especially stack variables) back to gint for access efficiency */
49 * @short_description: a DVB subtitle parsing class
50 * @stability: Unstable
52 * The #DvbSub represents an object used for parsing a DVB subpicture,
53 * and signalling the API user for new bitmaps to show on screen.
56 #define MAX_NEG_CROP 1024
58 #define AYUV(y,u,v,a) (((a) << 24) | ((y) << 16) | ((u) << 8) | (v))
59 #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)))
62 typedef struct DVBSubCLUT
64 int id; /* default_clut uses -1 for this, so guint8 isn't fine without adaptations first */
70 struct DVBSubCLUT *next;
73 static DVBSubCLUT default_clut;
75 typedef struct DVBSubObjectDisplay
77 /* FIXME: Use more correct sizes */
87 /* FIXME: Should we use GSList? The relating interaction and pointer assigment is quite complex and perhaps unsuited for a plain GSList anyway */
88 struct DVBSubObjectDisplay *region_list_next;
89 struct DVBSubObjectDisplay *object_list_next;
90 } DVBSubObjectDisplay;
92 typedef struct DVBSubObject
94 /* FIXME: Use more correct sizes */
95 int id; /* FIXME: Use guint8 after checking it's fine in all code using it */
99 /* FIXME: Should we use GSList? */
100 DVBSubObjectDisplay *display_list;
101 struct DVBSubObject *next;
104 typedef struct DVBSubRegionDisplay
105 { /* FIXME: Figure out if this structure is only used temporarily in page_segment parser, or also more */
111 struct DVBSubRegionDisplay *next;
112 } DVBSubRegionDisplay;
114 typedef struct DVBSubRegion
119 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 */
124 /* FIXME: Validate these fields existence and exact types */
128 DVBSubObjectDisplay *display_list;
130 struct DVBSubRegion *next;
135 DvbSubCallbacks callbacks;
138 guint8 page_time_out;
139 DVBSubRegion *region_list;
140 DVBSubCLUT *clut_list;
141 DVBSubObject *object_list;
143 int display_list_size;
144 DVBSubRegionDisplay *display_list;
146 DVBSubtitleWindow display_def;
153 } DvbSubPixelDataSubBlockFieldType;
156 rgb_to_y (gint r, gint g, gint b)
160 ret = (gint) (((19595 * r) >> 16) + ((38470 * g) >> 16) + ((7471 * b) >> 16));
161 ret = CLAMP (ret, 0, 255);
166 rgb_to_u (gint r, gint g, gint b)
171 (gint) (-((11059 * r) >> 16) - ((21709 * g) >> 16) + ((32768 * b) >> 16) +
173 ret = CLAMP (ret, 0, 255);
178 rgb_to_v (gint r, gint g, gint b)
183 (gint) (((32768 * r) >> 16) - ((27439 * g) >> 16) - ((5329 * b) >> 16) +
185 ret = CLAMP (ret, 0, 255);
189 static DVBSubObject *
190 get_object (DvbSub * dvb_sub, guint16 object_id)
192 DVBSubObject *ptr = dvb_sub->object_list;
194 while (ptr && ptr->id != object_id) {
202 get_clut (DvbSub * dvb_sub, gint clut_id)
204 DVBSubCLUT *ptr = dvb_sub->clut_list;
206 while (ptr && ptr->id != clut_id) {
213 static DVBSubRegion *
214 get_region (DvbSub * dvb_sub, guint8 region_id)
216 DVBSubRegion *ptr = dvb_sub->region_list;
218 while (ptr && ptr->id != region_id) {
226 delete_region_display_list (DvbSub * dvb_sub, DVBSubRegion * region)
228 DVBSubObject *object, *obj2;
229 DVBSubObject **obj2_ptr;
230 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
232 while (region->display_list) {
233 display = region->display_list;
235 object = get_object (dvb_sub, display->object_id);
238 obj_disp_ptr = &object->display_list;
239 obj_disp = *obj_disp_ptr;
241 while (obj_disp && obj_disp != display) {
242 obj_disp_ptr = &obj_disp->object_list_next;
243 obj_disp = *obj_disp_ptr;
247 *obj_disp_ptr = obj_disp->object_list_next;
249 if (!object->display_list) {
250 obj2_ptr = (DVBSubObject **) & dvb_sub->object_list; /* FIXME: Evil casting */
253 while (obj2 != object) {
255 obj2_ptr = &obj2->next;
259 *obj2_ptr = obj2->next;
261 g_slice_free (DVBSubObject, obj2);
266 region->display_list = display->region_list_next;
268 g_slice_free (DVBSubObjectDisplay, display);
273 delete_state (DvbSub * dvb_sub)
275 DVBSubRegion *region;
277 while (dvb_sub->region_list) {
278 region = dvb_sub->region_list;
280 dvb_sub->region_list = region->next;
282 delete_region_display_list (dvb_sub, region);
284 g_free (region->pbuf);
286 g_slice_free (DVBSubRegion, region);
289 g_slice_free_chain (DVBSubCLUT, dvb_sub->clut_list, next);
290 dvb_sub->clut_list = NULL;
292 /* Should already be NULL */
293 g_warn_if_fail (dvb_sub->object_list == NULL);
299 int i, r, g, b, a = 0;
301 GST_DEBUG_CATEGORY_INIT (dvbsub_debug, "dvbsub", 0, "dvbsuboverlay parser");
303 /* Initialize the static default_clut structure, from which other clut
304 * structures are initialized from (to start off with default CLUTs
305 * as defined in the specification). */
306 default_clut.id = -1;
308 default_clut.clut4[0] = RGBA_TO_AYUV (0, 0, 0, 0);
309 default_clut.clut4[1] = RGBA_TO_AYUV (255, 255, 255, 255);
310 default_clut.clut4[2] = RGBA_TO_AYUV (0, 0, 0, 255);
311 default_clut.clut4[3] = RGBA_TO_AYUV (127, 127, 127, 255);
313 default_clut.clut16[0] = RGBA_TO_AYUV (0, 0, 0, 0);
314 for (i = 1; i < 16; i++) {
316 r = (i & 1) ? 255 : 0;
317 g = (i & 2) ? 255 : 0;
318 b = (i & 4) ? 255 : 0;
320 r = (i & 1) ? 127 : 0;
321 g = (i & 2) ? 127 : 0;
322 b = (i & 4) ? 127 : 0;
324 default_clut.clut16[i] = RGBA_TO_AYUV (r, g, b, 255);
327 default_clut.clut256[0] = RGBA_TO_AYUV (0, 0, 0, 0);
328 for (i = 1; i < 256; i++) {
330 r = (i & 1) ? 255 : 0;
331 g = (i & 2) ? 255 : 0;
332 b = (i & 4) ? 255 : 0;
337 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
338 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
339 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
343 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
344 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
345 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
349 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
350 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
351 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
355 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
356 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
357 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
362 default_clut.clut256[i] = RGBA_TO_AYUV (r, g, b, a);
367 _dvb_sub_parse_page_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
369 { /* FIXME: Use guint for buf_size here and in many other places? */
370 DVBSubRegionDisplay *display;
371 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
373 const guint8 *buf_end = buf + buf_size;
377 #ifndef GST_DISABLE_GST_DEBUG
378 static int counter = 0; /* FIXME: static counter? I think not.. */
384 dvb_sub->page_time_out = *buf++;
385 page_state = ((*buf++) >> 2) & 3;
387 #ifndef GST_DISABLE_GST_DEBUG
389 static const gchar *page_state_str[4] = {
390 "Normal case", "ACQUISITION POINT", "Mode Change", "RESERVED"
394 GST_DEBUG ("PAGE: %d: page_id = %u, length = %d, page_time_out = %u secs, "
395 "page_state = %s", counter, page_id, buf_size, dvb_sub->page_time_out,
396 page_state_str[page_state]);
400 if (page_state == 2) { /* Mode change */
401 delete_state (dvb_sub);
404 tmp_display_list = dvb_sub->display_list;
405 dvb_sub->display_list = NULL;
406 dvb_sub->display_list_size = 0;
408 while (buf + 5 < buf_end) {
412 display = tmp_display_list;
413 tmp_ptr = &tmp_display_list;
415 while (display && display->region_id != region_id) {
416 tmp_ptr = &display->next;
417 display = display->next;
421 display = g_slice_new0 (DVBSubRegionDisplay);
423 display->region_id = region_id;
425 display->x_pos = GST_READ_UINT16_BE (buf);
427 display->y_pos = GST_READ_UINT16_BE (buf);
430 *tmp_ptr = display->next;
432 display->next = dvb_sub->display_list;
433 dvb_sub->display_list = display;
434 dvb_sub->display_list_size++;
436 GST_LOG ("PAGE %d: REGION information: ID = %u, address = %ux%u",
437 counter, region_id, display->x_pos, display->y_pos);
440 while (tmp_display_list) {
441 display = tmp_display_list;
443 tmp_display_list = display->next;
445 g_slice_free (DVBSubRegionDisplay, display);
450 _dvb_sub_parse_region_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
453 const guint8 *buf_end = buf + buf_size;
456 DVBSubRegion *region;
457 DVBSubObject *object;
458 DVBSubObjectDisplay *object_display;
466 region = get_region (dvb_sub, region_id);
468 if (!region) { /* Create a new region */
469 region = g_slice_new0 (DVBSubRegion);
470 region->id = region_id;
471 region->next = dvb_sub->region_list;
472 dvb_sub->region_list = region;
475 fill = ((*buf++) >> 3) & 1;
477 region->width = GST_READ_UINT16_BE (buf);
479 region->height = GST_READ_UINT16_BE (buf);
482 if (region->width * region->height != region->buf_size) { /* FIXME: Read closer from spec what happens when dimensions change */
484 g_free (region->pbuf);
486 region->buf_size = region->width * region->height;
488 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 */
490 fill = 1; /* FIXME: Validate from spec that fill is forced on (in the following codes context) when dimensions change */
493 region->depth = 1 << (((*buf++) >> 2) & 7);
494 if (region->depth < 2 || region->depth > 8) {
495 GST_WARNING ("region depth %d is invalid", region->depth);
496 region->depth = 4; /* FIXME: Check from spec this is the default? */
499 region->clut = *buf++;
501 if (region->depth == 8)
502 region->bgcolor = *buf++;
506 if (region->depth == 4)
507 region->bgcolor = (((*buf++) >> 4) & 15);
509 region->bgcolor = (((*buf++) >> 2) & 3);
512 GST_DEBUG ("REGION: id = %u, (%ux%u)@%u-bit", region_id, region->width,
513 region->height, region->depth);
516 memset (region->pbuf, region->bgcolor, region->buf_size);
517 GST_DEBUG ("REGION: filling region (%u) with bgcolor = %u", region->id,
521 delete_region_display_list (dvb_sub, region); /* Delete the region display list for current region - FIXME: why? */
523 while (buf + 6 <= buf_end) {
524 object_id = GST_READ_UINT16_BE (buf);
527 object = get_object (dvb_sub, object_id);
530 object = g_slice_new0 (DVBSubObject);
532 object->id = object_id;
534 object->next = dvb_sub->object_list;
535 dvb_sub->object_list = object;
538 object->type = (*buf) >> 6;
540 object_display = g_slice_new0 (DVBSubObjectDisplay);
542 object_display->object_id = object_id;
543 object_display->region_id = region_id;
545 object_display->x_pos = GST_READ_UINT16_BE (buf) & 0xfff;
547 object_display->y_pos = GST_READ_UINT16_BE (buf) & 0xfff;
550 if ((object->type == 1 || object->type == 2) && buf + 2 <= buf_end) {
551 object_display->fgcolor = *buf++;
552 object_display->bgcolor = *buf++;
555 object_display->region_list_next = region->display_list;
556 region->display_list = object_display;
558 object_display->object_list_next = object->display_list;
559 object->display_list = object_display;
561 GST_DEBUG ("REGION DATA: object_id = %u, region_id = %u, pos = %ux%u, "
562 "obj_type = %u", object->id, region->id, object_display->x_pos,
563 object_display->y_pos, object->type);
565 if (object->type == 1 || object->type == 2) {
566 GST_DEBUG ("REGION DATA: fgcolor = %u, bgcolor = %u",
567 object_display->fgcolor, object_display->bgcolor);
573 _dvb_sub_parse_clut_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
576 const guint8 *buf_end = buf + buf_size;
579 int entry_id, depth, full_range;
580 int y, cr, cb, alpha;
582 GST_MEMDUMP ("DVB clut packet", buf, buf_size);
587 clut = get_clut (dvb_sub, clut_id);
590 clut = g_slice_new (DVBSubCLUT); /* FIXME-MEMORY-LEAK: This seems to leak per valgrind */
592 memcpy (clut, &default_clut, sizeof (DVBSubCLUT));
596 clut->next = dvb_sub->clut_list;
597 dvb_sub->clut_list = clut;
600 while (buf + 4 < buf_end) {
603 depth = (*buf) & 0xe0;
606 GST_WARNING ("Invalid clut depth 0x%x!", *buf);
610 full_range = (*buf++) & 1;
619 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
620 cb = (buf[1] << 2) & 0xf0;
621 alpha = (buf[1] << 6) & 0xc0;
629 GST_DEBUG ("CLUT DEFINITION: clut %d := (%d,%d,%d,%d)", entry_id, y, cb, cr,
633 clut->clut4[entry_id] = AYUV (y, cb, cr, 255 - alpha);
635 clut->clut16[entry_id] = AYUV (y, cb, cr, 255 - alpha);
637 clut->clut256[entry_id] = AYUV (y, cb, cr, 255 - alpha);
641 // FFMPEG-FIXME: The same code in ffmpeg is much more complex, it could use the same
642 // FFMPEG-FIXME: refactoring as done here
644 _dvb_sub_read_2bit_string (guint8 * destbuf, gint dbuf_len,
645 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
647 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
648 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
650 gboolean stop_parsing = FALSE;
652 guint32 pixels_read = 0;
654 static gboolean warning_shown = FALSE;
655 if (!warning_shown) {
656 g_warning ("Parsing 2bit color DVB sub-picture. This is not tested at all. "
657 "If you see this message, please provide the developers with sample "
658 "media with these subtitles, if possible.");
659 warning_shown = TRUE;
662 GST_TRACE ("dbuf_len = %d", dbuf_len);
664 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
665 guint run_length = 0, clut_index = 0;
666 gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
668 if (bits) { /* 2-bit_pixel-code */
671 } else { /* 2-bit_zero */
672 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
673 if (bits == 1) { /* switch_1 == '1' */
674 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 3);
676 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
677 } else { /* switch_1 == '0' */
678 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
679 if (bits == 1) { /* switch_2 == '1' */
680 run_length = 1; /* 1x pseudo-colour '00' */
681 } else { /* switch_2 == '0' */
682 gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
683 switch (bits) { /* switch_3 */
684 case 0x0: /* end of 2-bit/pixel_code_string */
687 case 0x1: /* two pixels shall be set to pseudo colour (entry) '00' */
690 case 0x2: /* the following 6 bits contain run length coded pixel data */
691 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 4);
693 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
695 case 0x3: /* the following 10 bits contain run length coded pixel data */
696 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 8);
698 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
705 /* If run_length is zero, continue. Only case happening is when
706 * stop_parsing is TRUE too, so next cycle shouldn't run */
710 /* Trim the run_length to not go beyond the line end and consume
711 * it from remaining length of dest line */
712 run_length = MIN (run_length, dbuf_len);
713 dbuf_len -= run_length;
715 /* Make clut_index refer to the index into the desired bit depths
716 * CLUT definition table */
718 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
720 /* Now we can simply memset run_length count of destination bytes
721 * to clut_index, but only if not non_modifying */
722 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer, "
723 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
725 if (!(non_mod == 1 && bits == 1))
726 memset (destbuf, clut_index, run_length);
728 destbuf += run_length;
729 pixels_read += run_length;
732 // FIXME: Test skip_to_byte instead of adding 7 bits, once everything else is working good
733 //gst_bit_reader_skip_to_byte (&gb);
734 *srcbuf += (gst_bit_reader_get_pos (&gb) + 7) >> 3;
736 GST_TRACE ("PIXEL: returning, read %u pixels", pixels_read);
737 // FIXME: Shouldn't need this variable if tracking things in the loop better
741 // FFMPEG-FIXME: The same code in ffmpeg is much more complex, it could use the same
742 // FFMPEG-FIXME: refactoring as done here, explained in commit 895296c3
744 _dvb_sub_read_4bit_string (guint8 * destbuf, gint dbuf_len,
745 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
747 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
748 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
749 gboolean stop_parsing = FALSE;
751 guint32 pixels_read = 0;
753 GST_TRACE ("RUNLEN: srcbuf position %p, buf_size = %d; destination buffer "
754 "size is %d @ %p", *srcbuf, buf_size, dbuf_len, destbuf);
756 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
757 guint run_length = 0, clut_index = 0;
758 gst_bit_reader_get_bits_uint32 (&gb, &bits, 4);
764 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
765 if (bits == 0) { /* switch_1 == '0' */
766 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 3);
772 } else { /* switch_1 == '1' */
773 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
774 if (bits == 0) { /* switch_2 == '0' */
775 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 2);
777 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
778 } else { /* switch_2 == '1' */
779 gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
781 case 0x0: /* switch_3 == '00' */
782 run_length = 1; /* 1 pixel of pseudo-color 0 */
784 case 0x1: /* switch_3 == '01' */
785 run_length = 2; /* 2 pixels of pseudo-color 0 */
787 case 0x2: /* switch_3 == '10' */
788 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 4);
790 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
792 case 0x3: /* switch_3 == '11' */
793 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 8);
795 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
802 /* If run_length is zero, continue. Only case happening is when
803 * stop_parsing is TRUE too, so next cycle shouldn't run */
807 /* Trim the run_length to not go beyond the line end and consume
808 * it from remaining length of dest line */
809 run_length = MIN (run_length, dbuf_len);
810 dbuf_len -= run_length;
812 /* Make clut_index refer to the index into the desired bit depths
813 * CLUT definition table */
815 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
817 /* Now we can simply memset run_length count of destination bytes
818 * to clut_index, but only if not non_modifying */
819 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer; "
820 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
822 if (!(non_mod == 1 && bits == 1))
823 memset (destbuf, clut_index, run_length);
825 destbuf += run_length;
826 pixels_read += run_length;
829 // FIXME: Test skip_to_byte instead of adding 7 bits, once everything else is working good
830 //gst_bit_reader_skip_to_byte (&gb);
831 *srcbuf += (gst_bit_reader_get_pos (&gb) + 7) >> 3;
833 GST_LOG ("Returning with %u pixels read", pixels_read);
835 // FIXME: Shouldn't need this variable if tracking things in the loop better
840 _dvb_sub_read_8bit_string (guint8 * destbuf, gint dbuf_len,
841 const guint8 ** srcbuf, gint buf_size, guint8 non_mod, guint8 * map_table)
843 GstBitReader gb = GST_BIT_READER_INIT (*srcbuf, buf_size);
844 /* FIXME: Handle FALSE returns from gst_bit_reader_get_* calls? */
846 gboolean stop_parsing = FALSE;
848 guint32 pixels_read = 0;
850 static gboolean warning_shown = FALSE;
851 if (!warning_shown) {
853 ("Parsing 8bit color DVB sub-picture. This is not tested at all. If you see this message, "
854 "please provide the developers with sample media with these subtitles, if possible.");
855 warning_shown = TRUE;
858 GST_LOG ("dbuf_len = %d", dbuf_len);
860 /* FFMPEG-FIXME: ffmpeg uses a manual byte walking algorithm, which might be more performant,
861 * FFMPEG-FIXME: but it does almost absolutely no buffer length checking, so could walk over
862 * FFMPEG-FIXME: memory boundaries. While we don't check gst_bit_reader_get_bits_uint32
863 * FFMPEG-FIXME: return values either and therefore might get some pixels corrupted, we at
864 * FFMPEG-FIXME: lest have no chance of reading memory we don't own and visual corruption
865 * FFMPEG-FIXME: is guaranteed anyway when not all bytes are present */
866 /* Rephrased - it's better to work with bytes with default value '0' instead of reading from memory we don't own. */
867 while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
868 guint run_length = 0, clut_index = 0;
869 gst_bit_reader_get_bits_uint32 (&gb, &bits, 8);
871 if (bits) { /* 8-bit_pixel-code */
874 } else { /* 8-bit_zero */
875 gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
876 if (bits == 0) { /* switch_1 == '0' */
877 /* run_length_1-127 for pseudo-colour _entry) '0x00' */
878 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 7);
879 if (run_length == 0) { /* end_of_string_signal */
882 } else { /* switch_1 == '1' */
883 /* run_length_3-127 */
884 gst_bit_reader_get_bits_uint32 (&gb, &run_length, 7);
885 gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 8);
887 if (run_length < 3) {
888 GST_WARNING ("runlength value was %u, but the spec requires it "
889 "must be >=3", run_length);
894 /* If run_length is zero, continue. Only case happening is when
895 * stop_parsing is TRUE too, so next cycle shouldn't run */
899 /* Trim the run_length to not go beyond the line end and consume
900 * it from remaining length of dest line */
901 run_length = MIN (run_length, dbuf_len);
902 dbuf_len -= run_length;
904 /* Make clut_index refer to the index into the desired bit depths
905 * CLUT definition table */
907 clut_index = map_table[clut_index]; /* now clut_index signifies the index into map_table dest */
909 /* Now we can simply memset run_length count of destination bytes
910 * to clut_index, but only if not non_modifying */
911 GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer; "
912 "dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
914 if (!(non_mod == 1 && bits == 1))
915 memset (destbuf, clut_index, run_length);
917 destbuf += run_length;
918 pixels_read += run_length;
921 GST_LOG ("Returning with %u pixels read", pixels_read);
923 // FIXME: Shouldn't need this variable if tracking things in the loop better
928 _dvb_sub_parse_pixel_data_block (DvbSub * dvb_sub,
929 DVBSubObjectDisplay * display, const guint8 * buf, gint buf_size,
930 DvbSubPixelDataSubBlockFieldType top_bottom, guint8 non_mod)
932 DVBSubRegion *region = get_region (dvb_sub, display->region_id);
933 const guint8 *buf_end = buf + buf_size;
937 gboolean dest_buf_filled = FALSE;
939 guint8 map2to4[] = { 0x0, 0x7, 0x8, 0xf };
940 guint8 map2to8[] = { 0x00, 0x77, 0x88, 0xff };
941 guint8 map4to8[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
942 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
946 GST_LOG ("DVB pixel block size %d, %s field:", buf_size,
947 top_bottom ? "bottom" : "top");
949 GST_MEMDUMP ("packet", buf, buf_size);
951 if (region == NULL) {
952 GST_LOG ("Region is NULL, returning");
958 x_pos = display->x_pos;
959 y_pos = display->y_pos;
961 if ((y_pos & 1) != top_bottom)
964 while (buf < buf_end) {
965 GST_LOG ("Iteration start, %u bytes missing from end; buf = %p, "
966 "buf_end = %p; Region is number %u, with a dimension of %dx%d; "
967 "We are at position %dx%d", (guint) (buf_end - buf), buf, buf_end,
968 region->id, region->width, region->height, x_pos, y_pos);
970 // FFMPEG-FIXME: ffmpeg doesn't check for equality and so can overflow destination buffer later on with bad input data
971 // 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
972 // FIXME: Removed x_pos checking here, because we don't want to turn dest_buf_filled to TRUE permanently in that case
973 // FIXME: We assume that region->width - x_pos as dbuf_len to read_nbit_string will take care of that case nicely;
974 // FIXME: That is, that read_nbit_string never scribbles anything if dbuf_len passed to it is zero due to this.
975 if (y_pos >= region->height) {
976 dest_buf_filled = TRUE;
981 if (dest_buf_filled) {
982 /* FIXME: Be more verbose */
983 GST_WARNING ("Invalid object location for data_type 0x%x!",
985 GST_MEMDUMP ("Remaining data after invalid object location:", buf,
986 (guint) (buf_end - buf));
990 if (region->depth == 8)
992 else if (region->depth == 4)
997 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
998 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
1000 _dvb_sub_read_2bit_string (pbuf + (y_pos * region->width) + x_pos,
1001 region->width - x_pos, &buf, buf_end - buf, non_mod, map_table);
1004 if (dest_buf_filled) {
1005 /* FIXME: Be more verbose */
1006 GST_WARNING ("Invalid object location for data_type 0x%x!",
1008 GST_MEMDUMP ("Remaining data after invalid object location:", buf,
1010 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)
1013 if (region->depth < 4) {
1014 GST_WARNING ("4-bit pixel string in %d-bit region!", region->depth);
1018 if (region->depth == 8)
1019 map_table = map4to8;
1023 GST_LOG ("READ_4BIT_STRING: String data into position %dx%d; "
1024 "buf before is %p", x_pos, y_pos, buf);
1025 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
1026 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
1028 _dvb_sub_read_4bit_string (pbuf + (y_pos * region->width) + x_pos,
1029 region->width - x_pos, &buf, buf_end - buf, non_mod, map_table);
1030 GST_DEBUG ("READ_4BIT_STRING finished: buf pointer now %p", buf);
1033 if (dest_buf_filled) {
1034 /* FIXME: Be more verbose */
1035 GST_WARNING ("Invalid object location for data_type 0x%x!",
1037 GST_MEMDUMP ("Remaining data after invalid object location:",
1038 buf, (guint) (buf_end - buf));
1042 if (region->depth < 8) {
1043 GST_WARNING ("8-bit pixel string in %d-bit region!", region->depth);
1046 // FFMPEG-FIXME: ffmpeg code passes buf_size instead of buf_end - buf, and could
1047 // FFMPEG-FIXME: therefore potentially walk over the memory area we own
1049 _dvb_sub_read_8bit_string (pbuf + (y_pos * region->width) + x_pos,
1050 region->width - x_pos, &buf, buf_end - buf, non_mod, NULL);
1054 GST_DEBUG ("handling map2to4 table data");
1055 /* FIXME: I don't see any guards about buffer size here - buf++ happens with the switch, but
1056 * FIXME: buffer is walked without length checks? Same deal in other map table cases */
1057 map2to4[0] = (*buf) >> 4;
1058 map2to4[1] = (*buf++) & 0xf;
1059 map2to4[2] = (*buf) >> 4;
1060 map2to4[3] = (*buf++) & 0xf;
1063 GST_DEBUG ("handling map2to8 table data");
1064 for (i = 0; i < 4; i++)
1065 map2to8[i] = *buf++;
1068 GST_DEBUG ("handling map4to8 table data");
1069 for (i = 0; i < 16; i++)
1070 map4to8[i] = *buf++;
1073 GST_DEBUG ("end of object line code encountered");
1074 x_pos = display->x_pos;
1078 /* FIXME: Do we consume word align stuffing byte that could follow top/bottom data? */
1079 GST_WARNING ("Unknown/unsupported pixel block 0x%x", *(buf - 1));
1085 _dvb_sub_parse_object_segment (DvbSub * dvb_sub, guint16 page_id, guint8 * buf,
1088 const guint8 *buf_end = buf + buf_size;
1090 DVBSubObject *object;
1092 guint8 coding_method, non_modifying_color;
1094 object_id = GST_READ_UINT16_BE (buf);
1097 object = get_object (dvb_sub, object_id);
1099 GST_DEBUG ("OBJECT: a new object segment has occurred for object_id = %u",
1103 GST_WARNING ("Nothing known about object with ID %u yet, bailing out",
1108 coding_method = ((*buf) >> 2) & 3;
1109 non_modifying_color = ((*buf++) >> 1) & 1;
1111 if (coding_method == 0) {
1112 const guint8 *block;
1113 DVBSubObjectDisplay *display;
1114 guint16 top_field_len, bottom_field_len;
1116 top_field_len = GST_READ_UINT16_BE (buf);
1118 bottom_field_len = GST_READ_UINT16_BE (buf);
1121 if (buf + top_field_len + bottom_field_len > buf_end) {
1122 GST_WARNING ("Field data size too large");
1126 /* FIXME: Potential optimization opportunity here - parse the object pixmap only once, and copy it to all the
1127 * FIXME: regions that need it. One object being in multiple regions is a rare occurrence in real life, however */
1128 for (display = object->display_list; display;
1129 display = display->object_list_next) {
1132 GST_DEBUG ("OBJECT: parsing top and bottom part of object id %d; "
1133 "top_field_len = %u, bottom_field_len = %u",
1134 display->object_id, top_field_len, bottom_field_len);
1136 _dvb_sub_parse_pixel_data_block (dvb_sub, display, block, top_field_len,
1137 TOP_FIELD, non_modifying_color);
1139 if (bottom_field_len > 0)
1140 block = buf + top_field_len;
1142 bottom_field_len = top_field_len;
1144 _dvb_sub_parse_pixel_data_block (dvb_sub, display, block,
1145 bottom_field_len, BOTTOM_FIELD, non_modifying_color);
1148 } else if (coding_method == 1) {
1149 GST_FIXME ("'a string of characters' coding method not supported yet!");
1151 GST_WARNING ("Unknown object coding 0x%x", coding_method);
1156 _dvb_sub_parse_display_definition_segment (DvbSub * dvb_sub, guint8 * buf,
1159 int dds_version, info_byte;
1165 dds_version = info_byte >> 4;
1167 if (dvb_sub->display_def.version == dds_version)
1168 return 0; /* already have this display definition version */
1170 dvb_sub->display_def.version = dds_version;
1171 dvb_sub->display_def.display_width = GST_READ_UINT16_BE (buf) + 1;
1173 dvb_sub->display_def.display_height = GST_READ_UINT16_BE (buf) + 1;
1176 dvb_sub->display_def.window_flag = info_byte & 1 << 3;
1178 if (buf_size >= 13 && dvb_sub->display_def.window_flag) {
1179 dvb_sub->display_def.window_x = GST_READ_UINT16_BE (buf);
1181 dvb_sub->display_def.window_y = GST_READ_UINT16_BE (buf);
1183 dvb_sub->display_def.window_width =
1184 GST_READ_UINT16_BE (buf) - dvb_sub->display_def.window_x + 1;
1186 dvb_sub->display_def.window_height =
1187 GST_READ_UINT16_BE (buf) - dvb_sub->display_def.window_y + 1;
1195 _dvb_sub_parse_end_of_display_set (DvbSub * dvb_sub, guint16 page_id,
1196 guint8 * buf, gint buf_size, guint64 pts)
1198 DVBSubRegionDisplay *display;
1201 guint32 *clut_table;
1204 static unsigned counter = 0; /* DEBUG use only *//* FIXME: get rid */
1206 GST_DEBUG ("DISPLAY SET END: page_id = %u, length = %d", page_id, buf_size);
1208 sub = g_slice_new0 (DVBSubtitles);
1210 #if 0 /* FIXME: PTS stuff not figured out yet */
1211 sub->start_display_time = 0;
1212 sub->end_display_time = priv->page_time_out * 1000;
1213 sub->format = 0; /* 0 = graphics */
1216 /* N.B. g_new0() will return NULL if num_rects is 0 */
1217 sub->num_rects = dvb_sub->display_list_size;
1218 sub->rects = g_new0 (DVBSubtitleRect, sub->num_rects);
1222 /* copy subtitle display and window information */
1223 sub->display_def = dvb_sub->display_def;
1225 for (display = dvb_sub->display_list; display; display = display->next) {
1226 DVBSubtitleRect *rect;
1227 DVBSubRegion *region;
1229 region = get_region (dvb_sub, display->region_id);
1234 rect = &sub->rects[i];
1235 rect->x = display->x_pos;
1236 rect->y = display->y_pos;
1237 rect->w = region->width;
1238 rect->h = region->height;
1239 #if 0 /* FIXME: Don't think we need to save the number of colors in the palette when we are saving as RGBA? */
1240 rect->nb_colors = 16;
1242 #if 0 /* FIXME: Needed to be specified once we support strings of characters based subtitles */
1243 rect->type = SUBTITLE_BITMAP;
1245 rect->pict.rowstride = region->width;
1246 rect->pict.palette_bits_count = region->depth;
1248 clut = get_clut (dvb_sub, region->clut);
1251 clut = &default_clut;
1253 switch (region->depth) {
1255 clut_table = clut->clut4;
1258 clut_table = clut->clut256;
1262 clut_table = clut->clut16;
1266 /* FIXME: Tweak this to be saved in a format most suitable for Qt and GStreamer instead.
1267 * Currently kept in AVPicture for quick save_display_set testing */
1268 rect->pict.palette = g_malloc ((1 << region->depth) * sizeof (guint32)); /* FIXME: Can we use GSlice here? */
1269 memcpy (rect->pict.palette, clut_table,
1270 (1 << region->depth) * sizeof (guint32));
1272 GST_MEMDUMP ("rect->pict.data.palette content",
1273 (guint8 *) rect->pict.palette, (1 << region->depth) * sizeof (guint32));
1275 rect->pict.data = g_malloc (region->buf_size); /* FIXME: Can we use GSlice here? */
1276 memcpy (rect->pict.data, region->pbuf, region->buf_size);
1279 GST_DEBUG ("DISPLAY: an object rect created: number %u, iteration %u, "
1280 "pos: %d:%d, size: %dx%d", counter, i, rect->x, rect->y, rect->w,
1283 GST_MEMDUMP ("rect->pict.data content", rect->pict.data, region->buf_size);
1289 sub->page_time_out = dvb_sub->page_time_out;
1292 if (dvb_sub->callbacks.new_data) {
1293 dvb_sub->callbacks.new_data (dvb_sub, sub, dvb_sub->user_data);
1295 /* No-one responsible to clean up memory, so do it ourselves */
1296 /* FIXME: Just don't bother with all this palette image creation in the first place then... */
1297 dvb_subtitles_free (sub);
1300 return 1; /* FIXME: The caller of this function is probably supposed to do something with the return value */
1304 dvb_subtitles_free (DVBSubtitles * sub)
1311 /* Now free up all the temporary memory we allocated */
1312 for (i = 0; i < sub->num_rects; ++i) {
1313 g_free (sub->rects[i].pict.palette);
1314 g_free (sub->rects[i].pict.data);
1316 g_free (sub->rects);
1317 g_slice_free (DVBSubtitles, sub);
1323 static gsize inited = 0;
1326 if (g_once_init_enter (&inited)) {
1328 g_once_init_leave (&inited, TRUE);
1331 sub = g_slice_new0 (DvbSub);
1333 /* TODO: Add initialization code here */
1334 /* FIXME: Do we have a reason to initiate the members to zero, or are we guaranteed that anyway? */
1335 sub->region_list = NULL;
1336 sub->object_list = NULL;
1337 sub->page_time_out = 0; /* FIXME: Maybe 255 instead? */
1338 sub->pes_buffer = g_string_new (NULL);
1340 /* display/window information */
1341 sub->display_def.version = -1;
1342 sub->display_def.window_flag = 0;
1343 sub->display_def.display_width = 720;
1344 sub->display_def.display_height = 576;
1350 dvb_sub_free (DvbSub * sub)
1352 /* TODO: Add deinitalization code here */
1353 /* FIXME: Clear up region_list contents */
1355 g_string_free (sub->pes_buffer, TRUE);
1356 g_slice_free (DvbSub, sub);
1359 #define DVB_SUB_SEGMENT_PAGE_COMPOSITION 0x10
1360 #define DVB_SUB_SEGMENT_REGION_COMPOSITION 0x11
1361 #define DVB_SUB_SEGMENT_CLUT_DEFINITION 0x12
1362 #define DVB_SUB_SEGMENT_OBJECT_DATA 0x13
1363 #define DVB_SUB_SEGMENT_DISPLAY_DEFINITION 0x14
1364 #define DVB_SUB_SEGMENT_END_OF_DISPLAY_SET 0x80
1365 #define DVB_SUB_SEGMENT_STUFFING 0xFF
1367 #define DVB_SUB_SYNC_BYTE 0x0f
1369 * dvb_sub_feed_with_pts:
1370 * @dvb_sub: a #DvbSub
1371 * @pts: The PTS of the data
1372 * @data: The data to feed to the parser
1373 * @len: Length of the data
1375 * Feeds the DvbSub parser with new binary data to parse,
1376 * with an associated PTS value. E.g, data left after PES
1377 * packet header has been already parsed, which contains
1378 * the PTS information).
1380 * Return value: -1 if data was unhandled (e.g, not a subtitle packet),
1381 * -2 if data parsing was unsuccesful (e.g, length was invalid),
1382 * 0 or positive if data was handled. If positive, then amount of data consumed on success. FIXME: List the positive return values.
1385 dvb_sub_feed_with_pts (DvbSub * dvb_sub, guint64 pts, guint8 * data, gint len)
1387 unsigned int pos = 0;
1388 guint8 segment_type;
1389 guint16 segment_len;
1392 GST_DEBUG ("pts=%" G_GUINT64_FORMAT " and length %d", pts, len);
1394 g_return_val_if_fail (data != NULL, -1);
1396 if (len <= 3) { /* len(0x20 0x00 end_of_PES_data_field_marker) */
1397 GST_WARNING ("Data length too short");
1401 if (data[pos++] != 0x20) {
1402 GST_WARNING ("Tried to handle a PES packet private data that isn't a "
1403 "subtitle packet (does not start with 0x20)");
1407 if (data[pos++] != 0x00) {
1408 GST_WARNING ("'Subtitle stream in this PES packet' was not 0x00, so this "
1409 "is in theory not a DVB subtitle stream (but some other subtitle "
1410 "standard?); bailing out");
1414 while (data[pos++] == DVB_SUB_SYNC_BYTE) {
1415 if ((len - pos) < (2 * 2 + 1)) {
1416 GST_WARNING ("Data after SYNC BYTE too short, less than needed to "
1417 "even get to segment_length");
1420 segment_type = data[pos++];
1421 GST_DEBUG ("=== Segment type is 0x%x", segment_type);
1422 page_id = (data[pos] << 8) | data[pos + 1];
1423 GST_DEBUG ("page_id is 0x%x", page_id);
1425 segment_len = (data[pos] << 8) | data[pos + 1];
1426 GST_DEBUG ("segment_length is %d (0x%x 0x%x)", segment_len, data[pos],
1429 if ((len - pos) < segment_len) {
1430 GST_WARNING ("segment_length was told to be %u, but we only have "
1431 "%d bytes left", segment_len, len - pos);
1434 // TODO: Parse the segment per type (this is probably a leftover TODO that is now done?)
1435 /* FIXME: Handle differing PTS values - all segments of a given display set must be with the same PTS,
1436 * FIXME: but we let it slip and just take it for granted in end_of_display_set */
1437 switch (segment_type) {
1438 case DVB_SUB_SEGMENT_PAGE_COMPOSITION:
1439 GST_DEBUG ("Page composition segment at buffer pos %u", pos);
1440 _dvb_sub_parse_page_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1442 case DVB_SUB_SEGMENT_REGION_COMPOSITION:
1443 GST_DEBUG ("Region composition segment at buffer pos %u", pos);
1444 _dvb_sub_parse_region_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1446 case DVB_SUB_SEGMENT_CLUT_DEFINITION:
1447 GST_DEBUG ("CLUT definition segment at buffer pos %u", pos);
1448 _dvb_sub_parse_clut_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1450 case DVB_SUB_SEGMENT_OBJECT_DATA:
1451 GST_DEBUG ("Object data segment at buffer pos %u", pos);
1452 _dvb_sub_parse_object_segment (dvb_sub, page_id, data + pos, segment_len); /* FIXME: Not sure about args */
1454 case DVB_SUB_SEGMENT_DISPLAY_DEFINITION:
1455 GST_DEBUG ("display definition segment at buffer pos %u", pos);
1456 _dvb_sub_parse_display_definition_segment (dvb_sub, data + pos,
1459 case DVB_SUB_SEGMENT_END_OF_DISPLAY_SET:
1460 GST_DEBUG ("End of display set at buffer pos %u", pos);
1461 _dvb_sub_parse_end_of_display_set (dvb_sub, page_id, data + pos, segment_len, pts); /* FIXME: Not sure about args */
1464 GST_FIXME ("Unhandled segment type 0x%x", segment_type);
1471 GST_WARNING ("Data ended without a PES data end marker");
1476 GST_LOG ("Processed %d bytes out of %d", pos, len);
1481 * dvb_sub_set_callbacks:
1482 * @dvb_sub: a #DvbSub
1483 * @callbacks: the callbacks to install
1484 * @user_data: a user_data argument for the callback
1486 * Set callback which will be executed when new subpictures are available.
1489 dvb_sub_set_callbacks (DvbSub * dvb_sub, DvbSubCallbacks * callbacks,
1492 g_return_if_fail (dvb_sub != NULL);
1493 g_return_if_fail (callbacks != NULL);
1495 dvb_sub->callbacks = *callbacks;
1496 dvb_sub->user_data = user_data;