Commit 3e0bc017 authored by Wim Taymans's avatar Wim Taymans

ext/theora/theoradec.c: Refactor a bit, use STREAM_LOCK.

Original commit message from CVS:
* ext/theora/theoradec.c: (_inc_granulepos),
(theora_dec_sink_event), (theora_handle_comment_packet),
(theora_handle_type_packet), (theora_handle_header_packet),
(theora_handle_data_packet), (theora_dec_chain),
(theora_dec_change_state):
Refactor a bit, use STREAM_LOCK.
parent 851547e3
2005-04-28 Wim Taymans <wim@fluendo.com>
* ext/theora/theoradec.c: (_inc_granulepos),
(theora_dec_sink_event), (theora_handle_comment_packet),
(theora_handle_type_packet), (theora_handle_header_packet),
(theora_handle_data_packet), (theora_dec_chain),
(theora_dec_change_state):
Refactor a bit, use STREAM_LOCK.
2005-04-28 Wim Taymans <wim@fluendo.com>
* ext/alsa/Makefile.am:
......
......@@ -481,6 +481,7 @@ static gboolean
theora_dec_sink_event (GstPad * pad, GstEvent * event)
{
guint64 start_value, end_value, time, bytes;
gboolean ret = TRUE;
GstTheoraDec *dec;
dec = GST_THEORA_DEC (GST_PAD_PARENT (pad));
......@@ -534,11 +535,13 @@ theora_dec_sink_event (GstPad * pad, GstEvent * event)
/* sync to keyframe */
dec->need_keyframe = TRUE;
}
gst_event_unref (event);
break;
default:
ret = gst_pad_event_default (dec->sinkpad, event);
break;
}
return gst_pad_event_default (dec->sinkpad, event);
return ret;
}
#define ROUND_UP_2(x) (((x) + 1) & ~1)
......@@ -546,126 +549,25 @@ theora_dec_sink_event (GstPad * pad, GstEvent * event)
#define ROUND_UP_8(x) (((x) + 7) & ~7)
static GstFlowReturn
theora_dec_chain (GstPad * pad, GstBuffer * buffer)
theora_handle_comment_packet (GstTheoraDec * dec, ogg_packet * packet)
{
gchar *encoder = NULL;
GstBuffer *buf;
GstTheoraDec *dec;
ogg_packet packet;
guint64 offset_end;
GstClockTime outtime;
GstFlowReturn result = GST_FLOW_OK;
dec = GST_THEORA_DEC (GST_PAD_PARENT (pad));
buf = GST_BUFFER (buffer);
if (dec->packetno >= 3) {
/* try timestamp first */
outtime = GST_BUFFER_TIMESTAMP (buf);
if (outtime == -1) {
gboolean need_flush = FALSE;
/* the offset end field in theora is actually the time of
* this frame, not the end time */
offset_end = GST_BUFFER_OFFSET_END (buf);
GST_DEBUG_OBJECT (dec, "got buffer with granule %lld", offset_end);
if (offset_end != -1) {
if (dec->granulepos == -1) {
GST_DEBUG_OBJECT (dec, "first buffer with granule");
need_flush = TRUE;
}
dec->granulepos = offset_end;
}
if (dec->granulepos == -1) {
/* we need to wait for a buffer with at least a timestamp or an
* offset before we can generate valid timestamps */
dec->queued = g_list_append (dec->queued, buf);
GST_DEBUG_OBJECT (dec, "queued buffer");
return GST_FLOW_OK;
} else {
/* granulepos to time */
outtime =
GST_SECOND * theora_granule_time (&dec->state, dec->granulepos);
GST_DEBUG_OBJECT (dec, "granulepos=%lld outtime=%" GST_TIME_FORMAT,
dec->granulepos, GST_TIME_ARGS (outtime));
if (need_flush) {
GList *walk;
GstClockTime patch;
gint64 len;
gint64 old_granule = dec->granulepos;
dec->granulepos = -1;
len = g_list_length (dec->queued);
GST_DEBUG_OBJECT (dec, "first buffer with granule, flushing");
/* now resubmit all queued buffers with corrected timestamps */
for (walk = dec->queued; walk; walk = g_list_next (walk)) {
GstBuffer *qbuffer = GST_BUFFER (walk->data);
GstTagList *list;
patch = outtime - (GST_SECOND * len * dec->info.fps_denominator) /
dec->info.fps_numerator,
GST_DEBUG_OBJECT (dec, "patch buffer %lld %lld", len, patch);
GST_BUFFER_TIMESTAMP (qbuffer) = patch;
GST_DEBUG ("parsing comment packet");
/* buffers are unreffed in chain function */
theora_dec_chain (pad, qbuffer);
len--;
}
g_list_free (dec->queued);
dec->queued = NULL;
buf = gst_buffer_new_and_alloc (packet->bytes);
GST_BUFFER_DATA (buf) = packet->packet;
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_DONTFREE);
dec->granulepos = old_granule;
}
}
} else {
GST_DEBUG_OBJECT (dec, "got buffer with timestamp %lld", outtime);
}
} else {
/* we don't know yet */
outtime = -1;
}
/* make ogg_packet out of the buffer */
packet.packet = GST_BUFFER_DATA (buf);
packet.bytes = GST_BUFFER_SIZE (buf);
packet.granulepos = dec->granulepos;
packet.packetno = dec->packetno;
packet.b_o_s = (packet.packetno == 0) ? 1 : 0;
packet.e_o_s = 0;
GST_DEBUG_OBJECT (dec, "header=%d packetno=%lld, outtime=%" GST_TIME_FORMAT,
packet.packet[0], packet.packetno, GST_TIME_ARGS (outtime));
/* switch depending on packet type */
if (packet.packet[0] & 0x80) {
if (packet.packetno > 3) {
GST_WARNING_OBJECT (GST_OBJECT (dec), "Ignoring header");
goto done;
}
/* header packet */
if (theora_decode_header (&dec->info, &dec->comment, &packet)) {
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't read header packet"));
result = GST_FLOW_ERROR;
goto done;
}
if (packet.packetno == 0) {
dec->packetno++;
} else if (packet.packetno == 1) {
gchar *encoder = NULL;
GstTagList *list =
gst_tag_list_from_vorbiscomment_buffer (buf, "\201theora", 7,
list = gst_tag_list_from_vorbiscomment_buffer (buf, "\201theora", 7,
&encoder);
gst_buffer_unref (buf);
if (!list) {
GST_ERROR_OBJECT (dec, "failed to parse tags");
GST_ERROR_OBJECT (dec, "couldn't decode comments");
list = gst_tag_list_new ();
}
if (encoder) {
......@@ -677,10 +579,15 @@ theora_dec_chain (GstPad * pad, GstBuffer * buffer)
GST_TAG_ENCODER_VERSION, dec->info.version_major,
GST_TAG_NOMINAL_BITRATE, dec->info.target_bitrate,
GST_TAG_VIDEO_CODEC, "Theora", NULL);
//gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, 0, list);
dec->packetno++;
} else if (packet.packetno == 2) {
return GST_FLOW_OK;
}
static GstFlowReturn
theora_handle_type_packet (GstTheoraDec * dec, ogg_packet * packet)
{
GstCaps *caps;
gint par_num, par_den;
......@@ -695,7 +602,6 @@ theora_dec_chain (GstPad * pad, GstBuffer * buffer)
par_den = dec->info.aspect_denominator;
if (par_num == 0 && par_den == 0) {
par_num = par_den = 1;
}
/* theora has:
*
......@@ -712,8 +618,7 @@ theora_dec_chain (GstPad * pad, GstBuffer * buffer)
if (dec->crop) {
/* add black borders to make width/height/offsets even. we need this because
* we cannot express an offset to the peer plugin. */
dec->width =
ROUND_UP_2 (dec->info.frame_width + (dec->info.offset_x & 1));
dec->width = ROUND_UP_2 (dec->info.frame_width + (dec->info.offset_x & 1));
dec->height =
ROUND_UP_2 (dec->info.frame_height + (dec->info.offset_y & 1));
dec->offset_x = dec->info.offset_x & ~1;
......@@ -737,15 +642,52 @@ theora_dec_chain (GstPad * pad, GstBuffer * buffer)
"framerate", G_TYPE_DOUBLE,
((gdouble) dec->info.fps_numerator) / dec->info.fps_denominator,
"pixel-aspect-ratio", GST_TYPE_FRACTION, par_num, par_den,
"width", G_TYPE_INT, dec->width, "height", G_TYPE_INT,
dec->height, NULL);
"width", G_TYPE_INT, dec->width, "height", G_TYPE_INT, dec->height, NULL);
gst_pad_set_caps (dec->srcpad, caps);
gst_caps_unref (caps);
dec->initialized = TRUE;
dec->packetno++;
return GST_FLOW_OK;
}
static GstFlowReturn
theora_handle_header_packet (GstTheoraDec * dec, ogg_packet * packet)
{
GstFlowReturn res;
GST_DEBUG ("parsing header packet");
if (theora_decode_header (&dec->info, &dec->comment, packet))
goto header_read_error;
switch (packet->packetno) {
case 1:
res = theora_handle_comment_packet (dec, packet);
break;
case 2:
res = theora_handle_type_packet (dec, packet);
break;
default:
/* ignore */
res = GST_FLOW_OK;
break;
}
} else {
return res;
/* ERRORS */
header_read_error:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't read header packet"));
return GST_FLOW_ERROR;
}
}
static GstFlowReturn
theora_handle_data_packet (GstTheoraDec * dec, ogg_packet * packet,
GstClockTime outtime)
{
/* normal data packet */
yuv_buffer yuv;
GstBuffer *out;
......@@ -755,39 +697,29 @@ theora_dec_chain (GstPad * pad, GstBuffer * buffer)
gint stride_y, stride_uv;
gint width, height;
gint cwidth, cheight;
GstFlowReturn result;
dec->packetno++;
if (!dec->initialized) {
result = GST_FLOW_ERROR;
goto done;
}
if (!dec->initialized)
goto not_initialized;
/* the second most significant bit of the first data byte is cleared
* for keyframes */
keyframe = (packet.packet[0] & 0x40) == 0;
keyframe = (packet->packet[0] & 0x40) == 0;
if (keyframe) {
dec->need_keyframe = FALSE;
} else if (dec->need_keyframe) {
GST_WARNING_OBJECT (dec, "dropping frame because we need a keyframe");
/* drop frames if we're looking for a keyframe */
goto done;
}
if (theora_decode_packetin (&dec->state, &packet)) {
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("theora decoder did not read data packet"));
result = GST_FLOW_ERROR;
goto done;
}
if (theora_decode_YUVout (&dec->state, &yuv) < 0) {
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't read out YUV image"));
result = GST_FLOW_ERROR;
goto done;
goto dropping;
}
g_return_val_if_fail (yuv.y_width == dec->info.width, GST_FLOW_ERROR);
g_return_val_if_fail (yuv.y_height == dec->info.height, GST_FLOW_ERROR);
if (theora_decode_packetin (&dec->state, packet))
goto could_not_read;
if (theora_decode_YUVout (&dec->state, &yuv) < 0)
goto decode_error;
if ((yuv.y_width != dec->info.width) || (yuv.y_height != dec->info.height))
goto wrong_dimensions;
width = dec->width;
height = dec->height;
......@@ -807,7 +739,7 @@ theora_dec_chain (GstPad * pad, GstBuffer * buffer)
out = gst_pad_alloc_buffer (dec->srcpad, GST_BUFFER_OFFSET_NONE, out_size,
GST_PAD_CAPS (dec->srcpad));
if (out == NULL)
goto done;
goto no_buffer;
/* copy the visible region to the destination. This is actually pretty
* complicated and gstreamer doesn't support all the needed caps to do this
......@@ -856,10 +788,159 @@ theora_dec_chain (GstPad * pad, GstBuffer * buffer)
GST_BUFFER_TIMESTAMP (out) = outtime;
result = gst_pad_push (dec->srcpad, out);
return result;
/* ERRORS */
not_initialized:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("no header sent yet (packet no is %d)", packet->packetno));
return GST_FLOW_ERROR;
}
dropping:
{
GST_WARNING_OBJECT (dec, "dropping frame because we need a keyframe");
return GST_FLOW_OK;
}
could_not_read:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("theora decoder did not read data packet"));
return GST_FLOW_ERROR;
}
decode_error:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't read out YUV image"));
return GST_FLOW_ERROR;
}
wrong_dimensions:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("dimensions of image do not match header"));
return GST_FLOW_ERROR;
}
no_buffer:
{
return GST_FLOW_OK;
}
}
static GstFlowReturn
theora_dec_chain (GstPad * pad, GstBuffer * buf)
{
GstTheoraDec *dec;
ogg_packet packet;
guint64 offset_end;
GstClockTime outtime;
GstFlowReturn result = GST_FLOW_OK;
dec = GST_THEORA_DEC (GST_PAD_PARENT (pad));
GST_STREAM_LOCK (pad);
if (dec->packetno >= 3) {
/* try timestamp first */
outtime = GST_BUFFER_TIMESTAMP (buf);
if (outtime == -1) {
gboolean need_flush = FALSE;
/* the offset end field in theora is actually the time of
* this frame, not the end time */
offset_end = GST_BUFFER_OFFSET_END (buf);
GST_DEBUG_OBJECT (dec, "got buffer with granule %lld", offset_end);
if (offset_end != -1) {
if (dec->granulepos == -1) {
GST_DEBUG_OBJECT (dec, "first buffer with granule");
need_flush = TRUE;
}
dec->granulepos = offset_end;
}
if (dec->granulepos == -1) {
/* we need to wait for a buffer with at least a timestamp or an
* offset before we can generate valid timestamps */
dec->queued = g_list_append (dec->queued, buf);
GST_DEBUG_OBJECT (dec, "queued buffer");
GST_STREAM_UNLOCK (pad);
return GST_FLOW_OK;
} else {
/* granulepos to time */
outtime =
GST_SECOND * theora_granule_time (&dec->state, dec->granulepos);
GST_DEBUG_OBJECT (dec, "granulepos=%lld outtime=%" GST_TIME_FORMAT,
dec->granulepos, GST_TIME_ARGS (outtime));
if (need_flush) {
GList *walk;
GstClockTime patch;
gint64 len;
gint64 old_granule = dec->granulepos;
dec->granulepos = -1;
len = g_list_length (dec->queued);
GST_DEBUG_OBJECT (dec, "first buffer with granule, flushing");
/* now resubmit all queued buffers with corrected timestamps */
for (walk = dec->queued; walk; walk = g_list_next (walk)) {
GstBuffer *qbuffer = GST_BUFFER (walk->data);
patch = outtime - (GST_SECOND * len * dec->info.fps_denominator) /
dec->info.fps_numerator,
GST_DEBUG_OBJECT (dec, "patch buffer %lld %lld", len, patch);
GST_BUFFER_TIMESTAMP (qbuffer) = patch;
/* buffers are unreffed in chain function */
theora_dec_chain (pad, qbuffer);
len--;
}
g_list_free (dec->queued);
dec->queued = NULL;
dec->granulepos = old_granule;
}
}
} else {
GST_DEBUG_OBJECT (dec, "got buffer with timestamp %lld", outtime);
}
} else {
/* we don't know yet */
outtime = -1;
}
/* make ogg_packet out of the buffer */
packet.packet = GST_BUFFER_DATA (buf);
packet.bytes = GST_BUFFER_SIZE (buf);
packet.granulepos = dec->granulepos;
packet.packetno = dec->packetno;
packet.b_o_s = (packet.packetno == 0) ? 1 : 0;
packet.e_o_s = 0;
GST_DEBUG_OBJECT (dec, "header=%d packetno=%lld, outtime=%" GST_TIME_FORMAT,
packet.packet[0], packet.packetno, GST_TIME_ARGS (outtime));
/* switch depending on packet type */
if (packet.packet[0] & 0x80) {
if (packet.packetno > 3) {
GST_WARNING_OBJECT (GST_OBJECT (dec), "Ignoring header");
goto done;
}
result = theora_handle_header_packet (dec, &packet);
} else {
result = theora_handle_data_packet (dec, &packet, outtime);
}
done:
gst_buffer_unref (buffer);
dec->packetno++;
_inc_granulepos (dec);
GST_STREAM_UNLOCK (pad);
gst_buffer_unref (buf);
return result;
}
......@@ -884,11 +965,13 @@ theora_dec_change_state (GstElement * element)
case GST_STATE_PLAYING_TO_PAUSED:
break;
case GST_STATE_PAUSED_TO_READY:
GST_STREAM_LOCK (dec->sinkpad);
theora_clear (&dec->state);
theora_comment_clear (&dec->comment);
theora_info_clear (&dec->info);
dec->packetno = 0;
dec->granulepos = -1;
GST_STREAM_UNLOCK (dec->sinkpad);
break;
case GST_STATE_READY_TO_NULL:
break;
......
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