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: renatofilho@74: #include "glib/gi18n.h" 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: 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); renatofilho@77: static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad, renatofilho@77: 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 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: 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: renatofilho@77: gst_pad_set_activatepush_function (nuv->sinkpad, renatofilho@77: gst_nuv_demux_sink_activate_push); renatofilho@77: melunko@47: gst_pad_set_chain_function (nuv->sinkpad, melunko@47: GST_DEBUG_FUNCPTR (gst_nuv_demux_chain)); melunko@47: melunko@47: gst_element_add_pad (GST_ELEMENT (nuv), nuv->sinkpad); rosfran@56: 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; leo_sobral@80: leo_sobral@80: nuv->new_audio_segment = TRUE; leo_sobral@80: nuv->new_video_segment = TRUE; leo_sobral@80: 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: } rosfran@56: 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: rosfran@56: 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]); renatofilho@77: h->i_mode = GPOINTER_TO_INT (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: renatofilho@77: h->i_type = GPOINTER_TO_INT (data[0]); renatofilho@77: h->i_compression = GPOINTER_TO_INT (data[1]); renatofilho@77: h->i_keyframe = GPOINTER_TO_INT (data[2]); renatofilho@77: h->i_filters = GPOINTER_TO_INT (data[3]); rosfran@57: h->i_timecode = GST_READ_UINT32_LE (&data[4]); melunko@47: h->i_length = GST_READ_UINT32_LE (&data[8]); renatofilho@77: rosfran@56: GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d", 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: renatofilho@79: //TODO: create a function to control events and send to src pads 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, renatofilho@77: "format", GST_TYPE_FOURCC, nuv->eh->i_video_fcc, 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_caps (nuv->src_video_pad, video_caps); renatofilho@66: gst_pad_set_active (nuv->src_video_pad, TRUE); renatofilho@66: gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad); renatofilho@66: 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_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: 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: renatofilho@77: if (nuv->fh != NULL) renatofilho@77: { renatofilho@77: g_free (nuv->fh); renatofilho@77: nuv->fh = NULL; renatofilho@77: } renatofilho@77: 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: renatofilho@79: //TODO: send a new segment event before the firts data 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; rosfran@56: rosfran@56: if (h->i_length > 0) { rosfran@57: ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf); rosfran@57: if (ret != GST_FLOW_OK) rosfran@57: return ret; rosfran@57: leo_sobral@80: GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND; renatofilho@77: } renatofilho@60: melunko@47: switch (h->i_type) { melunko@47: case 'V': melunko@47: { rosfran@56: if (h->i_length == 0) rosfran@56: break; rosfran@73: rosfran@73: if ( !gst_pad_is_linked( nuv->src_video_pad ) ) rosfran@73: break; rosfran@73: leo_sobral@80: if (nuv->new_video_segment) { leo_sobral@80: /* send new segment event*/ leo_sobral@80: gst_pad_push_event (nuv->src_video_pad, leo_sobral@80: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, leo_sobral@80: GST_CLOCK_TIME_NONE, 0)); leo_sobral@80: nuv->new_video_segment = FALSE; leo_sobral@80: } leo_sobral@80: rosfran@73: GST_BUFFER_SIZE (buf) = h->i_length; 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); renatofilho@77: if (ret != GST_FLOW_OK) { renatofilho@77: GST_WARNING_OBJECT (nuv, "error pushing on srcpad %s, is linked? = %d", renatofilho@77: gst_pad_get_name (nuv->src_video_pad), gst_pad_is_linked (nuv->src_video_pad)); renatofilho@77: } melunko@47: break; melunko@47: } melunko@47: case 'A': melunko@47: { rosfran@56: if (h->i_length == 0) rosfran@56: break; rosfran@73: rosfran@73: if ( !gst_pad_is_linked( nuv->src_audio_pad ) ) rosfran@73: break; leo_sobral@80: leo_sobral@80: if (nuv->new_audio_segment) { leo_sobral@80: /* send new segment event*/ leo_sobral@80: gst_pad_push_event (nuv->src_audio_pad, leo_sobral@80: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, leo_sobral@80: GST_CLOCK_TIME_NONE, 0)); leo_sobral@80: nuv->new_audio_segment = FALSE; leo_sobral@80: } leo_sobral@80: rosfran@73: GST_BUFFER_SIZE (buf) = h->i_length; 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); renatofilho@77: if (ret != GST_FLOW_OK) { renatofilho@77: GST_WARNING_OBJECT (nuv, "Error pushing on srcpad %s, is linked? = %d", renatofilho@77: gst_pad_get_name (nuv->src_audio_pad), gst_pad_is_linked (nuv->src_audio_pad)); renatofilho@77: } melunko@47: break; melunko@47: } melunko@47: case 'S': melunko@47: { melunko@47: switch (h->i_compression) { melunko@47: case 'V': renatofilho@77: if ( !gst_pad_is_linked( nuv->src_video_pad ) ) renatofilho@77: break; rosfran@73: renatofilho@77: GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h->i_timecode); melunko@47: gst_pad_push_event (nuv->src_video_pad, renatofilho@77: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h->i_timecode * GST_MSECOND, rosfran@73: GST_CLOCK_TIME_NONE, 0)); melunko@47: break; rosfran@73: case 'A': rosfran@73: if ( !gst_pad_is_linked( nuv->src_audio_pad ) ) rosfran@73: break; rosfran@73: renatofilho@77: GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h->i_timecode); melunko@47: gst_pad_push_event (nuv->src_audio_pad, renatofilho@77: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, rosfran@73: GST_CLOCK_TIME_NONE, 0)); melunko@47: break; melunko@47: default: melunko@47: break; melunko@47: } renatofilho@77: if (buf != NULL) renatofilho@77: gst_buffer_unref (buf); rosfran@56: } rosfran@56: default: rosfran@56: if (buf != NULL) rosfran@56: gst_buffer_unref (buf); rosfran@56: melunko@47: break; 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) { renatofilho@77: return ret; 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); renatofilho@77: 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: renatofilho@79: //TODO: try use "gst_pad_alloc_buffer" function 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: } rosfran@73: 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) { renatofilho@77: if (move) { renatofilho@77: nuv->offset += size; renatofilho@77: } melunko@47: /* got eos */ melunko@47: } else if (ret == GST_FLOW_UNEXPECTED) { renatofilho@77: if (buffer != NULL) renatofilho@77: gst_buffer_unref (buffer); renatofilho@77: 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) { renatofilho@77: guint8 *data = NULL; renatofilho@77: data = (guint8 *) gst_adapter_take (nuv->adapter, size); renatofilho@77: *buffer = gst_buffer_new (); renatofilho@77: gst_buffer_set_data (*buffer, data, size); melunko@47: } else { melunko@47: guint8 *data = NULL; 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: res = gst_pad_activate_pull (sinkpad, TRUE); melunko@47: } else { melunko@47: nuv->mode = 1; rosfran@73: if (nuv->adapter) { rosfran@73: gst_adapter_clear (nuv->adapter); rosfran@73: } else { rosfran@73: nuv->adapter = gst_adapter_new (); rosfran@73: } melunko@47: res = gst_pad_activate_push (sinkpad, TRUE); 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) { renatofilho@79: nuv->mode = 0; renatofilho@79: if (nuv->adapter) { renatofilho@79: gst_adapter_clear (nuv->adapter); renatofilho@79: g_object_unref (nuv->adapter); renatofilho@79: nuv->adapter = NULL; renatofilho@79: } 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: renatofilho@77: static gboolean renatofilho@77: gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active) renatofilho@77: { renatofilho@77: GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad)); renatofilho@77: renatofilho@77: if (active) { renatofilho@77: GST_DEBUG_OBJECT (nuv, "activating push/chain function"); renatofilho@77: } else { renatofilho@77: GST_DEBUG_OBJECT (nuv, "deactivating push/chain function"); renatofilho@77: } renatofilho@77: renatofilho@77: gst_object_unref (nuv); renatofilho@77: renatofilho@77: return TRUE; renatofilho@77: } renatofilho@77: 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: renatofilho@77: gst_object_unref (nuv); renatofilho@77: 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; renatofilho@77: nuv->last_time = 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: { rosfran@62: GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; rosfran@73: 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: renatofilho@66: 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, ""); rosfran@73: 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", "")