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 renatofilho@85: #include melunko@47: renatofilho@74: #include "glib/gi18n.h" melunko@47: #include "gstnuvdemux.h" melunko@47: renatofilho@91: #define GST_NUV_DEMUX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NUV_DEMUX, GstNuvDemuxPrivate)) renatofilho@91: melunko@47: GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug); melunko@47: #define GST_CAT_DEFAULT nuvdemux_debug renatofilho@91: #define GST_FLOW_ERROR_NO_DATA -101 melunko@47: renatofilho@82: enum renatofilho@82: { renatofilho@82: NUV_PUSH_MODE = 0, renatofilho@82: NUV_PULL_MODE renatofilho@82: }; 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: renatofilho@91: renatofilho@91: /* file header */ renatofilho@91: typedef struct renatofilho@91: { renatofilho@91: gchar id[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */ renatofilho@91: gchar version[5]; /* "x.xx\0" */ renatofilho@91: renatofilho@91: gint i_width; renatofilho@91: gint i_height; renatofilho@91: gint i_width_desired; renatofilho@91: gint i_height_desired; renatofilho@91: renatofilho@91: gchar i_mode; /* P progressive, I interlaced */ renatofilho@91: renatofilho@91: gdouble d_aspect; /* 1.0 squared pixel */ renatofilho@91: gdouble d_fps; renatofilho@91: //fps num/denom renatofilho@91: gint i_fpsn; renatofilho@91: gint i_fpsd; renatofilho@91: renatofilho@91: gint i_video_blocks; /* 0 no video, -1 unknown */ renatofilho@91: gint i_audio_blocks; renatofilho@91: gint i_text_blocks; renatofilho@91: renatofilho@91: gint i_keyframe_distance; renatofilho@91: renatofilho@91: } nuv_header; renatofilho@91: renatofilho@91: /* frame header */ renatofilho@91: typedef struct renatofilho@91: { renatofilho@91: gchar i_type; /* A: audio, V: video, S: sync; T: test renatofilho@91: R: Seekpoint (string:RTjjjjjjjj) renatofilho@91: D: Extra data for codec */ renatofilho@91: gchar i_compression; /* V: 0 uncompressed renatofilho@91: 1 RTJpeg renatofilho@91: 2 RTJpeg+lzo renatofilho@91: N black frame renatofilho@91: L copy last renatofilho@91: A: 0 uncompressed (44100 1-bits, 2ch) renatofilho@91: 1 lzo renatofilho@91: 2 layer 2 renatofilho@91: 3 layer 3 renatofilho@91: F flac renatofilho@91: S shorten renatofilho@91: N null frame loudless renatofilho@91: L copy last renatofilho@91: S: B audio and vdeo sync point renatofilho@91: A audio sync info (timecode == effective renatofilho@91: dsp frequency*100) renatofilho@91: V next video sync (timecode == next video renatofilho@91: frame num) renatofilho@91: S audio,video,text correlation */ renatofilho@91: gchar i_keyframe; /* 0 keyframe, else no no key frame */ renatofilho@91: guint8 i_filters; /* 0x01: gauss 5 pixel (8,2,2,2,2)/16 renatofilho@91: 0x02: gauss 5 pixel (8,1,1,1,1)/12 renatofilho@91: 0x04: cartoon filter */ renatofilho@91: renatofilho@91: gint32 i_timecode; /* ms */ renatofilho@91: renatofilho@91: gint i_length; /* V,A,T: length of following data renatofilho@91: S: length of packet correl */ renatofilho@91: } nuv_frame_header; renatofilho@91: renatofilho@91: /* FIXME Not sure of this one */ renatofilho@91: typedef struct renatofilho@91: { renatofilho@91: gint i_version; renatofilho@91: guint32 i_video_fcc; renatofilho@91: renatofilho@91: guint32 i_audio_fcc; renatofilho@91: gint i_audio_sample_rate; renatofilho@91: gint i_audio_bits_per_sample; renatofilho@91: gint i_audio_channels; renatofilho@91: gint i_audio_compression_ratio; renatofilho@91: gint i_audio_quality; renatofilho@91: gint i_rtjpeg_quality; renatofilho@91: gint i_rtjpeg_luma_filter; renatofilho@91: gint i_rtjpeg_chroma_filter; renatofilho@91: gint i_lavc_bitrate; renatofilho@91: gint i_lavc_qmin; renatofilho@91: gint i_lavc_qmax; renatofilho@91: gint i_lavc_maxqdiff; renatofilho@91: gint64 i_seekable_offset; renatofilho@91: gint64 i_keyframe_adjust_offset; renatofilho@91: renatofilho@91: } nuv_extended_header; renatofilho@91: renatofilho@91: typedef enum { renatofilho@91: GST_NUV_DEMUX_START, renatofilho@91: GST_NUV_DEMUX_HEADER_DATA, renatofilho@91: GST_NUV_DEMUX_EXTRA_DATA, renatofilho@91: GST_NUV_DEMUX_MPEG_DATA, renatofilho@91: GST_NUV_DEMUX_EXTEND_HEADER, renatofilho@91: GST_NUV_DEMUX_EXTEND_HEADER_DATA, renatofilho@91: GST_NUV_DEMUX_FRAME_HEADER, renatofilho@91: GST_NUV_DEMUX_MOVI, renatofilho@91: GST_NUV_DEMUX_INVALID_DATA renatofilho@91: } GstNuvDemuxState; renatofilho@91: renatofilho@91: struct _GstNuvDemuxPrivate { renatofilho@91: /* used for indicate the mode */ renatofilho@91: guint mode; renatofilho@91: renatofilho@91: /* used on push mode */ renatofilho@91: GstAdapter *adapter; renatofilho@91: renatofilho@91: /* pads */ renatofilho@91: GstPad *sinkpad; renatofilho@91: GstPad *src_video_pad; renatofilho@91: GstPad *src_audio_pad; renatofilho@91: renatofilho@91: /* Flow control */ renatofilho@91: GstFlowReturn last_video_return; renatofilho@91: GstFlowReturn last_audio_return; renatofilho@91: renatofilho@91: /* NUV decoding state */ renatofilho@91: GstNuvDemuxState state; renatofilho@91: GstSegment segment; renatofilho@91: guint64 last_update; renatofilho@91: guint64 offset; renatofilho@91: guint64 streamer_offset; renatofilho@91: renatofilho@91: /* duration information */ renatofilho@91: gint64 duration_bytes; renatofilho@91: gint64 duration_time; renatofilho@91: gint64 duration_average; renatofilho@91: renatofilho@91: /* segment control info */ renatofilho@91: gboolean new_audio_segment; renatofilho@91: gboolean new_video_segment; renatofilho@91: renatofilho@91: /* Mpeg ExtraData */ renatofilho@91: guint64 mpeg_data_size; renatofilho@91: GstBuffer *mpeg_buffer; renatofilho@91: renatofilho@91: /* Headers */ renatofilho@170: nuv_header h; renatofilho@170: nuv_extended_header eh; renatofilho@170: nuv_frame_header fh; renatofilho@91: renatofilho@91: /* anothers info */ renatofilho@91: gint64 time_start; renatofilho@91: gint64 time_diff; renatofilho@91: gint64 time_qos; renatofilho@91: guint64 last_frame_time; renatofilho@91: }; renatofilho@91: renatofilho@91: 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: renatofilho@91: static void gst_nuv_demux_dispose (GObject * object); 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@91: 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); renatofilho@91: static gboolean gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event); 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); renatofilho@91: static void gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp); renatofilho@91: static gint64 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv); renatofilho@91: static gint64 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv); renatofilho@181: renatofilho@181: #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) renatofilho@173: GstBuffer * gst_nuv_demux_adapter_take_buffer (GstAdapter * adapter, guint nbytes); renatofilho@181: #endif melunko@47: renatofilho@91: melunko@47: GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT); melunko@47: renatofilho@91: /****************************************************************************** renatofilho@91: * Utils function renatofilho@91: ******************************************************************************/ 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: renatofilho@91: static void renatofilho@85: double2fraction (double in, int *num, int *denom) renatofilho@85: { renatofilho@85: if (in == 29.97) { renatofilho@85: *num = 30000; renatofilho@85: *denom = 1001; renatofilho@85: } else if (in == 23.976) { renatofilho@85: *num = 24000; renatofilho@85: *denom = 1001; renatofilho@85: } else { renatofilho@85: *denom = 1; renatofilho@85: while (in - floor(in) >= 0.1) { renatofilho@85: *denom *= 10; renatofilho@85: in *= 10.0; renatofilho@85: } renatofilho@85: *num = (int)floor(in); renatofilho@85: } renatofilho@85: } renatofilho@85: renatofilho@91: /* GObject Functions */ renatofilho@91: 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); renatofilho@181: renatofilho@181: g_debug ("GST VER: %d.%d", GST_VERSION_MINOR, GST_VERSION_MAJOR); 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: renatofilho@91: gobject_class->dispose = gst_nuv_demux_dispose; melunko@47: gobject_class->finalize = gst_nuv_demux_finalize; melunko@47: gstelement_class->change_state = gst_nuv_demux_change_state; renatofilho@91: renatofilho@91: g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate)); melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class) melunko@47: { renatofilho@91: nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv); renatofilho@91: nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink"); renatofilho@91: gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate); renatofilho@91: gst_pad_set_activatepull_function (nuv->priv->sinkpad, renatofilho@91: gst_nuv_demux_sink_activate_pull); renatofilho@91: gst_pad_set_activatepush_function (nuv->priv->sinkpad, renatofilho@91: gst_nuv_demux_sink_activate_push); renatofilho@91: gst_pad_set_chain_function (nuv->priv->sinkpad, renatofilho@91: GST_DEBUG_FUNCPTR (gst_nuv_demux_chain)); renatofilho@91: gst_pad_set_event_function (nuv->priv->sinkpad, renatofilho@91: gst_nuv_demux_sink_event); renatofilho@91: gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad); melunko@47: renatofilho@91: nuv->priv->new_audio_segment = TRUE; renatofilho@91: nuv->priv->new_video_segment = TRUE; melunko@47: renatofilho@133: /* creating adapter */ renatofilho@133: nuv->priv->mode = NUV_PUSH_MODE; renatofilho@133: nuv->priv->adapter = gst_adapter_new (); renatofilho@133: renatofilho@91: gst_nuv_demux_reset (nuv); renatofilho@91: } melunko@47: renatofilho@91: static void renatofilho@91: gst_nuv_demux_dispose (GObject * object) renatofilho@91: { renatofilho@91: GstNuvDemux *nuv = GST_NUV_DEMUX (object); renatofilho@77: renatofilho@91: if (nuv->priv->mpeg_buffer != NULL) { renatofilho@91: gst_buffer_unref (nuv->priv->mpeg_buffer); renatofilho@91: } melunko@47: renatofilho@91: gst_nuv_demux_reset (GST_NUV_DEMUX (object)); renatofilho@91: gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (object)); leo_sobral@80: renatofilho@91: if (nuv->priv->adapter != NULL) { renatofilho@91: gst_object_unref (nuv->priv->adapter); renatofilho@91: } melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_finalize (GObject * object) 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 renatofilho@170: gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header *h) melunko@47: { melunko@47: GstBuffer *buffer = NULL; melunko@47: GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer); melunko@47: renatofilho@84: if ((res != GST_FLOW_OK) || (buffer == NULL)) melunko@47: return res; 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]); renatofilho@85: /* get the num and denom values from fps */ renatofilho@85: double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd); 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: 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: { renatofilho@91: GstFlowReturn res; melunko@47: renatofilho@91: res = gst_nuv_demux_header_load (nuv, &nuv->priv->h); melunko@47: if (res == GST_FLOW_OK) renatofilho@91: nuv->priv->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); renatofilho@84: if ((res != GST_FLOW_OK) || (file_header == NULL)) { 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)) { renatofilho@91: nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA; melunko@47: } else { melunko@47: GST_DEBUG_OBJECT (nuv, "error parsing file header"); renatofilho@91: nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA; melunko@47: res = GST_FLOW_ERROR; melunko@47: } melunko@47: } renatofilho@163: melunko@47: if (file_header != NULL) { melunko@47: gst_buffer_unref (file_header); melunko@47: } renatofilho@163: melunko@47: return res; melunko@47: } melunko@47: melunko@47: /* FrameHeaderLoad: melunko@47: */ melunko@47: static GstFlowReturn renatofilho@170: gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header *h) melunko@47: { melunko@47: unsigned char *data; melunko@47: GstBuffer *buf = NULL; melunko@47: melunko@47: GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf); melunko@47: renatofilho@84: if ((res != GST_FLOW_OK) || (buf == NULL)) { melunko@47: if (buf != NULL) { melunko@47: gst_buffer_unref (buf); renatofilho@170: buf = NULL; melunko@47: } melunko@47: return res; melunko@47: } melunko@47: 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@91: 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: gst_buffer_unref (buf); renatofilho@170: buf = NULL; melunko@47: return GST_FLOW_OK; melunko@47: } melunko@47: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_extended_header_load (GstNuvDemux * nuv, renatofilho@170: nuv_extended_header * h) melunko@47: { melunko@47: unsigned char *data; melunko@47: GstBuffer *buff = NULL; melunko@47: melunko@47: GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff); melunko@47: renatofilho@84: if ((res != GST_FLOW_OK) || (buff == NULL)) { melunko@47: if (buff != NULL) { melunko@47: gst_buffer_unref (buff); melunko@47: } melunko@47: return res; melunko@47: } melunko@47: 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: gst_buffer_unref (buff); melunko@47: return res; melunko@47: } renatofilho@91: renatofilho@91: renatofilho@91: /* Query Functions */ renatofilho@85: static const GstQueryType * renatofilho@85: gst_nuv_demux_get_src_query_types (GstPad * pad) renatofilho@85: { renatofilho@85: static const GstQueryType src_types[] = { renatofilho@85: GST_QUERY_POSITION, renatofilho@91: GST_QUERY_DURATION, renatofilho@85: 0 renatofilho@85: }; renatofilho@85: renatofilho@85: return src_types; renatofilho@85: } renatofilho@85: renatofilho@85: static gboolean renatofilho@85: gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query) renatofilho@85: { renatofilho@85: gboolean res = FALSE; renatofilho@85: GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad)); renatofilho@85: renatofilho@85: switch (GST_QUERY_TYPE (query)) { renatofilho@85: case GST_QUERY_POSITION: renatofilho@91: if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) { renatofilho@91: renatofilho@91: gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time); renatofilho@85: res = TRUE; renatofilho@91: } renatofilho@91: break; renatofilho@91: case GST_QUERY_DURATION: renatofilho@91: { renatofilho@91: gint64 duration = 0; renatofilho@91: duration = gst_nuv_demux_get_time_duration (nuv); renatofilho@91: if (duration == GST_CLOCK_TIME_NONE) { renatofilho@91: duration = nuv->priv->duration_average; renatofilho@91: } renatofilho@91: if (duration != GST_CLOCK_TIME_NONE) { renatofilho@91: gst_query_set_duration (query, GST_FORMAT_TIME, duration); renatofilho@91: res = TRUE; renatofilho@91: } renatofilho@85: } renatofilho@85: break; renatofilho@85: default: renatofilho@85: res = FALSE; renatofilho@85: break; renatofilho@85: } renatofilho@85: renatofilho@85: gst_object_unref (nuv); renatofilho@85: renatofilho@85: return res; renatofilho@85: } melunko@47: renatofilho@91: static GstPad* renatofilho@91: gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name) renatofilho@91: { renatofilho@91: GstPad *pad = NULL; renatofilho@91: pad = gst_pad_new_from_static_template (template, name); renatofilho@91: gst_pad_set_caps (pad, caps); renatofilho@91: gst_pad_set_active (pad, TRUE); renatofilho@133: gst_pad_use_fixed_caps (pad); renatofilho@91: gst_element_add_pad (GST_ELEMENT (nuv), pad); renatofilho@91: renatofilho@91: return pad; renatofilho@91: } renatofilho@91: melunko@47: static void melunko@47: gst_nuv_demux_create_pads (GstNuvDemux * nuv) melunko@47: { renatofilho@170: if (nuv->priv->h.i_video_blocks != 0) { melunko@47: GstCaps *video_caps = NULL; melunko@47: melunko@47: video_caps = gst_caps_new_simple ("video/x-divx", melunko@47: "divxversion", G_TYPE_INT, 4, renatofilho@170: "width", G_TYPE_INT, nuv->priv->h.i_width, renatofilho@170: "height", G_TYPE_INT, nuv->priv->h.i_height, renatofilho@170: "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd, renatofilho@170: "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc, melunko@47: "pixel-aspect-ratio", GST_TYPE_FRACTION, renatofilho@170: (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL); melunko@47: renatofilho@91: nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src"); melunko@47: gst_caps_unref (video_caps); melunko@47: } melunko@47: renatofilho@170: if (nuv->priv->h.i_audio_blocks != 0) { melunko@47: GstCaps *audio_caps = NULL; melunko@47: renatofilho@91: audio_caps = gst_caps_new_simple ("audio/mpeg", renatofilho@170: "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate, renatofilho@170: "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc, renatofilho@170: "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels, renatofilho@170: "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL); melunko@47: renatofilho@91: nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src"); 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@91: ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh); melunko@47: if (ret != GST_FLOW_OK) melunko@47: return ret; melunko@47: renatofilho@91: nuv->priv->state = GST_NUV_DEMUX_MOVI; melunko@47: return ret; melunko@47: } melunko@47: renatofilho@88: static gboolean renatofilho@88: gst_nuv_combine_flow (GstNuvDemux *nuv) renatofilho@88: { renatofilho@91: GstFlowReturn ret_video = nuv->priv->last_video_return; renatofilho@91: GstFlowReturn ret_audio = nuv->priv->last_audio_return; renatofilho@88: renatofilho@88: if ((ret_video != GST_FLOW_OK) && renatofilho@88: (ret_audio != GST_FLOW_OK)) renatofilho@88: return FALSE; renatofilho@88: renatofilho@91: if (GST_FLOW_IS_FATAL (ret_video)) renatofilho@88: return FALSE; renatofilho@88: renatofilho@91: if (GST_FLOW_IS_FATAL (ret_audio)) renatofilho@88: return FALSE; renatofilho@88: renatofilho@91: return TRUE; renatofilho@88: } renatofilho@88: melunko@47: static GstFlowReturn melunko@47: gst_nuv_demux_stream_data (GstNuvDemux * nuv) melunko@47: { melunko@47: GstFlowReturn ret = GST_FLOW_OK; renatofilho@91: GstPad *pad = NULL; renatofilho@91: guint64 timestamp; melunko@47: GstBuffer *buf = NULL; renatofilho@170: nuv_frame_header h; renatofilho@91: renatofilho@91: h = nuv->priv->fh; melunko@47: renatofilho@170: if (h.i_type == 'R') melunko@47: goto done; renatofilho@91: renatofilho@170: if (h.i_length > 0) { renatofilho@170: ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf); renatofilho@84: if ((ret != GST_FLOW_OK) || (buf == NULL)) rosfran@57: return ret; rosfran@57: renatofilho@170: if (h.i_timecode < 0) { renatofilho@170: h.i_timecode = h.i_timecode * -1; renatofilho@170: nuv->priv->time_diff = h.i_timecode; leo_sobral@83: } renatofilho@84: else renatofilho@170: h.i_timecode += nuv->priv->time_diff; renatofilho@91: renatofilho@170: timestamp = h.i_timecode * GST_MSECOND; renatofilho@91: GST_BUFFER_TIMESTAMP (buf) = timestamp; renatofilho@77: } renatofilho@91: else { renatofilho@91: goto done; renatofilho@91: } renatofilho@91: renatofilho@60: renatofilho@170: switch (h.i_type) { melunko@47: case 'V': melunko@47: { renatofilho@91: pad = nuv->priv->src_video_pad; renatofilho@91: renatofilho@91: if (nuv->priv->new_video_segment) { renatofilho@136: leo_sobral@80: /* send new segment event*/ renatofilho@137: gst_pad_push_event (nuv->priv->src_video_pad, renatofilho@137: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, renatofilho@137: GST_CLOCK_TIME_NONE, 0)); renatofilho@91: renatofilho@91: if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) { renatofilho@91: nuv->priv->time_start = timestamp; renatofilho@91: } renatofilho@91: nuv->priv->new_video_segment = FALSE; leo_sobral@80: } renatofilho@91: melunko@47: break; melunko@47: } melunko@47: case 'A': melunko@47: { renatofilho@91: pad = nuv->priv->src_audio_pad; renatofilho@91: renatofilho@91: if (nuv->priv->new_audio_segment) { leo_sobral@80: /* send new segment event*/ renatofilho@137: gst_pad_push_event (nuv->priv->src_audio_pad, renatofilho@137: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, renatofilho@137: GST_CLOCK_TIME_NONE, 0)); renatofilho@91: renatofilho@91: if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) { renatofilho@91: nuv->priv->time_start = timestamp; renatofilho@91: } renatofilho@91: nuv->priv->new_audio_segment = FALSE; leo_sobral@80: } renatofilho@91: melunko@47: break; melunko@47: } melunko@47: case 'S': melunko@47: { renatofilho@170: switch (h.i_compression) { melunko@47: case 'V': renatofilho@170: GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode); renatofilho@91: gst_pad_push_event (nuv->priv->src_video_pad, renatofilho@170: 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; renatofilho@91: case 'A': renatofilho@170: GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode); renatofilho@91: gst_pad_push_event (nuv->priv->src_audio_pad, renatofilho@91: 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@91: goto done; rosfran@56: } rosfran@56: default: melunko@47: break; melunko@47: } melunko@47: renatofilho@91: if ((buf != NULL) && (pad != NULL)) { renatofilho@91: /* update average time */ renatofilho@170: nuv->priv->streamer_offset += h.i_length; renatofilho@91: gst_segment_set_last_stop (&nuv->priv->segment, GST_FORMAT_TIME, timestamp); renatofilho@91: gst_nuv_demux_update_duration (nuv, timestamp); renatofilho@91: renatofilho@91: /* pushing the buffer */ renatofilho@91: gst_buffer_set_caps (buf, GST_PAD_CAPS (pad)); renatofilho@91: ret = gst_pad_push (pad, buf); renatofilho@168: buf = NULL; renatofilho@91: renatofilho@91: if (ret != GST_FLOW_OK) { renatofilho@136: GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad)); renatofilho@136: renatofilho@91: if (pad == nuv->priv->src_video_pad) { renatofilho@91: nuv->priv->last_video_return = ret; renatofilho@91: } renatofilho@91: else if (pad == nuv->priv->src_audio_pad) { renatofilho@91: nuv->priv->last_audio_return = ret; renatofilho@91: } renatofilho@91: renatofilho@91: /* verify anothers flow if is necessary stop task */ renatofilho@91: if (gst_nuv_combine_flow (nuv) != FALSE) { renatofilho@91: ret = GST_FLOW_OK; renatofilho@91: } renatofilho@91: renatofilho@136: //GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad)); renatofilho@91: } renatofilho@168: } renatofilho@168: renatofilho@168: done: renatofilho@168: if (buf != NULL) { renatofilho@171: g_free (buf->data); renatofilho@163: gst_buffer_unref (buf); renatofilho@163: buf = NULL; renatofilho@91: } renatofilho@91: nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER; renatofilho@170: memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh)); 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 */ renatofilho@91: ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE, renatofilho@91: &nuv->priv->mpeg_buffer); renatofilho@91: if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) { renatofilho@91: return ret; melunko@47: } renatofilho@91: renatofilho@91: GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size; renatofilho@91: nuv->priv->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' */ renatofilho@170: 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: renatofilho@170: if (h.i_type != 'D') { renatofilho@92: GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg"); renatofilho@92: return GST_FLOW_NOT_SUPPORTED; melunko@47: } melunko@47: renatofilho@170: if (h.i_length > 0) { renatofilho@170: if (h.i_compression == 'F') { renatofilho@91: nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA; melunko@47: } else { renatofilho@92: GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported"); renatofilho@92: return GST_FLOW_NOT_SUPPORTED; melunko@47: } melunko@47: } else { renatofilho@91: nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER; melunko@47: } melunko@47: 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: renatofilho@91: ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh); melunko@47: if (ret != GST_FLOW_OK) melunko@47: return ret; melunko@47: melunko@47: gst_nuv_demux_create_pads (nuv); renatofilho@91: nuv->priv->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); renatofilho@84: if ((res != GST_FLOW_OK) || (buf == NULL)) { 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; renatofilho@170: nuv_frame_header h; 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: renatofilho@170: if (h.i_length != 512) { melunko@47: return GST_FLOW_ERROR; melunko@47: } renatofilho@91: nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA; melunko@47: } else { renatofilho@91: nuv->priv->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: renatofilho@91: switch (nuv->priv->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: } renatofilho@136: 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: } renatofilho@136: 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: } renatofilho@136: 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: } renatofilho@136: 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: } renatofilho@136: 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: } renatofilho@136: 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: } renatofilho@136: 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; renatofilho@136: 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: renatofilho@91: GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->priv->state, 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)); renatofilho@91: gst_pad_pause_task (nuv->priv->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: renatofilho@91: if (nuv->priv->src_video_pad) renatofilho@91: gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ()); renatofilho@91: if (nuv->priv->src_audio_pad) renatofilho@91: gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ()); melunko@47: } renatofilho@91: 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: return ret; melunko@47: } renatofilho@91: renatofilho@91: if (nuv->priv->mode == NUV_PULL_MODE) { renatofilho@91: ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer); melunko@47: if (ret == GST_FLOW_OK) { renatofilho@77: if (move) { renatofilho@91: nuv->priv->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@91: melunko@47: gst_nuv_demux_send_eos (nuv); melunko@47: return GST_FLOW_WRONG_STATE; melunko@47: } melunko@47: } else { renatofilho@91: if (gst_adapter_available (nuv->priv->adapter) < size) melunko@47: return GST_FLOW_ERROR_NO_DATA; melunko@47: melunko@47: if (move) { renatofilho@181: *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size); melunko@47: } else { renatofilho@156: guint8 *data = NULL; renatofilho@156: data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size); renatofilho@156: *buffer = gst_buffer_new (); renatofilho@165: 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: res = gst_pad_activate_push (sinkpad, TRUE); melunko@47: } renatofilho@173: 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@85: GST_DEBUG_OBJECT (nuv, "activating pull function"); renatofilho@91: nuv->priv->mode = NUV_PULL_MODE; renatofilho@91: if (nuv->priv->adapter) { renatofilho@91: gst_adapter_clear (nuv->priv->adapter); renatofilho@91: g_object_unref (nuv->priv->adapter); renatofilho@91: nuv->priv->adapter = NULL; renatofilho@79: } melunko@47: gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad); melunko@47: } else { renatofilho@85: GST_DEBUG_OBJECT (nuv, "deactivating pull function"); 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@133: 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: renatofilho@91: renatofilho@91: static gboolean renatofilho@91: gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event) renatofilho@91: { renatofilho@91: gboolean res; renatofilho@91: GstNuvDemux *nuv; renatofilho@91: renatofilho@91: nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad)); renatofilho@91: renatofilho@91: switch (GST_EVENT_TYPE (event)) { renatofilho@91: case GST_EVENT_SEEK: renatofilho@91: { renatofilho@91: gdouble rate; renatofilho@91: GstFormat format; renatofilho@91: GstSeekFlags flags; renatofilho@91: GstSeekType cur_type; renatofilho@91: gint64 cur; renatofilho@91: GstSeekType stop_type; renatofilho@91: gint64 stop; renatofilho@91: renatofilho@91: gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur, &stop_type, &stop); renatofilho@91: GST_DEBUG_OBJECT (nuv, "got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop); renatofilho@91: break; renatofilho@91: } renatofilho@91: case GST_EVENT_NEWSEGMENT: renatofilho@91: { renatofilho@91: gint64 start, stop, time; renatofilho@91: gdouble rate, arate; renatofilho@91: GstFormat format; renatofilho@91: gboolean update; renatofilho@91: renatofilho@91: GST_DEBUG_OBJECT (nuv, "got a new segment event"); renatofilho@167: gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &time); renatofilho@91: renatofilho@91: GST_DEBUG_OBJECT (nuv, "got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop); renatofilho@91: nuv->priv->duration_bytes = stop - start; renatofilho@91: gst_event_unref (event); renatofilho@91: res = TRUE; renatofilho@91: break; renatofilho@91: } renatofilho@91: case GST_EVENT_QOS: renatofilho@91: { renatofilho@91: /* renatofilho@91: gdouble proportion; renatofilho@91: GstClockTimeDiff diff; renatofilho@91: GstClockTime timestamp; renatofilho@91: renatofilho@91: gst_event_parse_qos (event, &proportion, &diff, ×tamp); renatofilho@91: if (diff > 0) renatofilho@91: nuv->time_qos = timecode + diff; renatofilho@91: else renatofilho@91: nuv->time_qos = -1; renatofilho@91: */ renatofilho@91: break; renatofilho@91: } renatofilho@91: renatofilho@91: case GST_EVENT_EOS: renatofilho@91: default: renatofilho@91: res = gst_pad_event_default (pad, event); renatofilho@91: break; renatofilho@91: } renatofilho@91: renatofilho@91: gst_object_unref (nuv); renatofilho@91: return res; renatofilho@91: } renatofilho@91: 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: renatofilho@85: GST_DEBUG_OBJECT (nuv, " gst_nuv_demux_chain function"); renatofilho@91: gst_adapter_push (nuv->priv->adapter, buf); melunko@47: renatofilho@77: gst_object_unref (nuv); renatofilho@168: //gst_buffer_unref (buf); 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: { renatofilho@91: nuv->priv->state = GST_NUV_DEMUX_START; renatofilho@91: nuv->priv->mode = 0; renatofilho@91: nuv->priv->offset = 0; renatofilho@91: nuv->priv->streamer_offset = 0; renatofilho@91: nuv->priv->time_start = 0; renatofilho@91: nuv->priv->time_qos = GST_CLOCK_TIME_NONE; renatofilho@91: nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE; renatofilho@91: nuv->priv->duration_time = GST_CLOCK_TIME_NONE; renatofilho@91: nuv->priv->duration_average = GST_CLOCK_TIME_NONE; renatofilho@91: gst_segment_init (&nuv->priv->segment, GST_FORMAT_TIME); melunko@47: renatofilho@91: if (nuv->priv->adapter != NULL) renatofilho@91: gst_adapter_clear (nuv->priv->adapter); melunko@47: renatofilho@91: if (nuv->priv->mpeg_buffer != NULL) { renatofilho@91: gst_buffer_unref (nuv->priv->mpeg_buffer); renatofilho@91: nuv->priv->mpeg_buffer = NULL; melunko@47: } melunko@47: } melunko@47: melunko@47: static void melunko@47: gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv) melunko@47: { renatofilho@91: if (nuv->priv->src_video_pad) { renatofilho@91: gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad); renatofilho@91: nuv->priv->src_video_pad = NULL; melunko@47: } melunko@47: renatofilho@91: if (nuv->priv->src_audio_pad) { renatofilho@91: gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad); renatofilho@91: nuv->priv->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) { renatofilho@136: case GST_STATE_CHANGE_NULL_TO_READY: renatofilho@91: gst_nuv_demux_reset (GST_NUV_DEMUX (element)); renatofilho@133: gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element)); 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); renatofilho@136: if (ret == GST_STATE_CHANGE_FAILURE) { melunko@47: goto done; renatofilho@136: } melunko@47: melunko@47: switch (transition) { renatofilho@91: case GST_STATE_CHANGE_READY_TO_NULL: renatofilho@91: gst_nuv_demux_reset (GST_NUV_DEMUX (element)); renatofilho@66: gst_nuv_demux_destoy_src_pad (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: renatofilho@91: static void renatofilho@91: gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp) renatofilho@91: { renatofilho@91: guint64 interval = 0; renatofilho@168: return; renatofilho@91: renatofilho@91: if (gst_nuv_demux_get_time_duration (nuv) != GST_CLOCK_TIME_NONE) renatofilho@91: return; renatofilho@91: renatofilho@91: interval = current_timestamp - nuv->priv->last_update; renatofilho@91: renatofilho@91: if (interval > (10 * GST_SECOND)) { renatofilho@156: GstMessage* msg = NULL; renatofilho@91: gint64 average = 0; renatofilho@91: gint64 duration_bytes = gst_nuv_demux_get_bytes_duration (nuv); renatofilho@91: renatofilho@91: if (duration_bytes == GST_CLOCK_TIME_NONE) renatofilho@91: return; renatofilho@91: renatofilho@168: interval = gst_util_uint64_scale (1, current_timestamp - nuv->priv->time_start, GST_SECOND); renatofilho@168: if (interval > 0) { renatofilho@168: average = gst_util_uint64_scale (1 , nuv->priv->streamer_offset, interval); renatofilho@136: } renatofilho@168: if (average > 0) { renatofilho@168: nuv->priv->duration_average = gst_util_uint64_scale (GST_SECOND, duration_bytes, average); renatofilho@168: } renatofilho@168: renatofilho@168: nuv->priv->last_update = current_timestamp; renatofilho@168: msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_average); renatofilho@168: gst_element_post_message (GST_ELEMENT (nuv), msg); renatofilho@168: GST_DEBUG_OBJECT (nuv, "New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average); renatofilho@91: } renatofilho@91: } renatofilho@91: renatofilho@91: static gint64 renatofilho@91: gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv) renatofilho@91: { renatofilho@91: if (nuv->priv->duration_bytes == GST_CLOCK_TIME_NONE) { renatofilho@156: GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad); renatofilho@156: GstQuery *query = gst_query_new_duration (GST_FORMAT_BYTES); renatofilho@156: if (gst_pad_query (peer, query)) { renatofilho@156: gint64 duration; renatofilho@156: renatofilho@156: gst_query_parse_duration (query, NULL, &duration); renatofilho@91: nuv->priv->duration_bytes = duration; renatofilho@91: } renatofilho@156: gst_object_unref (peer); renatofilho@156: gst_query_unref (query); renatofilho@91: } renatofilho@91: return nuv->priv->duration_bytes; renatofilho@91: } renatofilho@91: renatofilho@91: static gint64 renatofilho@91: gst_nuv_demux_get_time_duration (GstNuvDemux *nuv) renatofilho@91: { renatofilho@91: if (nuv->priv->duration_time == GST_CLOCK_TIME_NONE) { renatofilho@156: GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad); renatofilho@156: GstQuery *query = gst_query_new_duration (GST_FORMAT_TIME); renatofilho@156: if (gst_pad_query (peer, query)) { renatofilho@156: gint64 duration; renatofilho@156: gst_query_parse_duration (query, NULL, &duration); renatofilho@91: nuv->priv->duration_time = duration; renatofilho@91: } renatofilho@156: gst_object_unref (peer); renatofilho@156: gst_query_unref (query); renatofilho@91: } renatofilho@91: return nuv->priv->duration_time; renatofilho@91: } renatofilho@91: renatofilho@181: #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) renatofilho@173: GstBuffer * renatofilho@181: gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes) renatofilho@173: { renatofilho@173: GstBuffer *buffer; renatofilho@173: GstBuffer *cur; renatofilho@173: guint8 *data; renatofilho@173: renatofilho@173: g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL); renatofilho@173: g_return_val_if_fail (nbytes > 0, NULL); renatofilho@173: renatofilho@173: GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes); renatofilho@173: renatofilho@173: /* we don't have enough data, return NULL. This is unlikely renatofilho@173: * as one usually does an _available() first instead of peeking a renatofilho@173: * random size. */ renatofilho@173: if (G_UNLIKELY (nbytes > adapter->size)) renatofilho@173: return NULL; renatofilho@173: renatofilho@173: /* our head buffer has enough data left, return it */ renatofilho@173: cur = adapter->buflist->data; renatofilho@173: if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) { renatofilho@173: GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer", renatofilho@173: nbytes); renatofilho@173: buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes); renatofilho@173: renatofilho@173: gst_adapter_flush (adapter, nbytes); renatofilho@173: renatofilho@173: return buffer; renatofilho@173: } renatofilho@173: renatofilho@173: data = gst_adapter_take (adapter, nbytes); renatofilho@173: if (data == NULL) renatofilho@173: return NULL; renatofilho@173: renatofilho@173: buffer = gst_buffer_new (); renatofilho@173: GST_BUFFER_DATA (buffer) = data; renatofilho@173: GST_BUFFER_MALLOCDATA (buffer) = data; renatofilho@173: GST_BUFFER_SIZE (buffer) = nbytes; renatofilho@173: renatofilho@173: return buffer; renatofilho@173: } renatofilho@181: #endif renatofilho@173: renatofilho@91: 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", renatofilho@163: plugin_init, VERSION, "LGPL", "NuvDemux", "") renatofilho@163: