2 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
3 * Copyright (C) 2006 Andy Wingo <wingo@pobox.com>
4 * Copyright (C) 2008 Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-kateparse
25 * @short_description: parses kate streams
26 * @see_also: katedec, vorbisparse, oggdemux, theoraparse
28 * The kateparse element will parse the header packets of the Kate
29 * stream and put them as the streamheader in the caps. This is used in the
30 * multifdsink case where you want to stream live kate streams to multiple
31 * clients, each client has to receive the streamheaders first before they can
32 * consume the kate packets.
34 * This element also makes sure that the buffers that it pushes out are properly
35 * timestamped and that their offset and offset_end are set. The buffers that
36 * kateparse outputs have all of the metadata that oggmux expects to receive,
37 * which allows you to (for example) remux an ogg/kate file.
39 * ## Example pipelines
42 * gst-launch-1.0 -v filesrc location=kate.ogg ! oggdemux ! kateparse ! fakesink
44 * This pipeline shows that the streamheader is set in the caps, and that each
45 * buffer has the timestamp, duration, offset, and offset_end set.
48 * gst-launch-1.0 filesrc location=kate.ogg ! oggdemux ! kateparse \
49 * ! oggmux ! filesink location=kate-remuxed.ogg
51 * This pipeline shows remuxing. kate-remuxed.ogg might not be exactly the same
52 * as kate.ogg, but they should produce exactly the same decoded data.
60 #include "gstkateelements.h"
61 #include "gstkateutil.h"
62 #include "gstkateparse.h"
64 GST_DEBUG_CATEGORY_EXTERN (gst_kateparse_debug);
65 #define GST_CAT_DEFAULT gst_kateparse_debug
67 static GstStaticPadTemplate gst_kate_parse_sink_factory =
68 GST_STATIC_PAD_TEMPLATE ("sink",
71 GST_STATIC_CAPS ("subtitle/x-kate; application/x-kate")
74 static GstStaticPadTemplate gst_kate_parse_src_factory =
75 GST_STATIC_PAD_TEMPLATE ("src",
78 GST_STATIC_CAPS ("subtitle/x-kate; application/x-kate")
81 GST_DEBUG_CATEGORY (gst_kateparse_debug);
83 #define gst_kate_parse_parent_class parent_class
84 G_DEFINE_TYPE (GstKateParse, gst_kate_parse, GST_TYPE_ELEMENT);
86 kate_element_init (plugin); \
87 GST_DEBUG_CATEGORY_INIT (gst_kateparse_debug, "kateparse", 0, "Kate parser");
88 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (kateparse, "kateparse", GST_RANK_NONE,
89 GST_TYPE_KATE_PARSE, _do_init);
91 static GstFlowReturn gst_kate_parse_chain (GstPad * pad, GstObject * parent,
93 static GstStateChangeReturn gst_kate_parse_change_state (GstElement * element,
94 GstStateChange transition);
95 static gboolean gst_kate_parse_sink_event (GstPad * pad, GstObject * parent,
97 static gboolean gst_kate_parse_src_query (GstPad * pad, GstObject * parent,
100 static gboolean gst_kate_parse_convert (GstPad * pad,
101 GstFormat src_format, gint64 src_value,
102 GstFormat * dest_format, gint64 * dest_value);
104 static GstFlowReturn gst_kate_parse_parse_packet (GstKateParse * parse,
108 gst_kate_parse_class_init (GstKateParseClass * klass)
110 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
112 gstelement_class->change_state = gst_kate_parse_change_state;
114 gst_element_class_add_static_pad_template (gstelement_class,
115 &gst_kate_parse_src_factory);
116 gst_element_class_add_static_pad_template (gstelement_class,
117 &gst_kate_parse_sink_factory);
119 gst_element_class_set_static_metadata (gstelement_class, "Kate stream parser",
120 "Codec/Parser/Subtitle",
121 "parse raw kate streams",
122 "Vincent Penquerc'h <ogg.k.ogg.k at googlemail dot com>");
124 klass->parse_packet = GST_DEBUG_FUNCPTR (gst_kate_parse_parse_packet);
128 gst_kate_parse_init (GstKateParse * parse)
131 gst_pad_new_from_static_template (&gst_kate_parse_sink_factory, "sink");
132 gst_pad_set_chain_function (parse->sinkpad,
133 GST_DEBUG_FUNCPTR (gst_kate_parse_chain));
134 gst_pad_set_event_function (parse->sinkpad,
135 GST_DEBUG_FUNCPTR (gst_kate_parse_sink_event));
136 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
139 gst_pad_new_from_static_template (&gst_kate_parse_src_factory, "src");
140 gst_pad_set_query_function (parse->srcpad,
141 GST_DEBUG_FUNCPTR (gst_kate_parse_src_query));
142 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
146 gst_kate_parse_drain_event_queue (GstKateParse * parse)
148 while (parse->event_queue->length) {
151 event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
152 gst_pad_event_default (parse->sinkpad, NULL, event);
157 gst_kate_parse_push_headers (GstKateParse * parse)
159 /* mark and put on caps */
163 GList *headers, *outbuf_list = NULL;
167 /* get the headers into the caps, passing them to kate as we go */
169 gst_kate_util_set_header_on_caps (&parse->element,
170 gst_pad_get_current_caps (parse->sinkpad), parse->streamheader);
172 if (G_UNLIKELY (!caps)) {
173 GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL),
174 ("Failed to set headers on caps"));
175 return GST_FLOW_ERROR;
178 GST_DEBUG_OBJECT (parse, "here are the caps: %" GST_PTR_FORMAT, caps);
179 res = gst_pad_set_caps (parse->srcpad, caps);
180 gst_caps_unref (caps);
181 if (G_UNLIKELY (!res)) {
182 GST_WARNING_OBJECT (parse->srcpad, "Failed to set caps on source pad");
183 return GST_FLOW_NOT_NEGOTIATED;
186 headers = parse->streamheader;
190 outbuf = GST_BUFFER_CAST (headers->data);
192 if (!gst_buffer_map (outbuf, &info, GST_MAP_READ)) {
193 GST_WARNING_OBJECT (outbuf, "Failed to map buffer");
197 kate_packet_wrap (&packet, info.size, info.data);
198 ret = kate_decode_headerin (&parse->ki, &parse->kc, &packet);
199 if (G_UNLIKELY (ret < 0)) {
200 GST_WARNING_OBJECT (parse, "Failed to decode header: %s",
201 gst_kate_util_get_error_message (ret));
203 gst_buffer_unmap (outbuf, &info);
204 /* takes ownership of outbuf, which was previously in parse->streamheader */
205 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_HEADER);
206 outbuf_list = g_list_append (outbuf_list, outbuf);
207 headers = headers->next;
210 /* first process queued events */
211 gst_kate_parse_drain_event_queue (parse);
213 /* push out buffers, ignoring return value... */
214 headers = outbuf_list;
216 outbuf = GST_BUFFER_CAST (headers->data);
217 gst_pad_push (parse->srcpad, outbuf);
218 headers = headers->next;
221 g_list_free (outbuf_list);
222 g_list_free (parse->streamheader);
223 parse->streamheader = NULL;
225 parse->streamheader_sent = TRUE;
231 gst_kate_parse_clear_queue (GstKateParse * parse)
233 GST_DEBUG_OBJECT (parse, "Clearing queue");
234 while (parse->buffer_queue->length) {
237 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
238 gst_buffer_unref (buf);
240 while (parse->event_queue->length) {
243 event = GST_EVENT_CAST (g_queue_pop_head (parse->event_queue));
244 gst_event_unref (event);
249 gst_kate_parse_push_buffer (GstKateParse * parse, GstBuffer * buf,
252 GST_LOG_OBJECT (parse, "granulepos %16" G_GINT64_MODIFIER "x", granulepos);
253 if (granulepos < 0) {
254 /* packets coming not from Ogg won't have a granpos in the offset end,
255 so we have to synthesize one here - only problem is we don't know
256 the backlink - pretend there's none for now */
257 GST_INFO_OBJECT (parse, "No granulepos on buffer, synthesizing one");
259 kate_duration_granule (&parse->ki,
260 GST_BUFFER_TIMESTAMP (buf) /
261 (double) GST_SECOND) << kate_granule_shift (&parse->ki);
263 GST_BUFFER_OFFSET (buf) =
264 kate_granule_time (&parse->ki, granulepos) * GST_SECOND;
265 GST_BUFFER_OFFSET_END (buf) = granulepos;
266 GST_BUFFER_TIMESTAMP (buf) = GST_BUFFER_OFFSET (buf);
268 return gst_pad_push (parse->srcpad, buf);
272 gst_kate_parse_drain_queue_prematurely (GstKateParse * parse)
274 GstFlowReturn ret = GST_FLOW_OK;
276 /* got an EOS event, make sure to push out any buffers that were in the queue
277 * -- won't normally be the case, but this catches the
278 * didn't-get-a-granulepos-on-the-last-packet case. Assuming a continuous
281 /* if we got EOS before any buffers came, go ahead and push the other events
283 gst_kate_parse_drain_event_queue (parse);
285 while (!g_queue_is_empty (parse->buffer_queue)) {
289 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
291 granpos = GST_BUFFER_OFFSET_END (buf);
292 ret = gst_kate_parse_push_buffer (parse, buf, granpos);
294 if (ret != GST_FLOW_OK)
298 g_assert (g_queue_is_empty (parse->buffer_queue));
305 gst_kate_parse_drain_queue (GstKateParse * parse, gint64 granulepos)
307 GstFlowReturn ret = GST_FLOW_OK;
309 if (!g_queue_is_empty (parse->buffer_queue)) {
311 buf = GST_BUFFER_CAST (g_queue_pop_head (parse->buffer_queue));
312 ret = gst_kate_parse_push_buffer (parse, buf, granulepos);
314 if (ret != GST_FLOW_OK)
317 g_assert (g_queue_is_empty (parse->buffer_queue));
324 gst_kate_parse_queue_buffer (GstKateParse * parse, GstBuffer * buf)
326 GstFlowReturn ret = GST_FLOW_OK;
329 buf = gst_buffer_make_writable (buf);
331 /* oggdemux stores the granule pos in the offset end */
332 granpos = GST_BUFFER_OFFSET_END (buf);
333 GST_LOG_OBJECT (parse, "granpos %16" G_GINT64_MODIFIER "x", granpos);
334 g_queue_push_tail (parse->buffer_queue, buf);
337 /* if getting buffers from matroska, we won't have a granpos here... */
338 //if (GST_BUFFER_OFFSET_END_IS_VALID (buf)) {
339 ret = gst_kate_parse_drain_queue (parse, granpos);
343 ret = gst_kate_parse_drain_queue (parse, granpos);
345 GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL),
346 ("Bad granulepos %" G_GINT64_FORMAT, granpos));
347 ret = GST_FLOW_ERROR;
355 gst_kate_parse_parse_packet (GstKateParse * parse, GstBuffer * buf)
357 GstFlowReturn ret = GST_FLOW_OK;
365 size = gst_buffer_extract (buf, 0, header, 1);
367 GST_LOG_OBJECT (parse, "Got packet %02x, %" G_GSIZE_FORMAT " bytes",
368 size ? header[0] : -1, gst_buffer_get_size (buf));
370 if (size > 0 && header[0] & 0x80) {
371 GST_DEBUG_OBJECT (parse, "Found header %02x", header[0]);
372 /* if 0x80 is set, it's streamheader,
373 * so put it on the streamheader list and return */
374 parse->streamheader = g_list_append (parse->streamheader, buf);
377 if (!parse->streamheader_sent) {
378 GST_DEBUG_OBJECT (parse, "Found non header, pushing headers seen so far");
379 ret = gst_kate_parse_push_headers (parse);
382 if (ret == GST_FLOW_OK) {
383 ret = gst_kate_parse_queue_buffer (parse, buf);
391 gst_kate_parse_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
393 GstKateParseClass *klass;
396 parse = GST_KATE_PARSE (parent);
397 klass = GST_KATE_PARSE_CLASS (G_OBJECT_GET_CLASS (parse));
399 g_assert (klass->parse_packet != NULL);
401 if (G_UNLIKELY (!gst_pad_has_current_caps (pad)))
402 return GST_FLOW_NOT_NEGOTIATED;
404 return klass->parse_packet (parse, buffer);
408 gst_kate_parse_queue_event (GstKateParse * parse, GstEvent * event)
410 GstFlowReturn ret = TRUE;
412 g_queue_push_tail (parse->event_queue, event);
418 gst_kate_parse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
423 parse = GST_KATE_PARSE (parent);
425 switch (GST_EVENT_TYPE (event)) {
426 case GST_EVENT_FLUSH_STOP:
427 gst_kate_parse_clear_queue (parse);
428 ret = gst_pad_event_default (pad, parent, event);
431 if (!parse->streamheader_sent) {
432 GST_DEBUG_OBJECT (parse, "Got EOS, pushing headers seen so far");
433 ret = gst_kate_parse_push_headers (parse);
434 if (ret != GST_FLOW_OK)
437 gst_kate_parse_drain_queue_prematurely (parse);
438 ret = gst_pad_event_default (pad, parent, event);
441 if (!parse->streamheader_sent && GST_EVENT_IS_SERIALIZED (event)
442 && GST_EVENT_TYPE (event) > GST_EVENT_CAPS)
443 ret = gst_kate_parse_queue_event (parse, event);
445 ret = gst_pad_event_default (pad, parent, event);
454 gst_kate_parse_convert (GstPad * pad,
455 GstFormat src_format, gint64 src_value,
456 GstFormat * dest_format, gint64 * dest_value)
461 parse = GST_KATE_PARSE (GST_PAD_PARENT (pad));
463 /* fixme: assumes atomic access to lots of instance variables modified from
464 * the streaming thread, including 64-bit variables */
466 if (!parse->streamheader_sent)
469 if (src_format == *dest_format) {
470 *dest_value = src_value;
474 if (parse->sinkpad == pad &&
475 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
478 switch (src_format) {
479 case GST_FORMAT_TIME:
480 switch (*dest_format) {
485 case GST_FORMAT_DEFAULT:
486 switch (*dest_format) {
487 case GST_FORMAT_TIME:
488 *dest_value = kate_granule_time (&parse->ki, src_value) * GST_SECOND;
503 gst_kate_parse_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
507 GST_WARNING ("gst_kate_parse_src_query");
512 gboolean res = FALSE;
514 parse = GST_KATE_PARSE (parent);
516 switch (GST_QUERY_TYPE (query)) {
517 case GST_QUERY_POSITION:
522 granulepos = parse->prev_granulepos;
524 gst_query_parse_position (query, &format, NULL);
526 /* and convert to the final format */
528 gst_kate_parse_convert (pad, GST_FORMAT_DEFAULT, granulepos,
532 /* fixme: support segments
533 value = (value - parse->segment_start) + parse->segment_time;
536 gst_query_set_position (query, format, value);
538 GST_LOG_OBJECT (parse, "query %p: peer returned granulepos: %"
539 G_GUINT64_FORMAT " - we return %" G_GUINT64_FORMAT " (format %u)",
540 query, granulepos, value, format);
544 case GST_QUERY_DURATION:
546 /* fixme: not threadsafe */
547 /* query peer for total length */
548 if (!gst_pad_is_linked (parse->sinkpad)) {
549 GST_WARNING_OBJECT (parse, "sink pad %" GST_PTR_FORMAT " is not linked",
553 if (!(res = gst_pad_query (GST_PAD_PEER (parse->sinkpad), query)))
557 case GST_QUERY_CONVERT:
559 GstFormat src_fmt, dest_fmt;
560 gint64 src_val, dest_val;
562 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
564 gst_kate_parse_convert (pad, src_fmt, src_val, &dest_fmt,
567 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
571 res = gst_pad_query_default (pad, query);
578 GST_WARNING_OBJECT (parse, "error handling query");
585 gst_kate_parse_free_stream_headers (GstKateParse * parse)
587 while (parse->streamheader != NULL) {
588 gst_buffer_unref (GST_BUFFER (parse->streamheader->data));
589 parse->streamheader = g_list_delete_link (parse->streamheader,
590 parse->streamheader);
594 static GstStateChangeReturn
595 gst_kate_parse_change_state (GstElement * element, GstStateChange transition)
597 GstKateParse *parse = GST_KATE_PARSE (element);
598 GstStateChangeReturn ret;
600 switch (transition) {
601 case GST_STATE_CHANGE_READY_TO_PAUSED:
602 kate_info_init (&parse->ki);
603 kate_comment_init (&parse->kc);
605 parse->streamheader_sent = FALSE;
606 parse->streamheader = NULL;
607 parse->buffer_queue = g_queue_new ();
608 parse->event_queue = g_queue_new ();
614 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
616 switch (transition) {
617 case GST_STATE_CHANGE_PAUSED_TO_READY:
618 kate_info_clear (&parse->ki);
619 kate_comment_clear (&parse->kc);
621 gst_kate_parse_clear_queue (parse);
622 g_queue_free (parse->buffer_queue);
623 parse->buffer_queue = NULL;
624 g_queue_free (parse->event_queue);
625 parse->event_queue = NULL;
626 gst_kate_parse_free_stream_headers (parse);