Commit 87aed1a2 authored by Tim-Philipp Müller's avatar Tim-Philipp Müller 🐠
Browse files

gst/avi/gstavisubtitle.*: Refactor a bit; fix name extraction; don't assume...

gst/avi/gstavisubtitle.*: Refactor a bit; fix name extraction; don't assume all the data in the chunk is actually sub...

Original commit message from CVS:
* gst/avi/gstavisubtitle.c: (src_template),
(gst_avi_subtitle_extract_utf8_file),
(gst_avi_subtitle_parse_gab2_chunk), (gst_avi_subtitle_chain),
(gst_avi_subtitle_base_init), (gst_avi_subtitle_class_init),
(gst_avi_subtitle_init), (gst_avi_subtitle_change_state):
* gst/avi/gstavisubtitle.h:
Refactor a bit; fix name extraction; don't assume all the data
in the chunk is actually subtitle data, there may be padding at
the end; fix GST_ELEMENT_ERROR usage; store extracted subtitle
file so it's there to send again after a seek (for future use).
parent d0c62e91
2007-12-18 Tim-Philipp Müller <tim at centricular dot net>
* gst/avi/gstavisubtitle.c: (src_template),
(gst_avi_subtitle_extract_utf8_file),
(gst_avi_subtitle_parse_gab2_chunk), (gst_avi_subtitle_chain),
(gst_avi_subtitle_base_init), (gst_avi_subtitle_class_init),
(gst_avi_subtitle_init), (gst_avi_subtitle_change_state):
* gst/avi/gstavisubtitle.h:
Refactor a bit; fix name extraction; don't assume all the data
in the chunk is actually subtitle data, there may be padding at
the end; fix GST_ELEMENT_ERROR usage; store extracted subtitle
file so it's there to send again after a seek (for future use).
2007-12-18 Thijs Vermeir <thijsvermeir@gmail.com>
 
