summaryrefslogtreecommitdiff
path: root/ext/kate/gstkatespu.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/kate/gstkatespu.c')
-rw-r--r--ext/kate/gstkatespu.c67
1 files changed, 46 insertions, 21 deletions
diff --git a/ext/kate/gstkatespu.c b/ext/kate/gstkatespu.c
index 439291634..85606495e 100644
--- a/ext/kate/gstkatespu.c
+++ b/ext/kate/gstkatespu.c
@@ -276,17 +276,17 @@ gst_kate_spu_decode_command_sequence (GstKateEnc * ke, GstBuffer * buf,
guint16 date;
guint16 next_command_sequence;
const guint8 *ptr;
+ guint8 *data;
guint16 sz;
+ gsize size;
- if (command_sequence_offset >= GST_BUFFER_SIZE (buf)) {
- GST_ELEMENT_ERROR (ke, STREAM, DECODE, (NULL),
- ("Command sequence offset %u is out of range %u",
- command_sequence_offset, GST_BUFFER_SIZE (buf)));
- return GST_FLOW_ERROR;
- }
+ data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
- ptr = GST_BUFFER_DATA (buf) + command_sequence_offset;
- sz = GST_BUFFER_SIZE (buf) - command_sequence_offset;
+ if (command_sequence_offset >= size)
+ goto out_of_range;
+
+ ptr = data + command_sequence_offset;
+ sz = size - command_sequence_offset;
GST_DEBUG_OBJECT (ke, "Decoding command sequence at %u (%u bytes)",
command_sequence_offset, sz);
@@ -353,21 +353,35 @@ gst_kate_spu_decode_command_sequence (GstKateEnc * ke, GstBuffer * buf,
if (next_command_sequence != command_sequence_offset) {
GST_DEBUG_OBJECT (ke, "Jumping to next sequence at offset %u",
next_command_sequence);
+ gst_buffer_unmap (buf, data, size);
return gst_kate_spu_decode_command_sequence (ke, buf,
next_command_sequence);
} else {
+ gst_buffer_unmap (buf, data, size);
GST_DEBUG_OBJECT (ke, "No more sequences to decode");
return GST_FLOW_OK;
}
break;
default:
+ gst_buffer_unmap (buf, data, size);
GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL),
("Invalid SPU command: %u", cmd));
return GST_FLOW_ERROR;
}
}
+ gst_buffer_unmap (buf, data, size);
GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL), ("Error parsing SPU"));
return GST_FLOW_ERROR;
+
+ /* ERRORS */
+out_of_range:
+ {
+ gst_buffer_unmap (buf, data, size);
+ GST_ELEMENT_ERROR (ke, STREAM, DECODE, (NULL),
+ ("Command sequence offset %u is out of range %u",
+ command_sequence_offset, size));
+ return GST_FLOW_ERROR;
+ }
}
static inline int
@@ -451,8 +465,10 @@ GstFlowReturn
gst_kate_spu_decode_spu (GstKateEnc * ke, GstBuffer * buf, kate_region * kr,
kate_bitmap * kb, kate_palette * kp)
{
- const guint8 *ptr = GST_BUFFER_DATA (buf);
- size_t sz = GST_BUFFER_SIZE (buf);
+ guint8 *data;
+ gsize size;
+ const guint8 *ptr;
+ size_t sz;
guint16 packet_size;
guint16 x, y;
size_t n;
@@ -463,6 +479,11 @@ gst_kate_spu_decode_spu (GstKateEnc * ke, GstBuffer * buf, kate_region * kr,
guint16 next_command_sequence;
guint16 code;
+ data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
+
+ ptr = data;
+ sz = size;
+
/* before decoding anything, initialize to sensible defaults */
memset (ke->spu_colormap, 0, sizeof (ke->spu_colormap));
memset (ke->spu_alpha, 0, sizeof (ke->spu_alpha));
@@ -476,19 +497,21 @@ gst_kate_spu_decode_spu (GstKateEnc * ke, GstBuffer * buf, kate_region * kr,
packet_size = GST_KATE_UINT16_BE (ptr);
ADVANCE (2);
GST_DEBUG_OBJECT (ke, "packet size %u (GstBuffer size %u)", packet_size,
- GST_BUFFER_SIZE (buf));
+ size);
CHECK (2);
next_command_sequence = GST_KATE_UINT16_BE (ptr);
ADVANCE (2);
- ptr = GST_BUFFER_DATA (buf) + next_command_sequence;
- sz = GST_BUFFER_SIZE (buf) - next_command_sequence;
+ ptr = data + next_command_sequence;
+ sz = size - next_command_sequence;
GST_DEBUG_OBJECT (ke, "next command sequence at %u for %u",
next_command_sequence, (guint) sz);
rflow = gst_kate_spu_decode_command_sequence (ke, buf, next_command_sequence);
- if (G_UNLIKELY (rflow != GST_FLOW_OK))
+ if (G_UNLIKELY (rflow != GST_FLOW_OK)) {
+ gst_buffer_unmap (buf, data, size);
return rflow;
+ }
/* if no addresses or sizes were given, or if they define an empty SPU, nothing more to do */
if (G_UNLIKELY (ke->spu_right - ke->spu_left < 0
@@ -501,13 +524,16 @@ gst_kate_spu_decode_spu (GstKateEnc * ke, GstBuffer * buf, kate_region * kr,
GST_WARNING_OBJECT (ke, "SPU area is empty, nothing to encode");
kate_bitmap_init (kb);
kb->width = kb->height = 0;
+ gst_buffer_unmap (buf, data, size);
return GST_FLOW_OK;
}
/* create the palette */
rflow = gst_kate_spu_create_spu_palette (ke, kp);
- if (G_UNLIKELY (rflow != GST_FLOW_OK))
+ if (G_UNLIKELY (rflow != GST_FLOW_OK)) {
+ gst_buffer_unmap (buf, data, size);
return rflow;
+ }
/* create the bitmap */
kate_bitmap_init (kb);
@@ -517,14 +543,15 @@ gst_kate_spu_decode_spu (GstKateEnc * ke, GstBuffer * buf, kate_region * kr,
kb->type = kate_bitmap_type_paletted;
kb->pixels = (unsigned char *) g_malloc (kb->width * kb->height);
if (G_UNLIKELY (!kb->pixels)) {
+ gst_buffer_unmap (buf, data, size);
GST_ELEMENT_ERROR (ke, STREAM, ENCODE, (NULL),
("Failed to allocate memory for pixel data"));
return GST_FLOW_ERROR;
}
n = 0;
- pixptr[0] = GST_BUFFER_DATA (buf) + ke->spu_pix_data[0];
- pixptr[1] = GST_BUFFER_DATA (buf) + ke->spu_pix_data[1];
+ pixptr[0] = data + ke->spu_pix_data[0];
+ pixptr[1] = data + ke->spu_pix_data[1];
nybble_offset[0] = 0;
nybble_offset[1] = 0;
max_nybbles[0] = 2 * (packet_size - ke->spu_pix_data[0]);
@@ -591,6 +618,7 @@ gst_kate_spu_decode_spu (GstKateEnc * ke, GstBuffer * buf, kate_region * kr,
probably going to end before the next one while being readable */
//ke->hide_time = ke->show_time + (1000 * 90 / 1024);
}
+ gst_buffer_unmap (buf, data, size);
return GST_FLOW_OK;
}
@@ -856,15 +884,12 @@ gst_kate_spu_encode_spu (GstKateDec * kd, const kate_event * ev)
bytes[3] = first_commands_offset & 0xff;
/* Create a buffer with those values */
- buffer = gst_buffer_new ();
+ buffer = gst_buffer_new_wrapped (bytes, nbytes);
if (G_UNLIKELY (!buffer)) {
GST_WARNING_OBJECT (kd,
"Failed to allocate %" G_GSIZE_FORMAT " byte buffer", nbytes);
goto error;
}
- GST_BUFFER_DATA (buffer) = bytes;
- GST_BUFFER_MALLOCDATA (buffer) = bytes;
- GST_BUFFER_SIZE (buffer) = nbytes;
GST_BUFFER_OFFSET_END (buffer) = GST_SECOND * (ev->end_time);
GST_BUFFER_OFFSET (buffer) = GST_SECOND * (ev->start_time);
GST_BUFFER_TIMESTAMP (buffer) = GST_SECOND * (ev->start_time);