-/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
-/*
- * libdvbsub - DVB subtitle decoding
+/* dvb-sub.c - DVB subtitle decoding
* Copyright (C) Mart Raudsepp 2009 <mart.raudsepp@artecdesign.ee>
+ * Copyright (C) 2010 ONELAN Ltd.
*
* Heavily uses code algorithms ported from ffmpeg's libavcodec/dvbsubdec.c,
* especially the segment parsers. The original license applies to this
#include <string.h> /* memset */
#include <gst/gstutils.h> /* GST_READ_UINT16_BE */
#include <gst/base/gstbitreader.h> /* GstBitReader */
-#include "ffmpeg-colorspace.h" /* YUV_TO_RGB1_CCIR */ /* FIXME: Just give YUV data to gstreamer then? */
#include "dvb-sub.h"
* and signalling the API user for new bitmaps to show on screen.
*/
-#define MAX_NEG_CROP 1024
-static guint8 ff_cropTbl[256 + 2 * MAX_NEG_CROP] = { 0, };
-
-#define cm (ff_cropTbl + MAX_NEG_CROP)
+#define AYUV(y,u,v,a) (((a) << 24) | ((y) << 16) | ((u) << 8) | (v))
+#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)))
-/* FIXME: This is really ARGB... We might need this configurable for performant
- * FIXME: use in GStreamer as well if that likes RGBA more (Qt prefers ARGB) */
-#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
typedef struct DVBSubCLUT
{
BOTTOM_FIELD = 1
} DvbSubPixelDataSubBlockFieldType;
+static inline gint
+rgb_to_y (gint r, gint g, gint b)
+{
+ gint ret;
+
+ ret = (gint) (((19595 * r) >> 16) + ((38470 * g) >> 16) + ((7471 * b) >> 16));
+ ret = CLAMP (ret, 0, 255);
+ return ret;
+}
+
+static inline gint
+rgb_to_u (gint r, gint g, gint b)
+{
+ gint ret;
+
+ ret =
+ (gint) (-((11059 * r) >> 16) - ((21709 * g) >> 16) + ((32768 * b) >> 16) +
+ 128);
+ ret = CLAMP (ret, 0, 255);
+ return ret;
+}
+
+static inline gint
+rgb_to_v (gint r, gint g, gint b)
+{
+ gint ret;
+
+ ret =
+ (gint) (((32768 * r) >> 16) - ((27439 * g) >> 16) - ((5329 * b) >> 16) +
+ 128);
+ ret = CLAMP (ret, 0, 255);
+ return ret;
+}
+
static DVBSubObject *
get_object (DvbSub * dvb_sub, guint16 object_id)
{
g_warn_if_fail (dvb_sub->object_list == NULL);
}
-/* init static data necessary for ffmpeg-colorspace conversion */
-static void
-dsputil_static_init (void)
-{
- int i;
-
- for (i = 0; i < 256; i++)
- ff_cropTbl[i + MAX_NEG_CROP] = i;
- for (i = 0; i < MAX_NEG_CROP; i++) {
- ff_cropTbl[i] = 0;
- ff_cropTbl[i + MAX_NEG_CROP + 256] = 255;
- }
-}
-
static void
dvb_sub_init (void)
{
GST_DEBUG_CATEGORY_INIT (dvbsub_debug, "dvbsub", 0, "dvbsuboverlay parser");
- dsputil_static_init (); /* Initializes ff_cropTbl table, used in YUV_TO_RGB conversion */
-
/* Initialize the static default_clut structure, from which other clut
* structures are initialized from (to start off with default CLUTs
* as defined in the specification). */
default_clut.id = -1;
- default_clut.clut4[0] = RGBA (0, 0, 0, 0);
- default_clut.clut4[1] = RGBA (255, 255, 255, 255);
- default_clut.clut4[2] = RGBA (0, 0, 0, 255);
- default_clut.clut4[3] = RGBA (127, 127, 127, 255);
+ default_clut.clut4[0] = RGBA_TO_AYUV (0, 0, 0, 0);
+ default_clut.clut4[1] = RGBA_TO_AYUV (255, 255, 255, 255);
+ default_clut.clut4[2] = RGBA_TO_AYUV (0, 0, 0, 255);
+ default_clut.clut4[3] = RGBA_TO_AYUV (127, 127, 127, 255);
- default_clut.clut16[0] = RGBA (0, 0, 0, 0);
+ default_clut.clut16[0] = RGBA_TO_AYUV (0, 0, 0, 0);
for (i = 1; i < 16; i++) {
if (i < 8) {
r = (i & 1) ? 255 : 0;
g = (i & 2) ? 127 : 0;
b = (i & 4) ? 127 : 0;
}
- default_clut.clut16[i] = RGBA (r, g, b, 255);
+ default_clut.clut16[i] = RGBA_TO_AYUV (r, g, b, 255);
}
- default_clut.clut256[0] = RGBA (0, 0, 0, 0);
+ default_clut.clut256[0] = RGBA_TO_AYUV (0, 0, 0, 0);
for (i = 1; i < 256; i++) {
if (i < 8) {
r = (i & 1) ? 255 : 0;
break;
}
}
- default_clut.clut256[i] = RGBA (r, g, b, a);
+ default_clut.clut256[i] = RGBA_TO_AYUV (r, g, b, a);
}
}
guint8 region_id;
guint8 page_state;
-#ifndef GST_DISABLE_GST_DEBUG
- static int counter = 0; /* FIXME: static counter? I think not.. */
-#endif
-
if (buf_size < 1)
return;
"Normal case", "ACQUISITION POINT", "Mode Change", "RESERVED"
};
- ++counter;
- GST_DEBUG ("PAGE: %d: page_id = %u, length = %d, page_time_out = %u secs, "
- "page_state = %s", counter, page_id, buf_size, dvb_sub->page_time_out,
+ GST_DEBUG ("PAGE: page_id = %u, length = %d, page_time_out = %u secs, "
+ "page_state = %s", page_id, buf_size, dvb_sub->page_time_out,
page_state_str[page_state]);
}
#endif
dvb_sub->display_list = display;
dvb_sub->display_list_size++;
- GST_LOG ("PAGE %d: REGION information: ID = %u, address = %ux%u",
- counter, region_id, display->x_pos, display->y_pos);
+ GST_LOG ("PAGE: REGION information: ID = %u, address = %ux%u", region_id,
+ display->x_pos, display->y_pos);
}
while (tmp_display_list) {
DVBSubCLUT *clut;
int entry_id, depth, full_range;
int y, cr, cb, alpha;
- int r, g, b, r_add, g_add, b_add;
GST_MEMDUMP ("DVB clut packet", buf, buf_size);
clut = get_clut (dvb_sub, clut_id);
if (!clut) {
- clut = g_slice_new (DVBSubCLUT); /* FIXME-MEMORY-LEAK: This seems to leak per valgrind */
+ clut = g_slice_new (DVBSubCLUT);
memcpy (clut, &default_clut, sizeof (DVBSubCLUT));
if (y == 0)
alpha = 0xff;
- YUV_TO_RGB1_CCIR (cb, cr);
- YUV_TO_RGB2_CCIR (r, g, b, y);
-
- GST_DEBUG ("CLUT DEFINITION: clut %d := (%d,%d,%d,%d)", entry_id, r, g, b,
+ GST_DEBUG ("CLUT DEFINITION: clut %d := (%d,%d,%d,%d)", entry_id, y, cb, cr,
alpha);
if (depth & 0x80)
- clut->clut4[entry_id] = RGBA (r, g, b, 255 - alpha);
+ clut->clut4[entry_id] = AYUV (y, cb, cr, 255 - alpha);
if (depth & 0x40)
- clut->clut16[entry_id] = RGBA (r, g, b, 255 - alpha);
+ clut->clut16[entry_id] = AYUV (y, cb, cr, 255 - alpha);
if (depth & 0x20)
- clut->clut256[entry_id] = RGBA (r, g, b, 255 - alpha);
+ clut->clut256[entry_id] = AYUV (y, cb, cr, 255 - alpha);
}
}
while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
guint run_length = 0, clut_index = 0;
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
+
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
if (bits) { /* 2-bit_pixel-code */
run_length = 1;
clut_index = bits;
} else { /* 2-bit_zero */
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
if (bits == 1) { /* switch_1 == '1' */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 3);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 3);
run_length += 3;
- gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
+ clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
} else { /* switch_1 == '0' */
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
if (bits == 1) { /* switch_2 == '1' */
run_length = 1; /* 1x pseudo-colour '00' */
} else { /* switch_2 == '0' */
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
switch (bits) { /* switch_3 */
case 0x0: /* end of 2-bit/pixel_code_string */
stop_parsing = TRUE;
run_length = 2;
break;
case 0x2: /* the following 6 bits contain run length coded pixel data */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 4);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
run_length += 12;
- gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
+ clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
break;
case 0x3: /* the following 10 bits contain run length coded pixel data */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 8);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 8);
run_length += 29;
- gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 2);
+ clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
break;
}
}
GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer, "
"dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
- if (!(non_mod == 1 && bits == 1))
+ if (!(non_mod == 1 && clut_index == 1))
memset (destbuf, clut_index, run_length);
destbuf += run_length;
while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
guint run_length = 0, clut_index = 0;
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 4);
+
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
if (bits) {
run_length = 1;
clut_index = bits;
} else {
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
if (bits == 0) { /* switch_1 == '0' */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 3);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 3);
if (!run_length) {
stop_parsing = TRUE;
} else {
run_length += 2;
}
} else { /* switch_1 == '1' */
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
if (bits == 0) { /* switch_2 == '0' */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 2);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
run_length += 4;
- gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
+ clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
} else { /* switch_2 == '1' */
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 2);
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 2);
switch (bits) {
case 0x0: /* switch_3 == '00' */
run_length = 1; /* 1 pixel of pseudo-color 0 */
run_length = 2; /* 2 pixels of pseudo-color 0 */
break;
case 0x2: /* switch_3 == '10' */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 4);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
run_length += 9;
- gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
+ clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
break;
case 0x3: /* switch_3 == '11' */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 8);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 8);
run_length += 25;
- gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 4);
+ clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 4);
break;
}
}
GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer; "
"dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
- if (!(non_mod == 1 && bits == 1))
+ if (!(non_mod == 1 && clut_index == 1))
memset (destbuf, clut_index, run_length);
destbuf += run_length;
/* Rephrased - it's better to work with bytes with default value '0' instead of reading from memory we don't own. */
while (!stop_parsing && (gst_bit_reader_get_remaining (&gb) > 0)) {
guint run_length = 0, clut_index = 0;
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 8);
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 8);
if (bits) { /* 8-bit_pixel-code */
run_length = 1;
clut_index = bits;
} else { /* 8-bit_zero */
- gst_bit_reader_get_bits_uint32 (&gb, &bits, 1);
+ bits = gst_bit_reader_get_bits_uint32_unchecked (&gb, 1);
if (bits == 0) { /* switch_1 == '0' */
/* run_length_1-127 for pseudo-colour _entry) '0x00' */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 7);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 7);
if (run_length == 0) { /* end_of_string_signal */
stop_parsing = TRUE;
}
} else { /* switch_1 == '1' */
/* run_length_3-127 */
- gst_bit_reader_get_bits_uint32 (&gb, &run_length, 7);
- gst_bit_reader_get_bits_uint32 (&gb, &clut_index, 8);
+ run_length = gst_bit_reader_get_bits_uint32_unchecked (&gb, 7);
+ clut_index = gst_bit_reader_get_bits_uint32_unchecked (&gb, 8);
if (run_length < 3) {
GST_WARNING ("runlength value was %u, but the spec requires it "
GST_TRACE ("RUNLEN: setting %u pixels to color 0x%x in destination buffer; "
"dbuf_len left is %d pixels", run_length, clut_index, dbuf_len);
- if (!(non_mod == 1 && bits == 1))
+ if (!(non_mod == 1 && clut_index == 1))
memset (destbuf, clut_index, run_length);
destbuf += run_length;
_dvb_sub_parse_end_of_display_set (DvbSub * dvb_sub, guint16 page_id,
guint8 * buf, gint buf_size, guint64 pts)
{
- DVBSubtitles *sub = g_slice_new0 (DVBSubtitles);
-
- DVBSubRegion *region;
DVBSubRegionDisplay *display;
- DVBSubtitleRect *rect;
+ DVBSubtitles *sub;
DVBSubCLUT *clut;
guint32 *clut_table;
int i;
- static unsigned counter = 0; /* DEBUG use only *//* FIXME: get rid */
-
GST_DEBUG ("DISPLAY SET END: page_id = %u, length = %d", page_id, buf_size);
- sub->rects = NULL;
+ sub = g_slice_new0 (DVBSubtitles);
+
#if 0 /* FIXME: PTS stuff not figured out yet */
sub->start_display_time = 0;
sub->end_display_time = priv->page_time_out * 1000;
sub->format = 0; /* 0 = graphics */
#endif
+ /* N.B. g_new0() will return NULL if num_rects is 0 */
sub->num_rects = dvb_sub->display_list_size;
-
- if (sub->num_rects > 0) {
- // FIXME-MEMORY-LEAK: This structure is not freed up yet
- sub->rects = g_malloc0 (sizeof (*sub->rects) * sub->num_rects); /* GSlice? */
- for (i = 0; i < sub->num_rects; i++)
- sub->rects[i] = g_malloc0 (sizeof (*sub->rects[i])); /* GSlice? */
- }
+ sub->rects = g_new0 (DVBSubtitleRect, sub->num_rects);
i = 0;
sub->display_def = dvb_sub->display_def;
for (display = dvb_sub->display_list; display; display = display->next) {
+ DVBSubtitleRect *rect;
+ DVBSubRegion *region;
+
region = get_region (dvb_sub, display->region_id);
- rect = sub->rects[i];
if (!region)
continue;
+ rect = &sub->rects[i];
rect->x = display->x_pos;
rect->y = display->y_pos;
rect->w = region->width;
rect->pict.data = g_malloc (region->buf_size); /* FIXME: Can we use GSlice here? */
memcpy (rect->pict.data, region->pbuf, region->buf_size);
- ++counter;
- GST_DEBUG ("DISPLAY: an object rect created: number %u, iteration %u, "
- "pos: %d:%d, size: %dx%d", counter, i, rect->x, rect->y, rect->w,
- rect->h);
+ GST_DEBUG ("DISPLAY: an object rect created: iteration %u, "
+ "pos: %d:%d, size: %dx%d", i, rect->x, rect->y, rect->w, rect->h);
GST_MEMDUMP ("rect->pict.data content", rect->pict.data, region->buf_size);
dvb_subtitles_free (DVBSubtitles * sub)
{
int i;
- DVBSubtitleRect *rect;
if (sub == NULL)
return;
/* Now free up all the temporary memory we allocated */
for (i = 0; i < sub->num_rects; ++i) {
- rect = sub->rects[i];
-
- g_free (rect->pict.palette);
- g_free (rect->pict.data);
- g_free (rect);
+ g_free (sub->rects[i].pict.palette);
+ g_free (sub->rects[i].pict.data);
}
g_free (sub->rects);
g_slice_free (DVBSubtitles, sub);
/* TODO: Add deinitalization code here */
/* FIXME: Clear up region_list contents */
delete_state (sub);
+ while (sub->display_list) {
+ DVBSubRegionDisplay *tmp = sub->display_list->next;
+ g_slice_free (DVBSubRegionDisplay, sub->display_list);
+ sub->display_list = tmp;
+ }
g_string_free (sub->pes_buffer, TRUE);
g_slice_free (DvbSub, sub);
}