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: rosfran@62: #define GETTEXT_PACKAGE "gst-plugins-nuvdemux-0.10.3" renatofilho@60: #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: renatofilho@66: #define DEMUX_INDEX_SIZE_MAX (100000) 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 */ renatofilho@66: static nuv_demux_index* gst_nuv_demux_index_new (void); renatofilho@66: static void gst_nuv_demux_index_destroy (nuv_demux_index *p_idx); rosfran@56: rosfran@56: /* NUV Demux indexes manipulation functions */ rosfran@56: static gint64 gst_nuv_demux_index_find_offset( GstNuvDemux *nuv, gint64 i_offset ); rosfran@56: static void gst_nuv_demux_index_append( GstNuvDemux *nuv, gint64 i_time, gint64 i_offset ); rosfran@56: static gint64 gst_nuv_demux_index_convert_time( GstNuvDemux *nuv, gint64 i_time ); rosfran@56: rosfran@56: /* NUV Demux plug-in time-line functions */ 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@66: static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad, renatofilho@66: 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@66: gst_pad_set_activatepush_function (nuv->sinkpad, renatofilho@66: gst_nuv_demux_sink_activate_push); renatofilho@66: 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: renatofilho@66: nuv->index_entries = gst_nuv_demux_index_new (); 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: } rosfran@56: rosfran@56: if ( nuv->index_entries != NULL ) { renatofilho@66: gst_nuv_demux_index_destroy (nuv->index_entries); rosfran@56: nuv->index_entries = NULL; rosfran@56: } 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: rosfran@56: melunko@47: /***************************************************************************** rosfran@56: * Indexes (timecode offset conversion) functions rosfran@56: *****************************************************************************/ rosfran@56: renatofilho@66: static nuv_demux_index* renatofilho@66: gst_nuv_demux_index_new (void) rosfran@56: { renatofilho@66: return g_new0 (nuv_demux_index, 1); rosfran@56: } rosfran@56: rosfran@56: static void renatofilho@66: gst_nuv_demux_index_destroy (nuv_demux_index *p_idx) rosfran@56: { renatofilho@66: if (p_idx != NULL) { renatofilho@66: g_free (p_idx->idx); renatofilho@66: p_idx->idx = NULL; renatofilho@66: g_free (p_idx); rosfran@57: } rosfran@56: } rosfran@56: rosfran@56: static void rosfran@56: gst_nuv_demux_index_append( GstNuvDemux *nuv, gint64 i_time, gint64 i_offset ) rosfran@56: { rosfran@56: nuv_demux_index *p_idx = nuv->index_entries; rosfran@57: rosfran@57: //if ( p_idx == NULL ) rosfran@57: // return; rosfran@57: rosfran@57: /* Be sure to append new entry (we don't insert point) */ renatofilho@66: if (p_idx != NULL && p_idx->i_idx > 0 && p_idx->idx[p_idx->i_idx-1].i_time >= i_time) rosfran@56: return; rosfran@56: rosfran@57: /* */ renatofilho@66: if (p_idx->i_idx >= p_idx->i_idx_max) rosfran@57: { renatofilho@66: if (p_idx->i_idx >= DEMUX_INDEX_SIZE_MAX) rosfran@57: { rosfran@57: /* Avoid too big index */ rosfran@57: const gint64 i_length = p_idx->idx[p_idx->i_idx-1].i_time - rosfran@57: p_idx->idx[0].i_time; rosfran@57: const gint i_count = DEMUX_INDEX_SIZE_MAX/2; rosfran@57: gint i, j; rosfran@56: rosfran@57: /* We try to reduce the resolution of the index by a factor 2 */ renatofilho@66: for (i = 1, j = 1; i < p_idx->i_idx; i++) rosfran@57: { renatofilho@66: if (p_idx->idx[i].i_time < j * i_length / i_count) rosfran@57: continue; rosfran@56: rosfran@57: p_idx->idx[j++] = p_idx->idx[i]; rosfran@57: } rosfran@57: p_idx->i_idx = j; rosfran@56: renatofilho@66: if (p_idx->i_idx > 3 * DEMUX_INDEX_SIZE_MAX / 4) rosfran@57: { rosfran@57: /* We haven't created enough space rosfran@57: * (This method won't create a good index but work for sure) */ renatofilho@66: for (i = 0; i < p_idx->i_idx/2; i++) rosfran@57: p_idx->idx[i] = p_idx->idx[2*i]; rosfran@57: p_idx->i_idx /= 2; rosfran@57: } rosfran@57: } rosfran@57: else rosfran@57: { rosfran@57: p_idx->i_idx_max += 1000; renatofilho@66: p_idx->idx = g_realloc (p_idx->idx, renatofilho@66: p_idx->i_idx_max*sizeof(nuv_demux_index_entry)); rosfran@57: } rosfran@57: } rosfran@56: rosfran@57: /* */ rosfran@57: p_idx->idx[p_idx->i_idx].i_time = i_time; rosfran@57: p_idx->idx[p_idx->i_idx].i_offset = i_offset; rosfran@56: rosfran@57: p_idx->i_idx++; rosfran@56: } rosfran@56: rosfran@56: static gint64 rosfran@56: gst_nuv_demux_index_convert_time( GstNuvDemux *nuv, gint64 i_time ) rosfran@56: { rosfran@56: nuv_demux_index *p_idx = nuv->index_entries; rosfran@56: rosfran@57: g_return_val_if_fail( p_idx != NULL , i_time ); rosfran@56: rosfran@57: gint i_min = 0; rosfran@57: gint i_max = p_idx->i_idx-1; rosfran@56: rosfran@57: /* Empty index */ rosfran@57: if( p_idx->i_idx <= 0 ) rosfran@57: return -1; rosfran@56: rosfran@57: /* Special border case */ rosfran@57: if( i_time <= p_idx->idx[0].i_time ) rosfran@57: return p_idx->idx[0].i_offset; rosfran@57: if( i_time >= p_idx->idx[i_max].i_time ) rosfran@57: return p_idx->idx[i_max].i_offset; rosfran@56: rosfran@57: /* Dicho */ rosfran@57: for( ;; ) rosfran@57: { rosfran@57: gint i_med; rosfran@56: rosfran@57: if( i_max - i_min <= 1 ) rosfran@57: break; rosfran@57: rosfran@57: i_med = (i_min+i_max)/2; rosfran@57: if( p_idx->idx[i_med].i_time < i_time ) rosfran@57: i_min = i_med; rosfran@57: else if( p_idx->idx[i_med].i_time > i_time ) rosfran@57: i_max = i_med; rosfran@57: else rosfran@57: return p_idx->idx[i_med].i_offset; rosfran@57: } rosfran@57: rosfran@57: /* return nearest in time */ rosfran@57: if( i_time - p_idx->idx[i_min].i_time < p_idx->idx[i_max].i_time - i_time ) rosfran@57: return p_idx->idx[i_min].i_offset; rosfran@57: else rosfran@57: return p_idx->idx[i_max].i_offset; rosfran@56: } rosfran@56: rosfran@56: static gint64 rosfran@56: gst_nuv_demux_index_find_offset( GstNuvDemux *nuv, gint64 i_offset ) rosfran@56: { rosfran@56: nuv_demux_index *p_idx = nuv->index_entries; rosfran@56: rosfran@57: g_return_val_if_fail( p_idx != NULL && p_idx->idx != NULL, i_offset ); rosfran@56: rosfran@57: gint i_min = 0; rosfran@57: gint i_max = p_idx->i_idx-1; rosfran@56: rosfran@57: /* Empty index */ rosfran@57: if( p_idx->i_idx <= 0 ) rosfran@57: return -1; rosfran@56: rosfran@57: /* Special border case */ rosfran@57: if( i_offset <= p_idx->idx[0].i_offset ) rosfran@57: return p_idx->idx[0].i_offset; rosfran@57: if( i_offset == p_idx->idx[i_max].i_offset ) rosfran@57: return p_idx->idx[i_max].i_offset; rosfran@57: if( i_offset > p_idx->idx[i_max].i_offset ) rosfran@57: return -1; rosfran@56: rosfran@57: /* Dicho */ rosfran@57: for( ;; ) rosfran@57: { rosfran@57: gint i_med; rosfran@56: rosfran@57: if ( i_max - i_min <= 1 ) rosfran@57: break; rosfran@57: rosfran@57: i_med = (i_min+i_max)/2; rosfran@57: if( p_idx->idx[i_med].i_offset < i_offset ) rosfran@57: i_min = i_med; rosfran@57: else if( p_idx->idx[i_med].i_offset > i_offset ) rosfran@57: i_max = i_med; rosfran@57: else rosfran@57: return p_idx->idx[i_med].i_offset; rosfran@57: } rosfran@57: rosfran@57: /* return nearest */ rosfran@57: if( i_offset - p_idx->idx[i_min].i_offset < p_idx->idx[i_max].i_offset - i_offset ) rosfran@57: return p_idx->idx[i_min].i_offset; rosfran@57: else rosfran@57: return p_idx->idx[i_max].i_offset; rosfran@56: } 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]); 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: rosfran@57: h->i_timecode = GST_READ_UINT32_LE (&data[4]); rosfran@57: rosfran@57: melunko@47: h->i_length = GST_READ_UINT32_LE (&data[8]); 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: 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: 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_pad_set_event_function (nuv->src_video_pad, melunko@47: gst_nuv_demux_handle_src_event); rosfran@56: 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); rosfran@56: 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; rosfran@56: renatofilho@60: renatofilho@60: GST_DEBUG_OBJECT (nuv, "starting append index"); rosfran@57: /* append the frame's header timecode field, and the actual offset */ rosfran@56: gst_nuv_demux_index_append( nuv, h->i_timecode, nuv->offset ); rosfran@57: 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: rosfran@57: /* search for a valid timecode in the indexes list (find the nearest valid timecode) */ rosfran@56: if ( h->i_timecode < 0 ) { rosfran@57: /* convert this actual timecode to a valid index in the timecode's table */ renatofilho@66: gint64 pos = gst_nuv_demux_index_convert_time (nuv, h->i_timecode); rosfran@57: rosfran@56: /* find the nearest empty frame slot */ renatofilho@66: gint64 near_off = gst_nuv_demux_index_find_offset (nuv, pos); renatofilho@66: rosfran@56: /* just get the timecode from the timecode's table */ rosfran@56: GST_BUFFER_TIMESTAMP (buf) = nuv->index_entries->idx[near_off].i_time * GST_MSECOND; rosfran@56: } else { rosfran@57: GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND; rosfran@56: } melunko@47: } melunko@47: renatofilho@60: GST_DEBUG_OBJECT (nuv, "index appended"); 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@56: 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); renatofilho@66: if (ret != GST_FLOW_OK) { renatofilho@66: GST_WARNING_OBJECT (nuv, "error pushing on srcpad %s, is linked? = %d", renatofilho@66: gst_pad_get_name (nuv->src_video_pad), gst_pad_is_linked (nuv->src_video_pad)); renatofilho@66: } melunko@47: nuv->video_offset++; melunko@47: break; melunko@47: } melunko@47: case 'A': melunko@47: { rosfran@56: if (h->i_length == 0) rosfran@56: break; rosfran@56: 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); renatofilho@66: if (ret != GST_FLOW_OK) { renatofilho@66: GST_WARNING_OBJECT (nuv, "Error pushing on srcpad %s, is linked? = %d", renatofilho@66: gst_pad_get_name (nuv->src_audio_pad), gst_pad_is_linked (nuv->src_audio_pad)); renatofilho@66: } melunko@47: nuv->audio_offset++; melunko@47: break; melunko@47: } melunko@47: case 'S': melunko@47: { renatofilho@66: /* melunko@47: switch (h->i_compression) { melunko@47: case 'V': renatofilho@66: GST_DEBUG_OBJECT (nuv, "Video New Segment"); renatofilho@66: nuv->video_offset = 0; melunko@47: gst_pad_push_event (nuv->src_video_pad, rosfran@56: gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, renatofilho@66: 0)); melunko@47: break; melunko@47: case 'A': renatofilho@66: GST_DEBUG_OBJECT (nuv, "Audio New Segment"); 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: break; melunko@47: default: melunko@47: break; melunko@47: } renatofilho@66: */ 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: renatofilho@66: GST_DEBUG_OBJECT (nuv, "data sent"); renatofilho@60: 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); renatofilho@60: 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: renatofilho@66: g_debug ("PAUSE"); renatofilho@66: 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) { renatofilho@66: guint8 *data = NULL; renatofilho@66: renatofilho@66: data = (guint8 *) gst_adapter_take (nuv->adapter, size); renatofilho@66: *buffer = gst_buffer_new (); renatofilho@66: 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); renatofilho@60: GST_DEBUG_OBJECT (nuv, "starting PULL mode"); melunko@47: } else { melunko@47: nuv->mode = 1; melunko@47: nuv->adapter = gst_adapter_new (); melunko@47: res = gst_pad_activate_push (sinkpad, TRUE); renatofilho@60: GST_DEBUG_OBJECT (nuv, "starting PUSH mode"); 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: renatofilho@66: static gboolean renatofilho@66: gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active) renatofilho@66: { renatofilho@66: GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad)); renatofilho@66: renatofilho@66: if (active) { renatofilho@66: GST_DEBUG_OBJECT (nuv, "activating push/chain function"); renatofilho@66: } else { renatofilho@66: GST_DEBUG_OBJECT (nuv, "deactivating push/chain function"); renatofilho@66: } renatofilho@66: renatofilho@66: gst_object_unref (nuv); renatofilho@66: renatofilho@66: return TRUE; renatofilho@66: } renatofilho@66: 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@60: GST_DEBUG_OBJECT (nuv, "PUSH adapter %d", gst_adapter_available (nuv->adapter)); renatofilho@60: renatofilho@66: gst_object_unref (nuv); renatofilho@66: 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: { rosfran@62: GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; renatofilho@66: 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, ""); 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", "")