melunko@47: /* GStreamer melunko@47: * Copyright (C) <2006> Renato Araujo Oliveira Filho melunko@47: * Rosfran Borges melunko@47: * melunko@47: * This library is free software; you can redistribute it and/or melunko@47: * modify it under the terms of the GNU Library General Public melunko@47: * License as published by the Free Software Foundation; either melunko@47: * version 2 of the License, or (at your option) any later version. melunko@47: * melunko@47: * This library is distributed in the hope that it will be useful, melunko@47: * but WITHOUT ANY WARRANTY; without even the implied warranty of melunko@47: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU melunko@47: * Library General Public License for more details. melunko@47: * melunko@47: * You should have received a copy of the GNU Library General Public melunko@47: * License along with this library; if not, write to the melunko@47: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, melunko@47: * Boston, MA 02111-1307, USA. melunko@47: */ melunko@47: /* Element-Checklist-Version: 5 */ melunko@47: melunko@47: /** melunko@47: * SECTION:element-nuvdemux melunko@47: * melunko@47: * melunko@47: * melunko@47: * Demuxes an .nuv file into raw or compressed audio and/or video streams. melunko@47: * melunko@47: * melunko@47: * This element currently only supports pull-based scheduling. melunko@47: * melunko@47: * Example launch line melunko@47: * melunko@47: * melunko@47: * gst-launch filesrc test.nuv ! nuvdemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink melunko@47: * melunko@47: * Play (parse and decode) an .nuv file and try to output it to melunko@47: * an automatically detected soundcard and videosink. If the NUV file contains melunko@47: * compressed audio or video data, this will only work if you have the melunko@47: * right decoder elements/plugins installed. melunko@47: * melunko@47: * melunko@47: * melunko@47: */ melunko@47: melunko@47: #ifdef HAVE_CONFIG_H melunko@47: #include "config.h" melunko@47: #endif melunko@47: melunko@47: #include melunko@47: #include melunko@47: #include melunko@47: #include melunko@47: melunko@47: //#include "gst/gst-i18n-plugin.h" melunko@47: #include melunko@47: #include "gstnuvdemux.h" melunko@47: melunko@47: GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug); melunko@47: #define GST_CAT_DEFAULT nuvdemux_debug melunko@47: melunko@47: melunko@47: #define GST_FLOW_ERROR_NO_DATA -101 melunko@47: melunko@47: GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT); melunko@47: melunko@47: static const GstElementDetails gst_nuv_demux_details = melunko@47: GST_ELEMENT_DETAILS ("Nuv demuxer", melunko@47: "Codec/Demuxer", melunko@47: "Demultiplex a .nuv file into audio and video", melunko@47: "Renato Araujo Oliveira Filho ," melunko@47: "Rosfran Borges "); melunko@47: melunko@47: static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", melunko@47: GST_PAD_SINK, melunko@47: GST_PAD_ALWAYS, melunko@47: GST_STATIC_CAPS ("video/x-nuv")); melunko@47: melunko@47: static GstStaticPadTemplate audio_src_template = melunko@47: GST_STATIC_PAD_TEMPLATE ("audio_src", melunko@47: GST_PAD_SRC, melunko@47: GST_PAD_SOMETIMES, melunko@47: GST_STATIC_CAPS_ANY); melunko@47: melunko@47: static GstStaticPadTemplate video_src_template = melunko@47: GST_STATIC_PAD_TEMPLATE ("video_src", melunko@47: GST_PAD_SRC, melunko@47: GST_PAD_SOMETIMES, melunko@47: GST_STATIC_CAPS_ANY); melunko@47: melunko@47: /* NUV Demux indexes init/dispose callers */ melunko@47: static void gst_nuv_demux_finalize (GObject * object); melunko@47: static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element, melunko@47: GstStateChange transition); melunko@47: static void gst_nuv_demux_loop (GstPad * pad); melunko@47: static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf); melunko@47: static GstFlowReturn gst_nuv_demux_play (GstPad * pad); melunko@47: static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, melunko@47: gboolean active); melunko@47: static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad); melunko@47: static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, melunko@47: gboolean move, GstBuffer ** buffer); melunko@47: static void gst_nuv_demux_reset (GstNuvDemux * nuv); melunko@47: static gboolean gst_nuv_demux_handle_sink_event (GstPad * sinkpad, melunko@47: GstEvent * event); melunko@47: static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv); melunko@47: static void gst_nuv_demux_send_eos (GstNuvDemux * nuv); melunko@47: melunko@47: /* GObject methods */ melunko@47: GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT); melunko@47: melunko@47: #if G_BYTE_ORDER == G_BIG_ENDIAN melunko@47: static inline gdouble melunko@47: _gdouble_swap_le_be (gdouble * d) melunko@47: { melunko@47: union melunko@47: { melunko@47: guint64 i; melunko@47: gdouble d; melunko@47: } u; melunko@47: melunko@47: u.d = *d; melunko@47: u.i = GUINT64_SWAP_LE_BE (u.i); melunko@47: return u.d; melunko@47: } melunko@47: melunko@47: #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d)) melunko@47: #else /* G_BYTE_ORDER != G_BIG_ENDIAN */ melunko@47: #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d)) melunko@47: #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */ melunko@47: melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_base_init (gpointer klass) melunko@47: { melunko@47: GstElementClass *element_class = GST_ELEMENT_CLASS (klass); melunko@47: melunko@47: gst_element_class_add_pad_template (element_class, melunko@47: gst_static_pad_template_get (&audio_src_template)); melunko@47: melunko@47: gst_element_class_add_pad_template (element_class, melunko@47: gst_static_pad_template_get (&video_src_template)); melunko@47: melunko@47: gst_element_class_add_pad_template (element_class, melunko@47: gst_static_pad_template_get (&sink_template)); melunko@47: gst_element_class_set_details (element_class, &gst_nuv_demux_details); melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_class_init (GstNuvDemuxClass * klass) melunko@47: { melunko@47: GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); melunko@47: GObjectClass *gobject_class = (GObjectClass *) klass; melunko@47: melunko@47: GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux", melunko@47: 0, "Demuxer for NUV streams"); melunko@47: melunko@47: parent_class = g_type_class_peek_parent (klass); melunko@47: melunko@47: gobject_class->finalize = gst_nuv_demux_finalize; melunko@47: gstelement_class->change_state = gst_nuv_demux_change_state; melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class) melunko@47: { melunko@47: nuv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink"); melunko@47: melunko@47: gst_pad_set_activate_function (nuv->sinkpad, gst_nuv_demux_sink_activate); melunko@47: melunko@47: gst_pad_set_activatepull_function (nuv->sinkpad, melunko@47: gst_nuv_demux_sink_activate_pull); melunko@47: melunko@47: gst_pad_set_chain_function (nuv->sinkpad, melunko@47: GST_DEBUG_FUNCPTR (gst_nuv_demux_chain)); melunko@47: melunko@47: gst_pad_set_event_function (nuv->sinkpad, gst_nuv_demux_handle_sink_event); melunko@47: melunko@47: gst_element_add_pad (GST_ELEMENT (nuv), nuv->sinkpad); melunko@47: melunko@47: nuv->adapter = NULL; melunko@47: nuv->mpeg_buffer = NULL; melunko@47: nuv->h = NULL; melunko@47: nuv->eh = NULL; melunko@47: nuv->fh = NULL; melunko@47: gst_nuv_demux_reset (nuv); melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_finalize (GObject * object) melunko@47: { melunko@47: GstNuvDemux *nuv = GST_NUV_DEMUX (object); melunko@47: melunko@47: if (nuv->mpeg_buffer != NULL) { melunko@47: gst_buffer_unref (nuv->mpeg_buffer); melunko@47: } melunko@47: melunko@47: gst_nuv_demux_destoy_src_pad (nuv); melunko@47: gst_nuv_demux_reset (nuv); melunko@47: if (nuv->adapter != NULL) { melunko@47: gst_object_unref (nuv->adapter); melunko@47: } melunko@47: G_OBJECT_CLASS (parent_class)->finalize (object); melunko@47: } melunko@47: melunko@47: /***************************************************************************** melunko@47: * Utils fucntions melunko@47: *****************************************************************************/ melunko@47: melunko@47: static gboolean melunko@47: gst_nuv_demux_handle_sink_event (GstPad * sinkpad, GstEvent * event) melunko@47: { melunko@47: gboolean res = FALSE; melunko@47: melunko@47: switch (GST_EVENT_TYPE (event)) { melunko@47: case GST_EVENT_NEWSEGMENT: melunko@47: res = TRUE; melunko@47: break; melunko@47: default: melunko@47: return gst_pad_event_default (sinkpad, event); melunko@47: break; melunko@47: } melunko@47: melunko@47: gst_event_unref (event); melunko@47: return res; melunko@47: } melunko@47: melunko@47: melunko@47: /* HeaderLoad: melunko@47: */ melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header ** h_ret) melunko@47: { melunko@47: GstBuffer *buffer = NULL; melunko@47: GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer); melunko@47: melunko@47: if (res != GST_FLOW_OK) melunko@47: return res; melunko@47: melunko@47: nuv_header *h = g_new0 (nuv_header, 1); melunko@47: melunko@47: memcpy (h->id, buffer->data, 12); melunko@47: memcpy (h->version, buffer->data + 12, 5); melunko@47: h->i_width = GST_READ_UINT32_LE (&buffer->data[20]); melunko@47: h->i_height = GST_READ_UINT32_LE (&buffer->data[24]); melunko@47: h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]); melunko@47: h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]); melunko@47: h->i_mode = buffer->data[36]; melunko@47: h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]); melunko@47: h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]); melunko@47: h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]); melunko@47: h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]); melunko@47: h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]); melunko@47: h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]); melunko@47: melunko@47: GST_DEBUG_OBJECT (nuv, melunko@47: "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id, melunko@47: h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps, melunko@47: h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks, melunko@47: h->i_keyframe_distance); melunko@47: melunko@47: *h_ret = h; melunko@47: gst_buffer_unref (buffer); melunko@47: return res; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_stream_header_data (GstNuvDemux * nuv) melunko@47: { melunko@47: GstFlowReturn res = gst_nuv_demux_header_load (nuv, &nuv->h); melunko@47: melunko@47: if (res == GST_FLOW_OK) melunko@47: nuv->state = GST_NUV_DEMUX_EXTRA_DATA; melunko@47: return res; melunko@47: } melunko@47: melunko@47: /* melunko@47: * Read NUV file tag melunko@47: */ melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_stream_file_header (GstNuvDemux * nuv) melunko@47: { melunko@47: GstFlowReturn res = GST_FLOW_OK; melunko@47: GstBuffer *file_header = NULL; melunko@47: melunko@47: res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header); melunko@47: if (res != GST_FLOW_OK) { melunko@47: return res; melunko@47: } else { melunko@47: if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) || melunko@47: strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) { melunko@47: nuv->state = GST_NUV_DEMUX_HEADER_DATA; melunko@47: } else { melunko@47: GST_DEBUG_OBJECT (nuv, "error parsing file header"); melunko@47: nuv->state = GST_NUV_DEMUX_INVALID_DATA; melunko@47: res = GST_FLOW_ERROR; melunko@47: } melunko@47: } melunko@47: if (file_header != NULL) { melunko@47: gst_buffer_unref (file_header); melunko@47: } melunko@47: return res; melunko@47: } melunko@47: melunko@47: /* FrameHeaderLoad: melunko@47: */ melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header ** h_ret) melunko@47: { melunko@47: unsigned char *data; melunko@47: nuv_frame_header *h; melunko@47: GstBuffer *buf = NULL; melunko@47: melunko@47: GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf); melunko@47: melunko@47: if (res != GST_FLOW_OK) { melunko@47: if (buf != NULL) { melunko@47: gst_buffer_unref (buf); melunko@47: } melunko@47: return res; melunko@47: } melunko@47: melunko@47: h = g_new0 (nuv_frame_header, 1); melunko@47: data = buf->data; melunko@47: melunko@47: h->i_type = data[0]; melunko@47: h->i_compression = data[1]; melunko@47: h->i_keyframe = data[2]; melunko@47: h->i_filters = data[3]; melunko@47: melunko@47: h->i_timecode = GST_READ_UINT32_LE (&data[4]); melunko@47: h->i_length = GST_READ_UINT32_LE (&data[8]); melunko@47: GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%u l=%u", melunko@47: h->i_type, melunko@47: h->i_compression ? h->i_compression : ' ', melunko@47: h->i_keyframe ? h->i_keyframe : ' ', melunko@47: h->i_filters, h->i_timecode, h->i_length); melunko@47: melunko@47: *h_ret = h; melunko@47: gst_buffer_unref (buf); melunko@47: return GST_FLOW_OK; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_extended_header_load (GstNuvDemux * nuv, melunko@47: nuv_extended_header ** h_ret) melunko@47: { melunko@47: unsigned char *data; melunko@47: GstBuffer *buff = NULL; melunko@47: nuv_extended_header *h; melunko@47: melunko@47: melunko@47: GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff); melunko@47: melunko@47: if (res != GST_FLOW_OK) { melunko@47: if (buff != NULL) { melunko@47: gst_buffer_unref (buff); melunko@47: } melunko@47: return res; melunko@47: } melunko@47: melunko@47: h = g_new0 (nuv_extended_header, 1); melunko@47: data = buff->data; melunko@47: h->i_version = GST_READ_UINT32_LE (&data[0]); melunko@47: h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]); melunko@47: h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]); melunko@47: h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]); melunko@47: h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]); melunko@47: h->i_audio_channels = GST_READ_UINT32_LE (&data[20]); melunko@47: h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]); melunko@47: h->i_audio_quality = GST_READ_UINT32_LE (&data[28]); melunko@47: h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]); melunko@47: h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]); melunko@47: h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]); melunko@47: h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]); melunko@47: h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]); melunko@47: h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]); melunko@47: h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]); melunko@47: h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]); melunko@47: h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]); melunko@47: melunko@47: GST_DEBUG_OBJECT (nuv, melunko@47: "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d" melunko@47: "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld", melunko@47: h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc, melunko@47: h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels, melunko@47: h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality, melunko@47: h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate, melunko@47: h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset, melunko@47: h->i_keyframe_adjust_offset); melunko@47: melunko@47: *h_ret = h; melunko@47: gst_buffer_unref (buff); melunko@47: return res; melunko@47: } melunko@47: melunko@47: static gboolean melunko@47: gst_nuv_demux_handle_src_event (GstPad * pad, GstEvent * event) melunko@47: { melunko@47: gst_event_unref (event); melunko@47: return FALSE; melunko@47: } melunko@47: melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_create_pads (GstNuvDemux * nuv) melunko@47: { melunko@47: if (nuv->h->i_video_blocks != 0) { melunko@47: GstCaps *video_caps = NULL; melunko@47: melunko@47: nuv->src_video_pad = melunko@47: gst_pad_new_from_static_template (&video_src_template, "video_src"); melunko@47: melunko@47: video_caps = gst_caps_new_simple ("video/x-divx", melunko@47: "divxversion", G_TYPE_INT, 4, melunko@47: "width", G_TYPE_INT, nuv->h->i_width, melunko@47: "height", G_TYPE_INT, nuv->h->i_height, melunko@47: "framerate", GST_TYPE_FRACTION, (gint) (nuv->h->d_fps * 1000.0f), 1000, melunko@47: "pixel-aspect-ratio", GST_TYPE_FRACTION, melunko@47: (gint) (nuv->h->d_aspect * 1000.0f), 1000, NULL); melunko@47: melunko@47: gst_pad_use_fixed_caps (nuv->src_video_pad); melunko@47: gst_pad_set_active (nuv->src_video_pad, TRUE); melunko@47: gst_pad_set_caps (nuv->src_video_pad, video_caps); melunko@47: melunko@47: gst_pad_set_event_function (nuv->src_video_pad, melunko@47: gst_nuv_demux_handle_src_event); melunko@47: gst_pad_set_active (nuv->src_video_pad, TRUE); melunko@47: gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad); melunko@47: gst_caps_unref (video_caps); melunko@47: } melunko@47: melunko@47: if (nuv->h->i_audio_blocks != 0) { melunko@47: GstCaps *audio_caps = NULL; melunko@47: melunko@47: nuv->src_audio_pad = melunko@47: gst_pad_new_from_static_template (&audio_src_template, "audio_src"); melunko@47: melunko@47: audio_caps = gst_caps_new_simple ("audio/mpeg", melunko@47: "rate", G_TYPE_INT, nuv->eh->i_audio_sample_rate, melunko@47: "format", GST_TYPE_FOURCC, nuv->eh->i_audio_fcc, melunko@47: "channels", G_TYPE_INT, nuv->eh->i_audio_channels, melunko@47: "mpegversion", G_TYPE_INT, nuv->eh->i_version, NULL); melunko@47: melunko@47: gst_pad_use_fixed_caps (nuv->src_audio_pad); melunko@47: gst_pad_set_active (nuv->src_audio_pad, TRUE); melunko@47: gst_pad_set_caps (nuv->src_audio_pad, audio_caps); melunko@47: gst_pad_set_active (nuv->src_audio_pad, TRUE); melunko@47: gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_audio_pad); melunko@47: melunko@47: gst_pad_set_event_function (nuv->src_audio_pad, melunko@47: gst_nuv_demux_handle_src_event); melunko@47: gst_caps_unref (audio_caps); melunko@47: } melunko@47: melunko@47: gst_element_no_more_pads (GST_ELEMENT (nuv)); melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_read_head_frame (GstNuvDemux * nuv) melunko@47: { melunko@47: GstFlowReturn ret = GST_FLOW_OK; melunko@47: melunko@47: ret = gst_nuv_demux_frame_header_load (nuv, &nuv->fh); melunko@47: if (ret != GST_FLOW_OK) melunko@47: return ret; melunko@47: melunko@47: nuv->state = GST_NUV_DEMUX_MOVI; melunko@47: return ret; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_stream_data (GstNuvDemux * nuv) melunko@47: { melunko@47: GstFlowReturn ret = GST_FLOW_OK; melunko@47: GstBuffer *buf = NULL; melunko@47: nuv_frame_header *h = nuv->fh; melunko@47: melunko@47: if (h->i_type == 'R') melunko@47: goto done; melunko@47: melunko@47: ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf); melunko@47: if (ret != GST_FLOW_OK) { melunko@47: return ret; melunko@47: } melunko@47: melunko@47: GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND; melunko@47: melunko@47: switch (h->i_type) { melunko@47: case 'V': melunko@47: { melunko@47: GST_BUFFER_OFFSET (buf) = nuv->video_offset; melunko@47: gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_video_pad)); melunko@47: ret = gst_pad_push (nuv->src_video_pad, buf); melunko@47: nuv->video_offset++; melunko@47: break; melunko@47: } melunko@47: case 'A': melunko@47: { melunko@47: GST_BUFFER_OFFSET (buf) = nuv->audio_offset; melunko@47: gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_audio_pad)); melunko@47: ret = gst_pad_push (nuv->src_audio_pad, buf); melunko@47: nuv->audio_offset++; melunko@47: break; melunko@47: } melunko@47: case 'S': melunko@47: { melunko@47: switch (h->i_compression) { melunko@47: case 'V': melunko@47: gst_pad_push_event (nuv->src_video_pad, melunko@47: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0)); melunko@47: break; melunko@47: case 'A': melunko@47: gst_pad_push_event (nuv->src_audio_pad, melunko@47: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0)); melunko@47: melunko@47: break; melunko@47: default: melunko@47: break; melunko@47: } melunko@47: break; melunko@47: } melunko@47: } melunko@47: melunko@47: done: melunko@47: nuv->state = GST_NUV_DEMUX_FRAME_HEADER; melunko@47: g_free (nuv->fh); melunko@47: nuv->fh = NULL; melunko@47: return ret; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv) melunko@47: { melunko@47: GstFlowReturn ret = GST_FLOW_OK; melunko@47: melunko@47: /* ffmpeg extra data */ melunko@47: ret = melunko@47: gst_nuv_demux_read_bytes (nuv, nuv->mpeg_data_size, TRUE, melunko@47: &nuv->mpeg_buffer); melunko@47: if (ret != GST_FLOW_OK) { melunko@47: return GST_FLOW_ERROR; melunko@47: } melunko@47: GST_BUFFER_SIZE (nuv->mpeg_buffer) = nuv->mpeg_data_size; melunko@47: nuv->state = GST_NUV_DEMUX_EXTEND_HEADER; melunko@47: return ret; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv) melunko@47: { melunko@47: GstFlowReturn ret = GST_FLOW_OK; melunko@47: melunko@47: /* Load 'D' */ melunko@47: nuv_frame_header *h; melunko@47: melunko@47: ret = gst_nuv_demux_frame_header_load (nuv, &h); melunko@47: if (ret != GST_FLOW_OK) melunko@47: return ret; melunko@47: melunko@47: if (h->i_type != 'D') { melunko@47: g_free (h); melunko@47: return GST_FLOW_ERROR; melunko@47: } melunko@47: melunko@47: if (h->i_length > 0) { melunko@47: if (h->i_compression == 'F') { melunko@47: nuv->state = GST_NUV_DEMUX_MPEG_DATA; melunko@47: } else { melunko@47: g_free (h); melunko@47: return GST_FLOW_ERROR; melunko@47: } melunko@47: } else { melunko@47: nuv->state = GST_NUV_DEMUX_EXTEND_HEADER; melunko@47: } melunko@47: melunko@47: g_free (h); melunko@47: h = NULL; melunko@47: return ret; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv) melunko@47: { melunko@47: GstFlowReturn ret = GST_FLOW_OK; melunko@47: melunko@47: ret = gst_nuv_demux_extended_header_load (nuv, &nuv->eh); melunko@47: if (ret != GST_FLOW_OK) melunko@47: return ret; melunko@47: melunko@47: gst_nuv_demux_create_pads (nuv); melunko@47: nuv->state = GST_NUV_DEMUX_FRAME_HEADER; melunko@47: return ret; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv) melunko@47: { melunko@47: GstBuffer *buf = NULL; melunko@47: GstFlowReturn res = GST_FLOW_OK; melunko@47: melunko@47: res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf); melunko@47: if (res != GST_FLOW_OK) { melunko@47: if (buf != NULL) { melunko@47: gst_buffer_unref (buf); melunko@47: } melunko@47: return res; melunko@47: } melunko@47: melunko@47: if (buf->data[0] == 'X') { melunko@47: gst_buffer_unref (buf); melunko@47: buf = NULL; melunko@47: nuv_frame_header *h = NULL; melunko@47: melunko@47: res = gst_nuv_demux_frame_header_load (nuv, &h); melunko@47: if (res != GST_FLOW_OK) melunko@47: return res; melunko@47: melunko@47: if (h->i_length != 512) { melunko@47: g_free (h); melunko@47: return GST_FLOW_ERROR; melunko@47: } melunko@47: g_free (h); melunko@47: h = NULL; melunko@47: nuv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA; melunko@47: } else { melunko@47: nuv->state = GST_NUV_DEMUX_INVALID_DATA; melunko@47: g_object_unref (buf); melunko@47: GST_ELEMENT_WARNING (nuv, STREAM, FAILED, melunko@47: (_("incomplete NUV support")), ("incomplete NUV support")); melunko@47: return GST_FLOW_ERROR; melunko@47: } melunko@47: return res; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_play (GstPad * pad) melunko@47: { melunko@47: GstFlowReturn res = GST_FLOW_OK; melunko@47: GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad)); melunko@47: melunko@47: switch (nuv->state) { melunko@47: case GST_NUV_DEMUX_START: melunko@47: res = gst_nuv_demux_stream_file_header (nuv); melunko@47: if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) { melunko@47: goto pause; melunko@47: } melunko@47: if (nuv->state != GST_NUV_DEMUX_HEADER_DATA) melunko@47: break; melunko@47: melunko@47: case GST_NUV_DEMUX_HEADER_DATA: melunko@47: res = gst_nuv_demux_stream_header_data (nuv); melunko@47: if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) { melunko@47: goto pause; melunko@47: } melunko@47: if (nuv->state != GST_NUV_DEMUX_EXTRA_DATA) melunko@47: break; melunko@47: melunko@47: case GST_NUV_DEMUX_EXTRA_DATA: melunko@47: res = gst_nuv_demux_stream_extra_data (nuv); melunko@47: if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) { melunko@47: goto pause; melunko@47: } melunko@47: if (nuv->state != GST_NUV_DEMUX_MPEG_DATA) melunko@47: break; melunko@47: melunko@47: case GST_NUV_DEMUX_MPEG_DATA: melunko@47: res = gst_nuv_demux_stream_mpeg_data (nuv); melunko@47: if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) { melunko@47: goto pause; melunko@47: } melunko@47: melunko@47: if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER) melunko@47: break; melunko@47: melunko@47: case GST_NUV_DEMUX_EXTEND_HEADER: melunko@47: res = gst_nuv_demux_stream_extend_header (nuv); melunko@47: if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) { melunko@47: goto pause; melunko@47: } melunko@47: if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER_DATA) melunko@47: break; melunko@47: melunko@47: case GST_NUV_DEMUX_EXTEND_HEADER_DATA: melunko@47: res = gst_nuv_demux_stream_extend_header_data (nuv); melunko@47: if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) { melunko@47: goto pause; melunko@47: } melunko@47: melunko@47: if (nuv->state != GST_NUV_DEMUX_FRAME_HEADER) melunko@47: break; melunko@47: melunko@47: case GST_NUV_DEMUX_FRAME_HEADER: melunko@47: res = gst_nuv_demux_read_head_frame (nuv); melunko@47: if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) { melunko@47: goto pause; melunko@47: } melunko@47: if (nuv->state != GST_NUV_DEMUX_MOVI) melunko@47: break; melunko@47: melunko@47: case GST_NUV_DEMUX_MOVI: melunko@47: res = gst_nuv_demux_stream_data (nuv); melunko@47: if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) { melunko@47: goto pause; melunko@47: } melunko@47: break; melunko@47: case GST_NUV_DEMUX_INVALID_DATA: melunko@47: goto pause; melunko@47: break; melunko@47: default: melunko@47: g_assert_not_reached (); melunko@47: } melunko@47: melunko@47: GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->state, melunko@47: gst_flow_get_name (res)); melunko@47: melunko@47: return GST_FLOW_OK; melunko@47: melunko@47: pause: melunko@47: GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res)); melunko@47: gst_pad_pause_task (nuv->sinkpad); melunko@47: if (GST_FLOW_IS_FATAL (res)) { melunko@47: GST_ELEMENT_ERROR (nuv, STREAM, FAILED, melunko@47: (_("Internal data stream error.")), melunko@47: ("streaming stopped, reason %s", gst_flow_get_name (res))); melunko@47: melunko@47: gst_nuv_demux_send_eos (nuv); melunko@47: } melunko@47: return res; melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_send_eos (GstNuvDemux * nuv) melunko@47: { melunko@47: gst_element_post_message (GST_ELEMENT (nuv), melunko@47: gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1)); melunko@47: melunko@47: if (nuv->src_video_pad) melunko@47: gst_pad_push_event (nuv->src_video_pad, gst_event_new_eos ()); melunko@47: if (nuv->src_audio_pad) melunko@47: gst_pad_push_event (nuv->src_audio_pad, gst_event_new_eos ()); melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move, melunko@47: GstBuffer ** buffer) melunko@47: { melunko@47: GstFlowReturn ret = GST_FLOW_OK; melunko@47: melunko@47: if (size == 0) { melunko@47: *buffer = gst_buffer_new (); melunko@47: return ret; melunko@47: } melunko@47: melunko@47: if (nuv->mode == 0) { melunko@47: ret = gst_pad_pull_range (nuv->sinkpad, nuv->offset, size, buffer); melunko@47: if (ret == GST_FLOW_OK) { melunko@47: if (move) { melunko@47: nuv->offset += size; melunko@47: } melunko@47: /* got eos */ melunko@47: } else if (ret == GST_FLOW_UNEXPECTED) { melunko@47: gst_nuv_demux_send_eos (nuv); melunko@47: return GST_FLOW_WRONG_STATE; melunko@47: } melunko@47: } else { melunko@47: if (gst_adapter_available (nuv->adapter) < size) melunko@47: return GST_FLOW_ERROR_NO_DATA; melunko@47: melunko@47: if (move) { melunko@47: guint8 *data = NULL; melunko@47: data = (guint8 *) gst_adapter_take (nuv->adapter, size); melunko@47: *buffer = gst_buffer_new (); melunko@47: gst_buffer_set_data (*buffer, data, size); melunko@47: } else { melunko@47: guint8 *data = NULL; melunko@47: melunko@47: data = (guint8 *) gst_adapter_peek (nuv->adapter, size); melunko@47: *buffer = gst_buffer_new (); melunko@47: gst_buffer_set_data (*buffer, data, size); melunko@47: } melunko@47: } melunko@47: return ret; melunko@47: } melunko@47: melunko@47: static gboolean melunko@47: gst_nuv_demux_sink_activate (GstPad * sinkpad) melunko@47: { melunko@47: gboolean res = TRUE; melunko@47: GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad)); melunko@47: melunko@47: if (gst_pad_check_pull_range (sinkpad)) { melunko@47: nuv->mode = 0; melunko@47: nuv->adapter = NULL; melunko@47: res = gst_pad_activate_pull (sinkpad, TRUE); melunko@47: } else { melunko@47: nuv->mode = 1; melunko@47: nuv->adapter = gst_adapter_new (); melunko@47: res = gst_pad_activate_push (sinkpad, TRUE); melunko@47: } melunko@47: melunko@47: g_object_unref (nuv); melunko@47: return res; melunko@47: } melunko@47: melunko@47: static gboolean melunko@47: gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active) melunko@47: { melunko@47: GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad)); melunko@47: melunko@47: if (active) { melunko@47: gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad); melunko@47: } else { melunko@47: gst_pad_stop_task (sinkpad); melunko@47: } melunko@47: gst_object_unref (nuv); melunko@47: melunko@47: return TRUE; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf) melunko@47: { melunko@47: GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad)); melunko@47: melunko@47: gst_adapter_push (nuv->adapter, buf); melunko@47: melunko@47: return gst_nuv_demux_play (pad); melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_loop (GstPad * pad) melunko@47: { melunko@47: gst_nuv_demux_play (pad); melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_reset (GstNuvDemux * nuv) melunko@47: { melunko@47: nuv->state = GST_NUV_DEMUX_START; melunko@47: nuv->mode = 0; melunko@47: nuv->offset = 0; melunko@47: nuv->video_offset = 0; melunko@47: nuv->audio_offset = 0; melunko@47: melunko@47: if (nuv->adapter != NULL) melunko@47: gst_adapter_clear (nuv->adapter); melunko@47: melunko@47: if (nuv->mpeg_buffer != NULL) { melunko@47: gst_buffer_unref (nuv->mpeg_buffer); melunko@47: nuv->mpeg_buffer = NULL; melunko@47: } melunko@47: melunko@47: g_free (nuv->h); melunko@47: nuv->h = NULL; melunko@47: melunko@47: g_free (nuv->eh); melunko@47: nuv->eh = NULL; melunko@47: melunko@47: g_free (nuv->fh); melunko@47: nuv->fh = NULL; melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv) melunko@47: { melunko@47: if (nuv->src_video_pad) { melunko@47: gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_video_pad); melunko@47: nuv->src_video_pad = NULL; melunko@47: } melunko@47: melunko@47: if (nuv->src_audio_pad) { melunko@47: gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_audio_pad); melunko@47: nuv->src_audio_pad = NULL; melunko@47: } melunko@47: } melunko@47: melunko@47: static GstStateChangeReturn melunko@47: gst_nuv_demux_change_state (GstElement * element, GstStateChange transition) melunko@47: { melunko@47: GstStateChangeReturn ret; melunko@47: melunko@47: switch (transition) { melunko@47: case GST_STATE_CHANGE_READY_TO_PAUSED: melunko@47: break; melunko@47: default: melunko@47: break; melunko@47: } melunko@47: melunko@47: ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); melunko@47: if (ret == GST_STATE_CHANGE_FAILURE) melunko@47: goto done; melunko@47: melunko@47: switch (transition) { melunko@47: case GST_STATE_CHANGE_PAUSED_TO_READY: melunko@47: gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element)); melunko@47: gst_nuv_demux_reset (GST_NUV_DEMUX (element)); melunko@47: break; melunko@47: default: melunko@47: break; melunko@47: } melunko@47: melunko@47: done: melunko@47: return ret; melunko@47: } melunko@47: melunko@47: static gboolean melunko@47: plugin_init (GstPlugin * plugin) melunko@47: { melunko@47: #ifdef ENABLE_NLS melunko@47: setlocale (LC_ALL, ""); melunko@47: bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); melunko@47: #endif /* ENABLE_NLS */ melunko@47: melunko@47: if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY, melunko@47: GST_TYPE_NUV_DEMUX)) { melunko@47: return FALSE; melunko@47: } melunko@47: return TRUE; melunko@47: } melunko@47: melunko@47: GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, melunko@47: GST_VERSION_MINOR, melunko@47: "nuvdemux", melunko@47: "Demuxes and muxes audio and video", melunko@47: plugin_init, VERSION, "LGPL", "NuvDemux", "")