static gboolean gst_interlace_src_query (GstPad * pad, GstObject * parent, GstQuery * query) { gboolean ret; GstInterlace *interlace; interlace = GST_INTERLACE (parent); switch (GST_QUERY_TYPE (query)) { case GST_QUERY_CAPS: { GstCaps *filter, *caps; gst_query_parse_caps (query, &filter); caps = gst_interlace_getcaps (pad, interlace, filter); gst_query_set_caps_result (query, caps); gst_caps_unref (caps); ret = TRUE; break; } default: ret = gst_pad_query_default (pad, parent, query); break; } return ret; }
static GstCaps * gst_interlace_getcaps (GstPad * pad) { GstInterlace *interlace; GstPad *otherpad; GstCaps *othercaps; GstCaps *icaps; interlace = GST_INTERLACE (gst_pad_get_parent (pad)); otherpad = (pad == interlace->srcpad) ? interlace->sinkpad : interlace->srcpad; othercaps = gst_pad_peer_get_caps (otherpad); if (othercaps == NULL) { icaps = gst_caps_copy (gst_pad_get_pad_template_caps (otherpad)); } else { icaps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (otherpad)); gst_caps_unref (othercaps); } gst_caps_set_simple (icaps, "interlaced", G_TYPE_BOOLEAN, pad == interlace->srcpad ? TRUE : FALSE, NULL); return icaps; }
static void gst_interlace_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstInterlace *interlace = GST_INTERLACE (object); switch (prop_id) { case PROP_TOP_FIELD_FIRST: g_value_set_boolean (value, interlace->top_field_first); break; case PROP_PATTERN: g_value_set_enum (value, interlace->pattern); break; case PROP_PATTERN_OFFSET: g_value_set_uint (value, interlace->pattern_offset); break; case PROP_ALLOW_RFF: g_value_set_boolean (value, interlace->allow_rff); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static GstFlowReturn gst_interlace_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer) { GstInterlace *interlace = GST_INTERLACE (parent); GstFlowReturn ret = GST_FLOW_OK; gint num_fields = 0; int current_fields; const PulldownFormat *format; GST_DEBUG ("Received buffer at %u:%02u:%02u:%09u", (guint) (GST_BUFFER_TIMESTAMP (buffer) / (GST_SECOND * 60 * 60)), (guint) ((GST_BUFFER_TIMESTAMP (buffer) / (GST_SECOND * 60)) % 60), (guint) ((GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND) % 60), (guint) (GST_BUFFER_TIMESTAMP (buffer) % GST_SECOND)); GST_DEBUG ("duration %" GST_TIME_FORMAT " flags %04x %s %s %s", GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)), GST_BUFFER_FLAGS (buffer), (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_TFF) ? "tff" : "", (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" : "", (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ? "onefield" : ""); if (GST_BUFFER_FLAGS (buffer) & GST_BUFFER_FLAG_DISCONT) { GST_DEBUG ("discont"); if (interlace->stored_frame) { gst_buffer_unref (interlace->stored_frame); } interlace->stored_frame = NULL; interlace->stored_fields = 0; if (interlace->top_field_first) { interlace->field_index = 0; } else { interlace->field_index = 1; } } if (interlace->timebase == GST_CLOCK_TIME_NONE) { /* get the initial ts */ interlace->timebase = GST_BUFFER_TIMESTAMP (buffer); } format = &formats[interlace->pattern]; if (interlace->stored_fields == 0 && interlace->phase_index == interlace->pattern_offset && GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer))) { interlace->timebase = GST_BUFFER_TIMESTAMP (buffer); interlace->fields_since_timebase = 0; } if (!format->n_fields[interlace->phase_index]) { interlace->phase_index = 0; } current_fields = format->n_fields[interlace->phase_index]; /* increment the phase index */ interlace->phase_index++; GST_DEBUG ("incoming buffer assigned %d fields", current_fields); num_fields = interlace->stored_fields + current_fields; while (num_fields >= 2) { GstBuffer *output_buffer; int n_output_fields; gboolean interlaced = FALSE; GST_DEBUG ("have %d fields, %d current, %d stored", num_fields, current_fields, interlace->stored_fields); if (interlace->stored_fields > 0) { GST_DEBUG ("1 field from stored, 1 from current"); output_buffer = gst_buffer_new_and_alloc (gst_buffer_get_size (buffer)); /* take the first field from the stored frame */ copy_field (interlace, output_buffer, interlace->stored_frame, interlace->field_index); interlace->stored_fields--; /* take the second field from the incoming buffer */ copy_field (interlace, output_buffer, buffer, interlace->field_index ^ 1); current_fields--; n_output_fields = 2; interlaced = TRUE; } else { output_buffer = gst_buffer_make_writable (gst_buffer_ref (buffer)); if (num_fields >= 3 && interlace->allow_rff) { GST_DEBUG ("3 fields from current"); /* take both fields from incoming buffer */ current_fields -= 3; n_output_fields = 3; } else { GST_DEBUG ("2 fields from current"); /* take both buffers from incoming buffer */ current_fields -= 2; n_output_fields = 2; } } num_fields -= n_output_fields; gst_interlace_decorate_buffer (interlace, output_buffer, n_output_fields, interlaced); interlace->fields_since_timebase += n_output_fields; interlace->field_index ^= (n_output_fields & 1); GST_DEBUG_OBJECT (interlace, "output timestamp %" GST_TIME_FORMAT " duration %" GST_TIME_FORMAT " flags %04x %s %s %s", GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (output_buffer)), GST_TIME_ARGS (GST_BUFFER_DURATION (output_buffer)), GST_BUFFER_FLAGS (output_buffer), (GST_BUFFER_FLAGS (output_buffer) & GST_VIDEO_BUFFER_FLAG_TFF) ? "tff" : "", (GST_BUFFER_FLAGS (output_buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" : "", (GST_BUFFER_FLAGS (output_buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ? "onefield" : ""); ret = gst_pad_push (interlace->srcpad, output_buffer); if (ret != GST_FLOW_OK) { GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p", output_buffer); break; } } GST_DEBUG ("done. %d fields remaining", current_fields); if (interlace->stored_frame) { gst_buffer_unref (interlace->stored_frame); interlace->stored_frame = NULL; interlace->stored_fields = 0; } if (current_fields > 0) { interlace->stored_frame = buffer; interlace->stored_fields = current_fields; } else { gst_buffer_unref (buffer); } return ret; }
static gboolean gst_interlace_sink_event (GstPad * pad, GstObject * parent, GstEvent * event) { gboolean ret; GstInterlace *interlace; interlace = GST_INTERLACE (parent); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_FLUSH_START: GST_DEBUG_OBJECT (interlace, "handling FLUSH_START"); ret = gst_pad_push_event (interlace->srcpad, event); break; case GST_EVENT_FLUSH_STOP: GST_DEBUG_OBJECT (interlace, "handling FLUSH_STOP"); gst_interlace_reset (interlace); ret = gst_pad_push_event (interlace->srcpad, event); break; case GST_EVENT_EOS: #if 0 /* FIXME revive this when we output ONEFIELD and RFF buffers */ { gint num_fields; const PulldownFormat *format = &formats[interlace->pattern]; num_fields = format->n_fields[interlace->phase_index] - interlace->stored_fields_pushed; interlace->stored_fields_pushed = 0; /* on EOS we want to push as many sane frames as are left */ while (num_fields > 1) { GstBuffer *output_buffer; /* make metadata writable before editing it */ interlace->stored_frame = gst_buffer_make_metadata_writable (interlace->stored_frame); num_fields -= 2; gst_interlace_decorate_buffer (interlace, interlace->stored_frame, n_fields, FALSE); /* ref output_buffer/stored frame because we want to keep it for now * and pushing gives away a ref */ output_buffer = gst_buffer_ref (interlace->stored_frame); if (gst_pad_push (interlace->srcpad, output_buffer)) { GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p", output_buffer); return FALSE; } output_buffer = NULL; if (num_fields <= 1) { gst_buffer_unref (interlace->stored_frame); interlace->stored_frame = NULL; break; } } /* increment the phase index */ interlace->phase_index++; if (!format->n_fields[interlace->phase_index]) { interlace->phase_index = 0; } } #endif ret = gst_pad_push_event (interlace->srcpad, event); break; case GST_EVENT_CAPS: { GstCaps *caps; gst_event_parse_caps (event, &caps); ret = gst_interlace_setcaps (interlace, caps); gst_event_unref (event); break; } default: ret = gst_pad_push_event (interlace->srcpad, event); break; } return ret; }
static gboolean gst_interlace_setcaps (GstPad * pad, GstCaps * caps) { GstInterlace *interlace; gboolean ret; int width, height; GstVideoFormat format; gboolean interlaced = TRUE; int fps_n, fps_d; GstPad *otherpad; GstCaps *othercaps; const PulldownFormat *pdformat; interlace = GST_INTERLACE (gst_pad_get_parent (pad)); otherpad = (pad == interlace->srcpad) ? interlace->sinkpad : interlace->srcpad; ret = gst_video_format_parse_caps (caps, &format, &width, &height); gst_video_format_parse_caps_interlaced (caps, &interlaced); ret &= gst_video_parse_caps_framerate (caps, &fps_n, &fps_d); if (!ret) goto error; othercaps = gst_caps_copy (caps); pdformat = &formats[interlace->pattern]; if (pad == interlace->srcpad) { gst_caps_set_simple (othercaps, "interlaced", G_TYPE_BOOLEAN, FALSE, NULL); gst_caps_set_simple (othercaps, "framerate", GST_TYPE_FRACTION, fps_n * pdformat->ratio_d, fps_d * pdformat->ratio_n, NULL); } else { gst_caps_set_simple (othercaps, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL); gst_caps_set_simple (othercaps, "framerate", GST_TYPE_FRACTION, fps_n * pdformat->ratio_n, fps_d * pdformat->ratio_d, NULL); } ret = gst_pad_set_caps (otherpad, othercaps); if (!ret) goto error; interlace->format = format; interlace->width = width; interlace->height = height; interlace->phase_index = interlace->pattern_offset; if (pad == interlace->sinkpad) { gst_caps_replace (&interlace->srccaps, othercaps); interlace->src_fps_n = fps_n * pdformat->ratio_n; interlace->src_fps_d = fps_d * pdformat->ratio_d; } else { gst_caps_replace (&interlace->srccaps, caps); interlace->src_fps_n = fps_n; interlace->src_fps_d = fps_d; } error: g_object_unref (interlace); return ret; }