* gst/avi/Makefile.am:
......
/* GStreamer
/* GStreamer AVI GAB2 subtitle parser
* Copyright (C) <2007> Thijs Vermeir <thijsvermeir@gmail.com>
* Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -17,19 +18,21 @@
* Boston, MA 02111-1307, USA.
*/
/* example of a subtitle chunk in an avi file
* 00000000 (0x8051700): 47 41 42 32 00 02 00 10 00 00 00 45 00 6e 00 67 GAB2.......E.n.g
* 00000010 (0x8051710): 00 6c 00 69 00 73 00 68 00 00 00 04 00 8e 00 00 .l.i.s.h........
* 00000020 (0x8051720): 00 ef bb bf 31 0d 0a 30 30 3a 30 30 3a 30 30 2c ....1..00:00:00,
* 00000030 (0x8051730): 31 30 30 20 2d 2d 3e 20 30 30 3a 30 30 3a 30 32 100 --> 00:00:02
* 00000040 (0x8051740): 2c 30 30 30 0d 0a 3c 62 3e 41 6e 20 55 54 46 38 ,000..<b>An UTF8
* 00000050 (0x8051750): 20 53 75 62 74 69 74 6c 65 20 77 69 74 68 20 42 Subtitle with B
* 00000060 (0x8051760): 4f 4d 3c 2f 62 3e 0d 0a 0d 0a 32 0d 0a 30 30 3a OM</b>....2..00:
* 00000070 (0x8051770): 30 30 3a 30 32 2c 31 30 30 20 2d 2d 3e 20 30 30 00:02,100 --> 00
* 00000080 (0x8051780): 3a 30 30 3a 30 34 2c 30 30 30 0d 0a 53 6f 6d 65 :00:04,000..Some
* 00000090 (0x8051790): 74 68 69 6e 67 20 6e 6f 6e 41 53 43 49 49 20 2d thing nonASCII -
* 000000a0 (0x80517a0): 20 c2 b5 c3 b6 c3 a4 c3 bc c3 9f 0d 0a 0d 0a ..............
*/
/* FIXME: BOM detection and format conversion; validate UTF-8; handle seeks */
/* example of a subtitle chunk in an avi file
* 00000000: 47 41 42 32 00 02 00 10 00 00 00 45 00 6e 00 67 GAB2.......E.n.g
* 00000010: 00 6c 00 69 00 73 00 68 00 00 00 04 00 8e 00 00 .l.i.s.h........
* 00000020: 00 ef bb bf 31 0d 0a 30 30 3a 30 30 3a 30 30 2c ....1..00:00:00,
* 00000030: 31 30 30 20 2d 2d 3e 20 30 30 3a 30 30 3a 30 32 100 --> 00:00:02
* 00000040: 2c 30 30 30 0d 0a 3c 62 3e 41 6e 20 55 54 46 38 ,000..<b>An UTF8
* 00000050: 20 53 75 62 74 69 74 6c 65 20 77 69 74 68 20 42 Subtitle with B
* 00000060: 4f 4d 3c 2f 62 3e 0d 0a 0d 0a 32 0d 0a 30 30 3a OM</b>....2..00:
* 00000070: 30 30 3a 30 32 2c 31 30 30 20 2d 2d 3e 20 30 30 00:02,100 --> 00
* 00000080: 3a 30 30 3a 30 34 2c 30 30 30 0d 0a 53 6f 6d 65 :00:04,000..Some
* 00000090: 74 68 69 6e 67 20 6e 6f 6e 41 53 43 49 49 20 2d thing nonASCII -
* 000000a0: 20 c2 b5 c3 b6 c3 a4 c3 bc c3 9f 0d 0a 0d 0a ..............
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
......@@ -41,7 +44,6 @@
GST_DEBUG_CATEGORY_STATIC (avisubtitle_debug);
#define GST_CAT_DEFAULT avisubtitle_debug
GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
......@@ -56,19 +58,20 @@ static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
);
static GstFlowReturn gst_avi_subtitle_chain (GstPad * pad, GstBuffer * buffer);
static GstStateChangeReturn gst_avi_subtitle_change_state (GstElement * element,
GstStateChange transition);
GST_BOILERPLATE (GstAviSubtitle, gst_avi_subtitle, GstElement,
GST_TYPE_ELEMENT);
static GstBuffer *
gst_avi_subtitle_utf8_file (GstBuffer * buffer, guint offset)
gst_avi_subtitle_extract_utf8_file (GstBuffer * buffer, guint offset, guint len)
{
guint8 *file = GST_BUFFER_DATA (buffer) + offset;
if (file[0] == 0xEF && file[1] == 0xBB && file[2] == 0xBF) {
/* UTF-8 */
return gst_buffer_create_sub (buffer, offset + 3,
GST_BUFFER_SIZE (buffer) - offset - 3);
return gst_buffer_create_sub (buffer, offset + 3, len - 3);
}
/* TODO Check for:
* 00 00 FE FF UTF-32, big-endian
......@@ -78,121 +81,137 @@ gst_avi_subtitle_utf8_file (GstBuffer * buffer, guint offset)
*/
/* No BOM detected assuming UTF-8 */
return gst_buffer_create_sub (buffer, offset,
GST_BUFFER_SIZE (buffer) - offset);
return gst_buffer_create_sub (buffer, offset, len);
}
static GstFlowReturn
gst_avi_subtitle_chain (GstPad * pad, GstBuffer * buffer)
gst_avi_subtitle_parse_gab2_chunk (GstAviSubtitle * sub, GstBuffer * buf)
{
guint name_length, file_length;
gunichar2 *name;
// gchar* name_utf8;
GstFlowReturn ret;
GstAviSubtitle *avisubtitle = GST_AVI_SUBTITLE (GST_PAD_PARENT (pad));
const guint8 *data;
gchar *name_utf8;
guint name_length;
guint file_length;
guint size;
/* we expext only one buffer packet with the whole srt/ssa file in it */
data = GST_BUFFER_DATA (buf);
size = GST_BUFFER_SIZE (buf);
/* check the magic word "GAB2\0" */
if (GST_BUFFER_SIZE (buffer) <= 11
|| memcmp (GST_BUFFER_DATA (buffer), "GAB2\0", 5) != 0)
/* check the magic word "GAB2\0", and the next word must be 2 */
if (size < 12 || memcmp (data, "GAB2\0\2\0", 5 + 2) != 0)
goto wrong_magic_word;
/* next word must be 2 */
if (GST_READ_UINT16_LE (GST_BUFFER_DATA (buffer) + 5) != 0x2)
goto wrong_fixed_word_1;
/* read 'name' of subtitle */
name_length = GST_READ_UINT32_LE (data + 5 + 2);
GST_LOG_OBJECT (sub, "length of name: %u", name_length);
if (size <= 17 + name_length)
goto wrong_name_length;
name_length = GST_READ_UINT32_LE (GST_BUFFER_DATA (buffer) + 7);
GST_LOG ("length of name: %d", name_length);
if (GST_BUFFER_SIZE (buffer) <= 17 + name_length)
goto wrong_length_1;
name_utf8 = g_convert ((gchar *) data + 11, name_length, "UTF-8", "UTF-16LE",
NULL, NULL, NULL);
name = (gunichar2 *) & (GST_BUFFER_DATA (buffer)[11]);
// FIXME Take care for endianess in UTF-16
// name_utf8 = g_utf16_to_utf8( name, name_length, NULL, NULL, NULL);
// GST_LOG("avi subtitle name: %s", name_utf8);
// g_free (name_utf8);
if (name_utf8) {
/* FIXME: put in a taglist */
GST_LOG_OBJECT (sub, "subtitle name: %s", name_utf8);
g_free (name_utf8);
}
/* next word must be 4 */
if (GST_READ_UINT16_LE (GST_BUFFER_DATA (buffer) + 11 + name_length) != 0x4)
if (GST_READ_UINT16_LE (data + 11 + name_length) != 0x4)
goto wrong_fixed_word_2;
file_length =
GST_READ_UINT32_LE (GST_BUFFER_DATA (buffer) + 13 + name_length);
GST_LOG ("length srt/ssa file: %d", file_length);
file_length = GST_READ_UINT32_LE (data + 13 + name_length);
GST_LOG_OBJECT (sub, "length srt/ssa file: %u", file_length);
if (GST_BUFFER_SIZE (buffer) != 17 + name_length + file_length)
if (size < (17 + name_length + file_length))
goto wrong_total_length;
/* push the file over the src pad */
ret =
gst_pad_push (avisubtitle->src, gst_avi_subtitle_utf8_file (buffer,
17 + name_length));
gst_buffer_unref (buffer);
/* store this, so we can send it again after a seek; note that we shouldn't
* assume all the remaining data in the chunk is subtitle data, there may
* be padding at the end for some reason, so only parse file_length bytes */
sub->subfile =
gst_avi_subtitle_extract_utf8_file (buf, 17 + name_length, file_length);
return ret;
return GST_FLOW_OK;
/* all the errors */
/* ERRORS */
wrong_magic_word:
GST_ELEMENT_ERROR (avisubtitle, STREAM, DECODE, NULL, ("Wrong magic word"));
gst_buffer_unref (buffer);
return GST_FLOW_ERROR;
{
GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL), ("Wrong magic word"));
return GST_FLOW_ERROR;
}
wrong_name_length:
{
GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
("name doesn't fit in buffer (%d < %d)", size, 17 + name_length));
return GST_FLOW_ERROR;
}
wrong_fixed_word_2:
{
GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
("wrong fixed word: expected %u, got %u", 4,
GST_READ_UINT16_LE (data + 11 + name_length)));
return GST_FLOW_ERROR;
}
wrong_total_length:
{
GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
("buffer size is wrong: need %d bytes, have %d bytes",
17 + name_length + file_length, size));
return GST_FLOW_ERROR;
}
}
wrong_fixed_word_1:
GST_ELEMENT_ERROR (avisubtitle, STREAM, DECODE, NULL,
("wrong fixed word: expected %d found %d", 2,
GST_READ_UINT16_LE (GST_BUFFER_DATA (buffer) + 5)));
gst_buffer_unref (buffer);
return GST_FLOW_ERROR;
static GstFlowReturn
gst_avi_subtitle_chain (GstPad * pad, GstBuffer * buffer)
{
GstAviSubtitle *sub = GST_AVI_SUBTITLE (GST_PAD_PARENT (pad));
GstFlowReturn ret;
wrong_length_1:
GST_ELEMENT_ERROR (avisubtitle, STREAM, DECODE, NULL,
("length of the buffer is too small (%d < %d)", GST_BUFFER_SIZE (buffer),
17 + name_length));
gst_buffer_unref (buffer);
return GST_FLOW_ERROR;
if (sub->subfile != NULL) {
GST_WARNING_OBJECT (sub, "Got more buffers than expected, dropping");
ret = GST_FLOW_UNEXPECTED;
goto done;
}
wrong_fixed_word_2:
GST_ELEMENT_ERROR (avisubtitle, STREAM, DECODE, NULL,
("wrong fixed word: expected %d found %d", 4,
GST_READ_UINT16_LE (GST_BUFFER_DATA (buffer) + 11 + name_length)));
gst_buffer_unref (buffer);
return GST_FLOW_ERROR;
/* we expect exactly one buffer with the whole srt/ssa file in it */
ret = gst_avi_subtitle_parse_gab2_chunk (sub, buffer);
if (ret != GST_FLOW_OK)
goto done;
/* now push the subtitle data downstream */
ret = gst_pad_push (sub->src, gst_buffer_ref (sub->subfile));
done:
wrong_total_length:
GST_ELEMENT_ERROR (avisubtitle, STREAM, DECODE, NULL,
("buffer size is wrong: need %d bytes, have %d bytes",
17 + name_length + file_length, GST_BUFFER_SIZE (buffer)));
gst_buffer_unref (buffer);
return GST_FLOW_ERROR;
return ret;
}
static void
gst_avi_subtitle_base_init (gpointer klass)
{
static const GstElementDetails gst_avi_demux_details =
GST_ELEMENT_DETAILS ("Avi subtitle parser",
"Codec/Demuxer",
"Parse avi subtitle stream",
"Thijs Vermeir <thijsvermeir@gmail.com>");
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
/* add the pad templates to the element */
GST_DEBUG_CATEGORY_INIT (avisubtitle_debug, "avisubtitle", 0,
"parse avi subtitle stream");
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sink_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&src_template));
/* set the element details */
gst_element_class_set_details (element_class, &gst_avi_demux_details);
gst_element_class_set_details_simple (element_class,
"Avi subtitle parser", "Codec/Demuxer", "Parse avi subtitle stream",
"Thijs Vermeir <thijsvermeir@gmail.com>");
}
static void
gst_avi_subtitle_class_init (GstAviSubtitleClass * klass)
{
GST_DEBUG_CATEGORY_INIT (avisubtitle_debug, "avisubtitle", 0,
"parse avi subtitle stream");
GstElementClass *gstelement_class = (GstElementClass *) klass;
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_avi_subtitle_change_state);
}
static void
......@@ -202,6 +221,38 @@ gst_avi_subtitle_init (GstAviSubtitle * self, GstAviSubtitleClass * klass)
gst_element_add_pad (GST_ELEMENT (self), self->src);
self->sink = gst_pad_new_from_static_template (&sink_template, "sink");
gst_pad_set_chain_function (self->sink,
GST_DEBUG_FUNCPTR (gst_avi_subtitle_chain));
gst_element_add_pad (GST_ELEMENT (self), self->sink);
gst_pad_set_chain_function (self->sink, gst_avi_subtitle_chain);
}
static GstStateChangeReturn
gst_avi_subtitle_change_state (GstElement * element, GstStateChange transition)
{
GstStateChangeReturn ret;
GstAviSubtitle *sub = GST_AVI_SUBTITLE (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
case GST_STATE_CHANGE_READY_TO_PAUSED:
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
if (sub->subfile) {
gst_buffer_unref (sub->subfile);
sub->subfile = NULL;
}
break;
default:
break;
}
return ret;
}
......@@ -24,8 +24,10 @@ struct _GstAviSubtitle
{
GstElement parent;
GstPad* src;
GstPad* sink;
GstPad *src;
GstPad *sink;
GstBuffer *subfile; /* the complete subtitle file in one buffer */
};
struct _GstAviSubtitleClass
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment