gst-gmyth/nuvdemux/gstnuvdemux.c
author rosfran
Fri May 25 18:25:50 2007 +0100 (2007-05-25)
branchtrunk
changeset 715 e01c60057f07
parent 713 e8fe96f87916
child 716 1ee5df4c9ccd
permissions -rw-r--r--
[svn r721] Added GStreamer-like indentation.
     1 /* GStreamer
     2  * Copyright (C) <2006> Renato Araujo Oliveira Filho <renato.filho@indt.org.br>
     3  *                      Rosfran Borges <rosfran.borges@indt.org.br>
     4  *
     5  * This library is free software; you can redistribute it and/or
     6  * modify it under the terms of the GNU Library General Public
     7  * License as published by the Free Software Foundation; either
     8  * version 2 of the License, or (at your option) any later version.
     9  *
    10  * This library is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  * Library General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU Library General Public
    16  * License along with this library; if not, write to the
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    18  * Boston, MA 02111-1307, USA.
    19  */
    20 /* Element-Checklist-Version: 5 */
    21 
    22 /**
    23  * SECTION:element-nuvdemux
    24  *
    25  * <refsect2>
    26  * <para>
    27  * Demuxes an .nuv file into raw or compressed audio and/or video streams.
    28  * </para>
    29  * <para>
    30  * This element currently only supports pull-based scheduling.
    31  * </para>
    32  * <title>Example launch line</title>
    33  * <para>
    34  * <programlisting>
    35  * gst-launch filesrc test.nuv ! nuvdemux name=demux  demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink   demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
    36  * </programlisting>
    37  * Play (parse and decode) an .nuv file and try to output it to
    38  * an automatically detected soundcard and videosink. If the NUV file contains
    39  * compressed audio or video data, this will only work if you have the
    40  * right decoder elements/plugins installed.
    41  * </para>
    42  * </refsect2>
    43  *
    44  */
    45 
    46 #ifdef HAVE_CONFIG_H
    47 #include "config.h"
    48 #endif
    49 
    50 #include <gst/gst.h>
    51 #include <gst/gsterror.h>
    52 #include <gst/gstplugin.h>
    53 #include <string.h>
    54 #include <math.h>
    55 
    56 #include "glib/gi18n.h"
    57 #include "gstnuvdemux.h"
    58 
    59 #define GST_NUV_DEMUX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NUV_DEMUX, GstNuvDemuxPrivate))
    60 
    61 GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug);
    62 #define GST_CAT_DEFAULT nuvdemux_debug
    63 #define GST_FLOW_ERROR_NO_DATA  -101
    64 #define GST_FLOW_ERROR_EOS	-102
    65 
    66 enum
    67 {
    68    NUV_PUSH_MODE = 0,
    69    NUV_PULL_MODE
    70 };
    71 
    72 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
    73 
    74 static const GstElementDetails gst_nuv_demux_details =
    75 GST_ELEMENT_DETAILS ("Nuv demuxer",
    76     "Codec/Demuxer",
    77     "Demultiplex a .nuv file into audio and video",
    78     "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
    79     "Rosfran Borges <rosfran.borges@indt.org.br>");
    80 
    81 
    82 /* file header */
    83 typedef struct
    84 {
    85     gchar id[12];       /* "NuppelVideo\0" or "MythTVVideo\0" */
    86     gchar version[5];    /* "x.xx\0" */
    87 
    88     gint  i_width;
    89     gint  i_height;
    90     gint  i_width_desired;
    91     gint  i_height_desired;
    92 
    93     gchar i_mode;            /* P progressive, I interlaced */
    94 
    95     gdouble  d_aspect;       /* 1.0 squared pixel */
    96     gdouble  d_fps;
    97     //fps num/denom
    98     gint     i_fpsn;
    99     gint     i_fpsd;
   100 
   101     gint     i_video_blocks; /* 0 no video, -1 unknown */
   102     gint     i_audio_blocks;
   103     gint     i_text_blocks;
   104 
   105     gint     i_keyframe_distance;
   106 
   107 } nuv_header;
   108 
   109 /* frame header */
   110 typedef struct
   111 {
   112     gchar i_type;        /* A: audio, V: video, S: sync; T: test
   113                            R: Seekpoint (string:RTjjjjjjjj)
   114                            D: Extra data for codec */
   115     gchar i_compression; /* V: 0 uncompressed
   116                               1 RTJpeg
   117                               2 RTJpeg+lzo
   118                               N black frame
   119                               L copy last
   120                            A: 0 uncompressed (44100 1-bits, 2ch)
   121                               1 lzo
   122                               2 layer 2
   123                               3 layer 3
   124                               F flac
   125                               S shorten
   126                               N null frame loudless
   127                               L copy last
   128                             S: B audio and vdeo sync point
   129                                A audio sync info (timecode == effective
   130                                     dsp frequency*100)
   131                                V next video sync (timecode == next video
   132                                     frame num)
   133                                S audio,video,text correlation */
   134     gchar i_keyframe;    /* 0 keyframe, else no no key frame */
   135     guint8 i_filters;  /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
   136                            0x02: gauss 5 pixel (8,1,1,1,1)/12
   137                            0x04: cartoon filter */
   138 
   139     gint32 i_timecode;     /* ms */
   140 
   141     gint i_length;       /* V,A,T: length of following data
   142                            S: length of packet correl */
   143 } nuv_frame_header;
   144 
   145 
   146 /* FIXME Not sure of this one */
   147 typedef struct
   148 {
   149     gint             i_version;
   150     guint32	     i_video_fcc;
   151 
   152     guint32	     i_audio_fcc;
   153     gint             i_audio_sample_rate;
   154     gint             i_audio_bits_per_sample;
   155     gint             i_audio_channels;
   156     gint             i_audio_compression_ratio;
   157     gint             i_audio_quality;
   158     gint             i_rtjpeg_quality;
   159     gint             i_rtjpeg_luma_filter;
   160     gint             i_rtjpeg_chroma_filter;
   161     gint             i_lavc_bitrate;
   162     gint             i_lavc_qmin;
   163     gint             i_lavc_qmax;
   164     gint             i_lavc_maxqdiff;
   165     gint64           i_seekable_offset;
   166     gint64           i_keyframe_adjust_offset;
   167 
   168 } nuv_extended_header;
   169 
   170 typedef struct
   171 {
   172   gint64 timecode;
   173   gint64 offset;
   174 
   175 } frame_index_data;
   176 
   177 typedef enum {
   178   GST_NUV_DEMUX_START,
   179   GST_NUV_DEMUX_HEADER_DATA,
   180   GST_NUV_DEMUX_EXTRA_DATA,
   181   GST_NUV_DEMUX_MPEG_DATA,
   182   GST_NUV_DEMUX_EXTEND_HEADER,
   183   GST_NUV_DEMUX_EXTEND_HEADER_DATA,
   184   GST_NUV_DEMUX_INDEX_CREATE,
   185   GST_NUV_DEMUX_FRAME_HEADER,
   186   GST_NUV_DEMUX_MOVI,
   187   GST_NUV_DEMUX_INVALID_DATA
   188 } GstNuvDemuxState;
   189 
   190 struct _GstNuvDemuxPrivate {
   191   /* used for indicate the mode */
   192   guint         mode;
   193 
   194   /* used on push mode */
   195   GstAdapter    *adapter;
   196 
   197   /* pads */
   198   GstPad        *sinkpad;
   199   GstPad        *src_video_pad;
   200   GstPad        *src_audio_pad;
   201 
   202   /* Flow control */
   203   GstFlowReturn     last_video_return;
   204   GstFlowReturn     last_audio_return;
   205   gboolean          more_data;
   206   gboolean	    	eos;
   207   gboolean 			new_file;
   208   guint 			segment;
   209 
   210   /* NUV decoding state */
   211   GstNuvDemuxState  state;
   212   guint64           offset;
   213 
   214   /* duration information */
   215   guint64            duration_bytes;
   216   guint64            duration_time;
   217   guint64            segment_stop;
   218   guint64            segment_start;
   219 
   220   /* segment control info */
   221   gboolean          new_audio_segment;
   222   gboolean          new_video_segment;
   223 
   224   /* Mpeg ExtraData */
   225   guint64           mpeg_data_size;
   226   GstBuffer         *mpeg_buffer;
   227 
   228   /* Headers */
   229   nuv_header h;
   230   nuv_extended_header eh;
   231   nuv_frame_header fh;
   232 
   233   /* anothers info */
   234   guint64           header_lengh;
   235   gint64            time_start;
   236   gint64            time_diff;
   237   gint64            time_qos;
   238   guint64           last_frame_time;
   239   GSList            *index;
   240 };
   241 
   242 
   243 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
   244     GST_PAD_SINK,
   245     GST_PAD_ALWAYS,
   246     GST_STATIC_CAPS ("video/x-nuv"));
   247 
   248 static GstStaticPadTemplate audio_src_template =
   249 GST_STATIC_PAD_TEMPLATE ("audio_src",
   250     GST_PAD_SRC,
   251     GST_PAD_SOMETIMES,
   252     GST_STATIC_CAPS_ANY);
   253 
   254 static GstStaticPadTemplate video_src_template =
   255 GST_STATIC_PAD_TEMPLATE ("video_src",
   256     GST_PAD_SRC,
   257     GST_PAD_SOMETIMES,
   258     GST_STATIC_CAPS_ANY);
   259 
   260 static void gst_nuv_demux_dispose (GObject * object);
   261 static void gst_nuv_demux_finalize (GObject * object);
   262 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
   263     GstStateChange transition);
   264 static void gst_nuv_demux_loop (GstPad * pad);
   265 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
   266 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
   267 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
   268     gboolean active);
   269 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
   270     gboolean active);
   271 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
   272 static gboolean gst_nuv_demux_sink_event    (GstPad *pad, GstEvent *event);
   273 static gboolean gst_nuv_demux_srcpad_event  (GstPad * pad, GstEvent * event);
   274 static frame_index_data * gst_nuv_demux_do_seek_index (GstNuvDemux *nuv, gint64 seek_pos, 
   275   gint64 segment_stop, GstFormat format);
   276 
   277 
   278 static GstFlowReturn gst_nuv_demux_move_bytes (GstNuvDemux * nuv, guint64 size);
   279 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
   280     gboolean move, GstBuffer ** buffer);
   281 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
   282 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
   283 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
   284 static void gst_nuv_demux_create_seek_index (GstNuvDemux * nuv);
   285 
   286 
   287 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
   288 GstBuffer * gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes);
   289 #endif
   290 
   291 
   292 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
   293 
   294 /******************************************************************************
   295  * Utils function
   296  ******************************************************************************/
   297 #if G_BYTE_ORDER == G_BIG_ENDIAN
   298 static inline gdouble
   299 _gdouble_swap_le_be (gdouble * d)
   300 {
   301   union
   302   {
   303     guint64 i;
   304     gdouble d;
   305   } u;
   306 
   307   u.d = *d;
   308   u.i = GUINT64_SWAP_LE_BE (u.i);
   309   return u.d;
   310 }
   311 
   312 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
   313 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
   314 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
   315 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
   316 
   317 static void
   318 double2fraction (double in, int *num, int *denom)
   319 {
   320     if (in == 29.97) {
   321         *num = 30000;
   322         *denom = 1001;
   323     } else if (in == 23.976) {
   324         *num = 24000;
   325         *denom = 1001;
   326     } else {
   327         *denom = 1;
   328         while (in - floor(in) >= 0.1) {
   329             *denom *= 10;
   330             in *= 10.0;
   331         }
   332         *num = (int)floor(in);
   333     }
   334 }
   335 
   336 /* GObject Functions */
   337 
   338 static void
   339 gst_nuv_demux_base_init (gpointer klass)
   340 {
   341   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
   342 
   343   gst_element_class_add_pad_template (element_class,
   344       gst_static_pad_template_get (&audio_src_template));
   345 
   346   gst_element_class_add_pad_template (element_class,
   347       gst_static_pad_template_get (&video_src_template));
   348 
   349   gst_element_class_add_pad_template (element_class,
   350       gst_static_pad_template_get (&sink_template));
   351   gst_element_class_set_details (element_class, &gst_nuv_demux_details);
   352 }
   353 
   354 static void
   355 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
   356 {
   357   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   358   GObjectClass *gobject_class = (GObjectClass *) klass;
   359 
   360   GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
   361       0, "Demuxer for NUV streams");
   362 
   363   parent_class = g_type_class_peek_parent (klass);
   364 
   365   gobject_class->dispose = gst_nuv_demux_dispose;
   366   gobject_class->finalize = gst_nuv_demux_finalize;
   367   gstelement_class->change_state = gst_nuv_demux_change_state;
   368 
   369   g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate));
   370 }
   371 
   372 static void
   373 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
   374 {
   375   nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv);
   376   nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
   377 
   378   /* creating adapter */
   379   nuv->priv->mode = NUV_PUSH_MODE;
   380   nuv->priv->adapter = gst_adapter_new ();
   381 
   382   nuv->priv->new_audio_segment = TRUE;
   383   nuv->priv->new_video_segment = TRUE;
   384 
   385   gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate);
   386   gst_pad_set_activatepull_function (nuv->priv->sinkpad,
   387       gst_nuv_demux_sink_activate_pull);
   388   gst_pad_set_activatepush_function (nuv->priv->sinkpad,
   389       gst_nuv_demux_sink_activate_push);
   390   gst_pad_set_chain_function (nuv->priv->sinkpad,
   391       GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
   392   gst_pad_set_event_function (nuv->priv->sinkpad,
   393       GST_DEBUG_FUNCPTR (gst_nuv_demux_sink_event));
   394 
   395 
   396   gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad);
   397 
   398 }
   399 
   400 static void
   401 gst_nuv_demux_dispose (GObject * object)
   402 {
   403   GstNuvDemux *nuv = GST_NUV_DEMUX (object);
   404 
   405 
   406   if (nuv->priv->mpeg_buffer != NULL) {
   407     gst_buffer_unref (nuv->priv->mpeg_buffer);
   408   }
   409 
   410   gst_nuv_demux_reset (GST_NUV_DEMUX (object));
   411   gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (object));
   412 
   413   if (nuv->priv->adapter != NULL) {
   414     gst_object_unref (nuv->priv->adapter);
   415   }
   416 }
   417 
   418 static void
   419 gst_nuv_demux_finalize (GObject * object)
   420 {
   421   G_OBJECT_CLASS (parent_class)->finalize (object);
   422 }
   423 
   424 
   425 /* HeaderLoad:
   426  */
   427 static GstFlowReturn
   428 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header *h)
   429 {
   430   GstBuffer *buffer = NULL;
   431   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
   432 
   433   if ((res != GST_FLOW_OK) || (buffer == NULL)) {
   434       goto done;
   435   }
   436 
   437   if (h != NULL) {
   438   	memcpy (h->id, buffer->data, 12);
   439 	memcpy (h->version, buffer->data + 12, 5);
   440   	h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
   441   	h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
   442   	h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
   443   	h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
   444   	h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
   445   	h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
   446   	h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
   447   	/* get the num and denom values from fps */
   448   	double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
   449   	h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
   450   	h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
   451   	h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
   452   	h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
   453 
   454   	GST_DEBUG_OBJECT (nuv,
   455     	"nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
   456       	h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
   457       	h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
   458       	h->i_keyframe_distance);
   459   }
   460 
   461 done:
   462   if (buffer != NULL) {
   463      gst_buffer_unref (buffer);
   464      buffer = NULL;
   465   }
   466   return res;
   467 }
   468 
   469 static GstFlowReturn
   470 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
   471 {
   472   GstFlowReturn res;
   473 
   474   if (nuv->priv->new_file) 
   475 	  res = gst_nuv_demux_header_load (nuv, NULL);
   476   else
   477 	  res = gst_nuv_demux_header_load (nuv, &nuv->priv->h);
   478 
   479   if (res == GST_FLOW_OK)
   480     nuv->priv->state = GST_NUV_DEMUX_EXTRA_DATA;
   481   return res;
   482 }
   483 
   484 /*
   485  * Read NUV file tag
   486  */
   487 static GstFlowReturn
   488 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
   489 {
   490   GstFlowReturn res = GST_FLOW_OK;
   491   GstBuffer *file_header = NULL;
   492 
   493   res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
   494   if (res == GST_FLOW_OK) {
   495     if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
   496         strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
   497       nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
   498     } else {
   499       GST_DEBUG_OBJECT (nuv, "error parsing file header");
   500       nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
   501       res = GST_FLOW_ERROR;
   502     }
   503   }
   504 
   505   if (file_header != NULL) {
   506     gst_buffer_unref (file_header);
   507     file_header = NULL;
   508   }
   509   return res;
   510 }
   511 
   512 /* FrameHeaderLoad:
   513  */
   514 static GstFlowReturn
   515 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header *h)
   516 {
   517   unsigned char *data;
   518   GstBuffer *buf = NULL;
   519 
   520   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &buf);
   521 
   522   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   523       goto done;
   524   }
   525 
   526   if (h == NULL)
   527 	goto done;
   528 		  
   529   data = buf->data;
   530 <<<<<<< .mine
   531 =======
   532   /* verifies if it is a MythTV file header */
   533   if ( ( data[0] == 'M' || data[0] == 'N') && data[6] == 'V' )  {
   534 	  nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
   535 	  goto done;
   536   }
   537   /* it is not a NUV file header, so now can move the 12 bytes */
   538   res = gst_nuv_demux_move_bytes (nuv, 12);
   539 
   540 >>>>>>> .r719
   541   h->i_type = GPOINTER_TO_INT (data[0]);
   542   h->i_compression = GPOINTER_TO_INT (data[1]);
   543   h->i_keyframe = GPOINTER_TO_INT (data[2]);
   544   h->i_filters = GPOINTER_TO_INT (data[3]);
   545   h->i_timecode = GST_READ_UINT32_LE (&data[4]);
   546   h->i_length = GST_READ_UINT32_LE (&data[8]);
   547 
   548   GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
   549       h->i_type,
   550       h->i_compression ? h->i_compression : ' ',
   551       h->i_keyframe ? h->i_keyframe : ' ',
   552       h->i_filters, h->i_timecode, h->i_length);
   553 
   554 done:  
   555   if (buf != NULL) {
   556       gst_buffer_unref (buf);
   557       buf = NULL;
   558   }
   559 
   560   return res;
   561 }
   562 
   563 static GstFlowReturn
   564 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
   565     nuv_extended_header * h)
   566 {
   567   unsigned char *data;
   568   GstBuffer *buff = NULL;
   569 
   570   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
   571 
   572   if ((res != GST_FLOW_OK) || (buff == NULL)) {
   573       goto done;
   574   }
   575 
   576   if (h == NULL)
   577     goto done;
   578 
   579   data = buff->data;
   580   h->i_version = GST_READ_UINT32_LE (&data[0]);
   581   h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
   582   h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
   583   h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
   584   h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
   585   h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
   586   h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
   587   h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
   588   h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
   589   h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
   590   h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
   591   h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
   592   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
   593   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
   594   h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
   595   h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
   596   h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
   597 
   598   GST_DEBUG_OBJECT (nuv,
   599       "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
   600       "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
   601       h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
   602       h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
   603       h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
   604       h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
   605       h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
   606       h->i_keyframe_adjust_offset);
   607 
   608 done:
   609   if (buff != NULL) {
   610     gst_buffer_unref (buff);
   611     buff = NULL;
   612   }
   613   return res;
   614 }
   615 
   616 
   617 /* Query Functions */
   618 static const GstQueryType *
   619 gst_nuv_demux_get_src_query_types (GstPad * pad)
   620 {
   621   static const GstQueryType src_types[] = {
   622     GST_QUERY_POSITION,
   623     GST_QUERY_DURATION,
   624     0
   625   };
   626 
   627   return src_types;
   628 }
   629 
   630 static gboolean
   631 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
   632 {
   633   gboolean res = FALSE;
   634   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   635 
   636 
   637   switch (GST_QUERY_TYPE (query)) 
   638   {
   639     case GST_QUERY_POSITION:
   640 	{
   641 		GstFormat format;
   642 		gst_query_parse_position (query, &format, NULL); 
   643 		switch (format) 
   644 		{
   645 			case GST_FORMAT_TIME:
   646 				if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
   647 			        gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
   648 			        res = TRUE;
   649 				}
   650 			  	break;
   651 			default:				
   652 				break;
   653 		}
   654 		break;
   655 	}		
   656     case GST_QUERY_DURATION:
   657 	{
   658 	  GstFormat format;		
   659 	  gst_query_parse_duration (query, &format, NULL); 
   660 	  switch (format)
   661 	  {
   662 			case GST_FORMAT_TIME:			 
   663 				if (nuv->priv->duration_time != GST_CLOCK_TIME_NONE) {
   664 			        gst_query_set_duration (query, GST_FORMAT_TIME, nuv->priv->duration_time);
   665 				    res = TRUE;
   666 			    }
   667 				break;
   668 			default:				
   669 			    break;
   670 	  }
   671 	  break;
   672   	}
   673 	default:
   674 	  break;
   675   }
   676 
   677   if (res==FALSE) {
   678 	res = gst_pad_query_default (pad, query);		  
   679   } 
   680 
   681   gst_object_unref (nuv);
   682   return res;
   683 }
   684 
   685 static GstPad*
   686 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
   687 {
   688     GstPad *pad = NULL;
   689     pad = gst_pad_new_from_static_template (template, name);
   690     gst_pad_set_caps (pad, caps);
   691     gst_pad_set_active (pad, TRUE);
   692     gst_pad_use_fixed_caps (pad);
   693     gst_element_add_pad (GST_ELEMENT (nuv), pad);
   694 
   695     gst_pad_set_event_function (pad,
   696       GST_DEBUG_FUNCPTR (gst_nuv_demux_srcpad_event));
   697 
   698     gst_pad_set_query_type_function (pad,
   699       GST_DEBUG_FUNCPTR (gst_nuv_demux_get_src_query_types));
   700 
   701     gst_pad_set_query_function (pad,
   702       GST_DEBUG_FUNCPTR (gst_nuv_demux_handle_src_query));
   703       
   704 
   705     return pad;
   706 }
   707 
   708 static void
   709 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
   710 {
   711   if (nuv->priv->h.i_video_blocks != 0) {
   712     GstCaps *video_caps = NULL;
   713 
   714     video_caps = gst_caps_new_simple ("video/x-divx",
   715         "divxversion", G_TYPE_INT, 4,
   716         "width", G_TYPE_INT, nuv->priv->h.i_width,
   717         "height", G_TYPE_INT, nuv->priv->h.i_height,
   718         "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
   719         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
   720         "pixel-aspect-ratio", GST_TYPE_FRACTION,
   721         (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
   722 
   723     nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
   724     gst_caps_unref (video_caps);
   725   }
   726 
   727   if (nuv->priv->h.i_audio_blocks != 0) {
   728     GstCaps *audio_caps = NULL;
   729 
   730     audio_caps = gst_caps_new_simple ("audio/mpeg",
   731         "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
   732         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
   733         "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
   734         "layer", G_TYPE_INT, 3, // fixme: magic number
   735         "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
   736     
   737     nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
   738     gst_caps_unref (audio_caps);
   739   }
   740 
   741   gst_element_no_more_pads (GST_ELEMENT (nuv));
   742 }
   743 
   744 static gboolean
   745 gst_nuv_demux_validate_header (nuv_frame_header *h)
   746 {
   747   gboolean valid = FALSE;
   748   //g_usleep (1 * G_USEC_PER_SEC );
   749   switch  (h->i_type) {
   750 /*
   751     case 'V':
   752       if (h->i_compression == 0 ||
   753           h->i_compression == 1 ||
   754           h->i_compression == 2 ||
   755           h->i_compression == 'N' ||
   756           h->i_compression == 'L') {
   757          valid = TRUE;
   758       }
   759       break;
   760     case 'A':
   761       if (h->i_compression == 0 ||
   762           h->i_compression == 1 ||
   763           h->i_compression == 2 ||
   764           h->i_compression == 3 ||
   765           h->i_compression == 'F' ||
   766           h->i_compression == 'S' ||
   767           h->i_compression == 'N' ||
   768           h->i_compression == 'L') {
   769          valid = TRUE;
   770       }
   771       break;
   772     case 'S':
   773       if (h->i_compression == 'B' ||
   774           h->i_compression == 'A' ||
   775           h->i_compression == 'V' ||
   776           h->i_compression == 'S') {
   777          valid = TRUE;
   778       }
   779       break;
   780 */
   781     case 'A':
   782     case 'V':
   783     case 'S':
   784     case 'R':
   785     case 'D':
   786     case 'Q':
   787       valid  = TRUE;
   788       break;
   789     default:
   790       valid = FALSE;
   791   }
   792 
   793   return valid;
   794 }
   795 
   796 static GstFlowReturn
   797 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
   798 {
   799   GstFlowReturn ret = GST_FLOW_OK;
   800   gboolean valid = FALSE;
   801 
   802   do {
   803     ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
   804     if (ret != GST_FLOW_OK) {
   805       return ret;
   806     }
   807 
   808     if (gst_nuv_demux_validate_header (&nuv->priv->fh) == TRUE)
   809       valid = TRUE;
   810 
   811   } while (valid == FALSE);
   812 
   813   nuv->priv->state = GST_NUV_DEMUX_MOVI;
   814   return ret;
   815 }
   816 
   817 static gboolean
   818 gst_nuv_combine_flow (GstNuvDemux *nuv)
   819 {
   820     GstFlowReturn ret_video = nuv->priv->last_video_return;
   821     GstFlowReturn ret_audio = nuv->priv->last_audio_return;
   822 
   823     if ((ret_video != GST_FLOW_OK) &&
   824         (ret_audio != GST_FLOW_OK))
   825         return FALSE;
   826 
   827     if (GST_FLOW_IS_FATAL (ret_video))
   828         return FALSE;
   829 
   830     if (GST_FLOW_IS_FATAL (ret_audio))
   831         return FALSE;
   832 
   833     return TRUE;
   834 }
   835 
   836 static GstFlowReturn
   837 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
   838 {
   839   GstFlowReturn ret = GST_FLOW_OK;
   840   GstPad *pad = NULL;
   841   guint64 timestamp;
   842   GstBuffer *buf = NULL;
   843   nuv_frame_header h;
   844 
   845   h = nuv->priv->fh;
   846 
   847   if (h.i_type == 'R') {
   848     goto done;
   849   }
   850 
   851   if (h.i_length > 0) {
   852     ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
   853     if ((ret != GST_FLOW_OK) || (buf == NULL)) {
   854       goto done;
   855     }
   856 
   857     if ((h.i_timecode < 0)) {
   858       h.i_timecode = 0;
   859       //goto done;
   860     }
   861 
   862     timestamp = h.i_timecode * GST_MSECOND;
   863     GST_BUFFER_TIMESTAMP (buf) = timestamp;
   864   }
   865   else {
   866     goto done;
   867   }
   868 
   869 
   870   switch (h.i_type) {
   871     case 'V':
   872     {
   873       pad = nuv->priv->src_video_pad;
   874 
   875       if (nuv->priv->new_video_segment) {
   876 
   877         /* send new segment event*/
   878         gst_pad_push_event (nuv->priv->src_video_pad,
   879           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   880                                      GST_CLOCK_TIME_NONE, 0));
   881 
   882         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   883             nuv->priv->time_start = timestamp;
   884         }
   885         nuv->priv->new_video_segment = FALSE;
   886       }
   887 
   888       break;
   889     }
   890     case 'A':
   891     {
   892       pad = nuv->priv->src_audio_pad;
   893 
   894       if (nuv->priv->new_audio_segment) {
   895         /* send new segment event*/
   896         gst_pad_push_event (nuv->priv->src_audio_pad,
   897           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   898                                      GST_CLOCK_TIME_NONE, 0));
   899 
   900         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   901             nuv->priv->time_start = timestamp;
   902         }
   903         nuv->priv->new_audio_segment = FALSE;
   904       }
   905 
   906       break;
   907     }
   908     case 'S':
   909    {
   910       switch (h.i_compression) {
   911         case 'V':
   912           GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
   913           gst_pad_push_event (nuv->priv->src_video_pad,
   914               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
   915               		GST_CLOCK_TIME_NONE, 0));
   916           break;
   917         case 'A':
   918           GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
   919           gst_pad_push_event (nuv->priv->src_audio_pad,
   920               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   921               		GST_CLOCK_TIME_NONE, 0));
   922           break;
   923         default:
   924           break;
   925       }
   926       goto done;
   927     }
   928     default:
   929       break;
   930   }
   931 
   932   if ((buf != NULL) && (pad != NULL)) {
   933       /* pushing the buffer */
   934       gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
   935       ret = gst_pad_push (pad, buf);
   936       buf = NULL;
   937 
   938       if (ret != GST_FLOW_OK) {
   939         GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
   940 
   941         if (pad == nuv->priv->src_video_pad) {
   942             nuv->priv->last_video_return = ret;
   943         }
   944         else if (pad == nuv->priv->src_audio_pad) {
   945             nuv->priv->last_audio_return = ret;
   946         }
   947 
   948         /* verify anothers flow if is necessary stop task */
   949         if (gst_nuv_combine_flow (nuv) != FALSE) {
   950           ret = GST_FLOW_OK;
   951         } else {
   952 	  GST_WARNING_OBJECT (nuv, "error: on push");
   953 	}
   954 
   955       }
   956   } 
   957 
   958 done:
   959   if (buf != NULL) {
   960       gst_buffer_unref (buf);
   961       buf = NULL;
   962   }
   963   if (ret == GST_FLOW_OK) {
   964      nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   965      memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
   966   }
   967   return ret;
   968 }
   969 
   970 static GstFlowReturn
   971 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
   972 {
   973   GstFlowReturn ret = GST_FLOW_OK;
   974 
   975   /* ffmpeg extra data */
   976   if (nuv->priv->new_file) {
   977 	GstBuffer *buf;
   978 	ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE, &buf);
   979 	gst_buffer_unref (buf);
   980   } else {
   981     ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
   982       &nuv->priv->mpeg_buffer);
   983   }
   984 
   985   if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
   986     return ret;
   987   }
   988 
   989   GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
   990   nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   991   return ret;
   992 }
   993 
   994 static GstFlowReturn
   995 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
   996 {
   997   GstFlowReturn ret = GST_FLOW_OK;
   998 
   999   /* Load 'D' */
  1000   nuv_frame_header h;
  1001 
  1002   if (nuv->priv->new_file) 
  1003 	  ret = gst_nuv_demux_frame_header_load (nuv, NULL);
  1004   else		  
  1005 	  ret = gst_nuv_demux_frame_header_load (nuv, &h);
  1006 
  1007   if (ret != GST_FLOW_OK)
  1008     return ret;
  1009 
  1010   if (h.i_type != 'D') {
  1011     GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
  1012     return GST_FLOW_NOT_SUPPORTED;
  1013   }
  1014 
  1015   if (h.i_length > 0) {
  1016     if (h.i_compression == 'F') {
  1017       nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
  1018     } else {
  1019       GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
  1020       return GST_FLOW_NOT_SUPPORTED;
  1021     }
  1022   } else {
  1023     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
  1024   }
  1025 
  1026   return ret;
  1027 }
  1028 
  1029 static GstFlowReturn
  1030 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
  1031 {
  1032   GstFlowReturn ret = GST_FLOW_OK;
  1033 
  1034   if (nuv->priv->new_file)
  1035 	  ret = gst_nuv_demux_extended_header_load (nuv, NULL);
  1036   else { 
  1037 	  ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
  1038 	  if (ret != GST_FLOW_OK)
  1039     	return ret;
  1040 	  gst_nuv_demux_create_pads (nuv);
  1041   }
  1042 
  1043   nuv->priv->state = GST_NUV_DEMUX_INDEX_CREATE;
  1044   return ret;
  1045 }
  1046 
  1047 static GstFlowReturn
  1048 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
  1049 {
  1050   GstBuffer *buf = NULL;
  1051   GstFlowReturn res = GST_FLOW_OK;
  1052 
  1053   res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
  1054   if ((res != GST_FLOW_OK) || (buf == NULL)) {
  1055     if (buf != NULL) {
  1056       gst_buffer_unref (buf);
  1057     }
  1058     return res;
  1059   }
  1060 
  1061   if (buf->data[0] == 'X') {
  1062     gst_buffer_unref (buf);
  1063     buf = NULL;
  1064     nuv_frame_header h;
  1065 
  1066     res = gst_nuv_demux_frame_header_load (nuv, &h);
  1067     if (res != GST_FLOW_OK)
  1068       return res;
  1069 
  1070     if (h.i_length != 512) {
  1071       return GST_FLOW_ERROR;
  1072     }
  1073     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
  1074   } else {
  1075     nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
  1076     g_object_unref (buf);
  1077     GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
  1078         (_("incomplete NUV support")), ("incomplete NUV support"));
  1079     return GST_FLOW_ERROR;
  1080   }
  1081   return res;
  1082 }
  1083 
  1084 static void
  1085 gst_nuv_demux_create_seek_index (GstNuvDemux * nuv)
  1086 {
  1087   GstMessage *msg;
  1088   nuv_frame_header h;
  1089 
  1090   while (gst_nuv_demux_frame_header_load (nuv, &h) == GST_FLOW_OK) {
  1091     if ((h.i_type == 'V') && (h.i_keyframe == 0)) {
  1092       frame_index_data *f = g_new0 (frame_index_data, 1);
  1093 
  1094       f->offset = nuv->priv->offset - 12;
  1095       f->timecode = h.i_timecode * GST_MSECOND;
  1096 
  1097       nuv->priv->index = g_slist_append (nuv->priv->index, f);
  1098     } 
  1099     if (h.i_type != 'R') {
  1100       nuv->priv->offset += h.i_length;
  1101 		  if (h.i_type == 'A' || h.i_type == 'V')
  1102   		  nuv->priv->duration_time = h.i_timecode * GST_MSECOND;
  1103     }
  1104   }
  1105   GST_DEBUG_OBJECT  (nuv, "CREATING INDEX: DONE : DURATION Bytes/Sec: %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, 
  1106     nuv->priv->offset, nuv->priv->duration_time);
  1107   
  1108   nuv->priv->duration_bytes = nuv->priv->offset;
  1109   nuv->priv->offset = nuv->priv->header_lengh;
  1110 
  1111   msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_time);
  1112   gst_element_post_message (GST_ELEMENT (nuv), msg);
  1113 }
  1114 
  1115 static GstFlowReturn
  1116 gst_nuv_demux_play (GstPad * pad)
  1117 {
  1118   GstFlowReturn res = GST_FLOW_OK;
  1119   GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
  1120 
  1121   switch (nuv->priv->state) {
  1122     case GST_NUV_DEMUX_START:
  1123       res = gst_nuv_demux_stream_file_header (nuv);
  1124       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1125         goto pause;
  1126       }
  1127       break;
  1128 
  1129     case GST_NUV_DEMUX_HEADER_DATA:
  1130       res = gst_nuv_demux_stream_header_data (nuv);
  1131       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1132         goto pause;
  1133       }
  1134       break;
  1135 
  1136     case GST_NUV_DEMUX_EXTRA_DATA:
  1137       res = gst_nuv_demux_stream_extra_data (nuv);
  1138       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1139         goto pause;
  1140       }
  1141       break;
  1142 
  1143     case GST_NUV_DEMUX_MPEG_DATA:
  1144       res = gst_nuv_demux_stream_mpeg_data (nuv);
  1145       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1146         goto pause;
  1147       }
  1148       break;
  1149 
  1150     case GST_NUV_DEMUX_EXTEND_HEADER:
  1151       res = gst_nuv_demux_stream_extend_header (nuv);
  1152       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1153         goto pause;
  1154       }
  1155       break;
  1156 
  1157     case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
  1158       res = gst_nuv_demux_stream_extend_header_data (nuv);
  1159       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1160         goto pause;
  1161       }
  1162       //store file header size
  1163       nuv->priv->header_lengh = nuv->priv->offset;
  1164       break;
  1165 
  1166     case GST_NUV_DEMUX_INDEX_CREATE:
  1167       if ((nuv->priv->mode == NUV_PULL_MODE) && (!nuv->priv->new_file)) {
  1168         gst_nuv_demux_create_seek_index (nuv);
  1169       }
  1170 
  1171     case GST_NUV_DEMUX_FRAME_HEADER:
  1172       res = gst_nuv_demux_read_head_frame (nuv);
  1173       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1174         goto pause;
  1175       }
  1176       break;
  1177 
  1178     case GST_NUV_DEMUX_MOVI: 
  1179       res = gst_nuv_demux_stream_data (nuv);
  1180       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1181         goto pause;
  1182       }
  1183       break;
  1184 
  1185     case GST_NUV_DEMUX_INVALID_DATA:
  1186       goto pause;
  1187       break;
  1188     default:
  1189       g_assert_not_reached ();
  1190   }
  1191 
  1192   return GST_FLOW_OK;
  1193 
  1194 pause:
  1195   GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
  1196   gst_pad_pause_task (nuv->priv->sinkpad);
  1197 
  1198   if (res == GST_FLOW_ERROR_EOS) {
  1199     gst_nuv_demux_send_eos (nuv);
  1200     nuv->priv->eos = TRUE;
  1201     res = GST_FLOW_OK;
  1202   }
  1203 
  1204   if (GST_FLOW_IS_FATAL (res)) {
  1205     GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
  1206         (_("Internal data stream error.")),
  1207         ("streaming stopped, reason %s", gst_flow_get_name (res)));
  1208 
  1209     gst_nuv_demux_send_eos (nuv);
  1210   }
  1211   return res;
  1212 }
  1213 
  1214 static void
  1215 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
  1216 {
  1217   gst_element_post_message (GST_ELEMENT (nuv),
  1218       gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
  1219 
  1220   if (nuv->priv->src_video_pad)
  1221     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
  1222   if (nuv->priv->src_audio_pad)
  1223     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
  1224 }
  1225 
  1226 static GstFlowReturn
  1227 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
  1228     GstBuffer ** buffer)
  1229 {
  1230   GstFlowReturn ret = GST_FLOW_OK;
  1231 
  1232   if (size == 0) {
  1233     return ret;
  1234   }
  1235 
  1236   if (nuv->priv->mode == NUV_PULL_MODE) {
  1237     ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
  1238     if (ret == GST_FLOW_OK) {
  1239       if (move) {
  1240         nuv->priv->offset += size;
  1241       }
  1242       /* got eos */
  1243     } else if (ret == GST_FLOW_UNEXPECTED) {
  1244       return GST_FLOW_ERROR_EOS;
  1245     }
  1246   } else {
  1247     if (gst_adapter_available (nuv->priv->adapter) < size) {
  1248       nuv->priv->more_data = TRUE;
  1249       return GST_FLOW_ERROR_NO_DATA;
  1250     }
  1251     if (move) {
  1252       *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
  1253     } else {
  1254       guint8 *data = NULL;
  1255       data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
  1256       *buffer = gst_buffer_new ();
  1257       gst_buffer_set_data (*buffer, data, size);
  1258     }
  1259   }
  1260   return ret;
  1261 }
  1262 
  1263 static GstFlowReturn
  1264 gst_nuv_demux_move_bytes (GstNuvDemux * nuv, guint64 size)
  1265 {
  1266   GstFlowReturn ret = GST_FLOW_OK;
  1267 
  1268   if (size == 0) {
  1269     return ret;
  1270   }
  1271 
  1272   if (nuv->priv->mode == NUV_PULL_MODE) {
  1273       nuv->priv->offset += size;
  1274   } else {
  1275     if (gst_adapter_available (nuv->priv->adapter) < size) {
  1276       nuv->priv->more_data = TRUE;
  1277       return GST_FLOW_ERROR_NO_DATA;
  1278     }
  1279     gst_adapter_flush (nuv->priv->adapter, size);
  1280   }
  1281   return ret;
  1282 }
  1283 
  1284 static gboolean
  1285 gst_nuv_demux_sink_activate (GstPad * sinkpad)
  1286 {
  1287   gboolean res = TRUE;
  1288   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1289  
  1290   if (gst_pad_check_pull_range (sinkpad)) {
  1291     gst_adapter_clear (nuv->priv->adapter);
  1292     res = gst_pad_activate_pull (sinkpad, TRUE);
  1293   } else {
  1294     gst_adapter_clear (nuv->priv->adapter);
  1295     res = gst_pad_activate_push (sinkpad, TRUE);
  1296   }
  1297   
  1298   g_object_unref (nuv);
  1299   return res;
  1300 }
  1301 
  1302 static gboolean
  1303 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
  1304 {
  1305   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1306 
  1307   if (active) {
  1308     GST_DEBUG_OBJECT (nuv, "activating pull function");
  1309     nuv->priv->mode = NUV_PULL_MODE;
  1310     gst_adapter_clear (nuv->priv->adapter);
  1311 
  1312     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
  1313   } else {
  1314     GST_DEBUG_OBJECT (nuv, "deactivating pull function");
  1315     gst_pad_stop_task (sinkpad);
  1316   }
  1317   gst_object_unref (nuv);
  1318 
  1319   return TRUE;
  1320 }
  1321 
  1322 static gboolean
  1323 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
  1324 {
  1325   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1326 
  1327   if (active) {
  1328     nuv->priv->mode = NUV_PUSH_MODE;
  1329     gst_adapter_clear (nuv->priv->adapter);
  1330 
  1331     GST_DEBUG_OBJECT (nuv, "activating push/chain function");
  1332   } else {
  1333     GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
  1334   }
  1335 
  1336   gst_object_unref (nuv);
  1337 
  1338   return TRUE;
  1339 }
  1340 
  1341 static frame_index_data *
  1342 gst_nuv_demux_do_seek_index (GstNuvDemux *nuv, gint64 seek_pos, 
  1343   gint64 segment_stop, GstFormat format)
  1344 {
  1345   GSList *l;
  1346   frame_index_data *ret = NULL;
  1347 
  1348   if (nuv->priv->index == NULL) {
  1349     return NULL;
  1350   }
  1351 
  1352   /* find keyframe closest to the requested position  */
  1353   for (l = nuv->priv->index; l != NULL; l = l->next) {
  1354     frame_index_data *f = (frame_index_data *) l->data;  
  1355     gint64 pos = 0;
  1356    
  1357     if (format == GST_FORMAT_BYTES) {     
  1358       pos = f->offset;
  1359     } else if (format == GST_FORMAT_TIME) {
  1360       pos = f->timecode;
  1361     } else {
  1362       return NULL;
  1363     }
  1364 
  1365     if (pos >= seek_pos) {
  1366       ret = f;
  1367       break;
  1368     }
  1369 
  1370     if ((segment_stop != -1) &&  (segment_stop != GST_CLOCK_TIME_NONE) && (pos > segment_stop)) {
  1371       break;
  1372     }
  1373   } 
  1374 
  1375   return ret;
  1376 }
  1377 
  1378 static gboolean
  1379 gst_nuv_demux_do_seek (GstNuvDemux *nuv, GstEvent * event)
  1380 {
  1381   gdouble rate;
  1382   GstFormat format;
  1383   GstSeekFlags flags;
  1384   GstSeekType cur_type;
  1385   gint64 cur;
  1386   GstSeekType stop_type;
  1387   gint64 stop;
  1388   gboolean flush;
  1389   frame_index_data *entry;
  1390   gint64 segment_start;
  1391   gint64 segment_stop;
  1392   GstEvent *newsegment_event;
  1393 
  1394   if (nuv->priv->eos) {
  1395     return FALSE;
  1396   }
  1397 
  1398   if (nuv->priv->mode == NUV_PUSH_MODE) {
  1399     return FALSE;
  1400   }
  1401 
  1402 
  1403   gst_event_parse_seek (event, &rate, &format, &flags,
  1404       &cur_type, &cur, &stop_type, &stop);
  1405 
  1406 
  1407 
  1408 /*
  1409   if (format == GST_FORMAT_TIME) {
  1410     GST_DEBUG_OBJECT (nuv, "Can only seek on BYTES");
  1411     return FALSE;
  1412   }
  1413 */
  1414 
  1415   if (rate <= 0.0) {
  1416     GST_DEBUG_OBJECT (nuv, "Can only seek with positive rate");
  1417     return FALSE;
  1418   }
  1419   
  1420   if (cur_type == GST_SEEK_TYPE_SET) {
  1421     GST_OBJECT_LOCK (nuv);
  1422     if (gst_nuv_demux_do_seek_index (nuv, cur, -1, format) == NULL) {
  1423       GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
  1424       GST_OBJECT_UNLOCK (nuv);
  1425       return FALSE;
  1426     }
  1427     GST_OBJECT_UNLOCK (nuv);
  1428   }
  1429 
  1430   flush = !!(flags & GST_SEEK_FLAG_FLUSH);
  1431   
  1432   if (flush) {
  1433     gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_start ());
  1434     if (nuv->priv->src_video_pad != NULL) {
  1435       gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_start ());
  1436     }
  1437 
  1438     if (nuv->priv->src_audio_pad != NULL) {
  1439       gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_start ());
  1440     }
  1441   }
  1442   else {
  1443     gst_pad_pause_task (nuv->priv->sinkpad);
  1444   }
  1445 
  1446   GST_PAD_STREAM_LOCK (nuv->priv->sinkpad);
  1447   GST_OBJECT_LOCK (nuv);
  1448 
  1449 
  1450   if (cur == GST_CLOCK_TIME_NONE)
  1451     cur = 0;
  1452   if (stop == GST_CLOCK_TIME_NONE)
  1453     stop = nuv->priv->duration_time;
  1454 
  1455   if (cur_type == GST_SEEK_TYPE_SET)
  1456     segment_start = cur;
  1457   else if (cur_type == GST_SEEK_TYPE_CUR)
  1458     segment_start = nuv->priv->segment_start + cur;
  1459   else
  1460     segment_start = nuv->priv->segment_start;
  1461 
  1462   if (stop_type == GST_SEEK_TYPE_SET)
  1463     segment_stop = stop;
  1464   else if (stop_type == GST_SEEK_TYPE_CUR)
  1465     segment_stop = nuv->priv->segment_stop + stop;
  1466   else
  1467     segment_stop = nuv->priv->segment_stop;
  1468 
  1469   segment_start = CLAMP (segment_start, 0, nuv->priv->duration_time);
  1470   segment_stop = CLAMP (segment_stop, 0, nuv->priv->duration_time);
  1471 
  1472   entry = gst_nuv_demux_do_seek_index (nuv, segment_start,
  1473       segment_stop, format);
  1474 
  1475   if (entry == NULL) {
  1476     GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
  1477     goto seek_error;
  1478   }
  1479 
  1480   segment_start = entry->timecode;
  1481 
  1482   nuv->priv->segment_start = segment_start;
  1483   nuv->priv->segment_stop = segment_stop;
  1484 
  1485   GST_OBJECT_UNLOCK (nuv);
  1486 
  1487   if (!nuv->priv->eos) {
  1488     GstMessage *msg;
  1489     msg = gst_message_new_segment_start (GST_OBJECT (nuv), GST_FORMAT_TIME,
  1490       nuv->priv->segment_start);
  1491   
  1492     gst_element_post_message (GST_ELEMENT (nuv), msg);
  1493   }
  1494 
  1495   GST_DEBUG_OBJECT (nuv, "NEW SEGMENT START %" G_GUINT64_FORMAT ", STOP %" G_GUINT64_FORMAT, 
  1496     segment_start, segment_stop);
  1497   newsegment_event = gst_event_new_new_segment (FALSE, rate,
  1498     GST_FORMAT_TIME, segment_start, segment_stop, segment_start);
  1499 
  1500 
  1501   if (flush) {
  1502     if (nuv->priv->src_video_pad != NULL) {
  1503       gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_stop ());
  1504     }
  1505 
  1506     if (nuv->priv->src_audio_pad != NULL) {
  1507       gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_stop ());
  1508     }
  1509 
  1510     gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_stop ());
  1511   }
  1512 
  1513   
  1514   if (nuv->priv->src_video_pad != NULL) {
  1515     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
  1516   }
  1517   if (nuv->priv->src_audio_pad != NULL) {
  1518     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
  1519   } 
  1520 
  1521   gst_event_unref (newsegment_event);
  1522 
  1523   nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
  1524   nuv->priv->offset = entry->offset;
  1525 
  1526   gst_pad_start_task (nuv->priv->sinkpad, (GstTaskFunction) gst_nuv_demux_loop,
  1527       nuv->priv->sinkpad);
  1528 
  1529   GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
  1530   return TRUE;
  1531 
  1532 seek_error:
  1533   GST_DEBUG_OBJECT (nuv, "Got a seek error");
  1534   GST_OBJECT_UNLOCK (nuv);
  1535   GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
  1536   return FALSE;
  1537 
  1538 }
  1539 
  1540 static gboolean
  1541 gst_nuv_demux_srcpad_event (GstPad * pad, GstEvent * event)
  1542 {
  1543   gboolean res = FALSE;
  1544   GstNuvDemux *nuv;
  1545 
  1546   nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1547   
  1548   switch (GST_EVENT_TYPE (event)) {
  1549     case GST_EVENT_SEEK:
  1550       res = gst_nuv_demux_do_seek (nuv, event);
  1551       break;
  1552     default:
  1553       res = FALSE;
  1554       break;
  1555   }
  1556   
  1557   gst_object_unref (nuv);
  1558   return res;
  1559 }
  1560 
  1561 static gboolean
  1562 gst_nuv_demux_sink_event (GstPad * pad, GstEvent * event)
  1563 {
  1564 	gboolean res = FALSE;
  1565 	GstNuvDemux *nuv;
  1566 
  1567 	nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1568 	
  1569 	switch  (GST_EVENT_TYPE (event)) {
  1570 		case GST_EVENT_NEWSEGMENT:
  1571 		{
  1572 			gboolean update;
  1573 			gdouble rate;
  1574 			GstFormat format;
  1575 			gint64 start;
  1576 			gint64 stop;
  1577 			gint64 position;
  1578 
  1579 			gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &position);				
  1580 			if ((format == GST_FORMAT_BYTES) && (start == 0)) {
  1581 				g_debug ("NEW SEGMENT 0");					
  1582 				if (nuv->priv->segment > 0) {
  1583 					nuv->priv->new_file = TRUE;					
  1584 					nuv->priv->state = GST_NUV_DEMUX_START;
  1585 					nuv->priv->offset = 0;
  1586 				}
  1587 				nuv->priv->segment++;					
  1588 
  1589 				/*
  1590 				newsegment_event = gst_event_new_new_segment (FALSE, rate,
  1591 				  GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
  1592 
  1593     			gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
  1594     			gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
  1595   				gst_event_unref (newsegment_event);
  1596 				*/
  1597 				res = gst_pad_event_default(pad, event);
  1598 			}
  1599 			break;
  1600 		}
  1601 		default:
  1602 			res = gst_pad_event_default (pad, event);
  1603 			break;
  1604 	}	
  1605 
  1606 	return res;
  1607 }
  1608 
  1609 static GstFlowReturn
  1610 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
  1611 {
  1612   GstFlowReturn ret = GST_FLOW_OK;
  1613   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1614   
  1615   if (nuv->priv->mode != NUV_PUSH_MODE)
  1616     return ret;
  1617 
  1618   gst_adapter_push (nuv->priv->adapter, buf);  
  1619   
  1620   while ((ret == GST_FLOW_OK) && (nuv->priv->more_data == FALSE)) {
  1621       ret = gst_nuv_demux_play (pad);
  1622   }
  1623 
  1624   nuv->priv->more_data = FALSE;
  1625   gst_object_unref (nuv);
  1626 
  1627   return ret;
  1628 }
  1629 
  1630 static void
  1631 gst_nuv_demux_loop (GstPad * pad)
  1632 {
  1633   gst_nuv_demux_play (pad);
  1634 }
  1635 
  1636 static void
  1637 gst_nuv_demux_index_free (gpointer data, gpointer user_data)
  1638 {
  1639   g_free (data);
  1640 }
  1641 
  1642 static void
  1643 gst_nuv_demux_reset (GstNuvDemux * nuv)
  1644 {
  1645   nuv->priv->eos = FALSE;
  1646   nuv->priv->more_data = FALSE;
  1647   nuv->priv->state = GST_NUV_DEMUX_START;
  1648   nuv->priv->mode = NUV_PUSH_MODE;
  1649   nuv->priv->offset = 0;
  1650   nuv->priv->time_start = 0;
  1651   nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
  1652   nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
  1653   nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
  1654   nuv->priv->last_video_return = GST_FLOW_OK;
  1655   nuv->priv->last_audio_return = GST_FLOW_OK;
  1656   nuv->priv->header_lengh = 0;
  1657   nuv->priv->segment_stop = GST_CLOCK_TIME_NONE;
  1658   nuv->priv->segment_start = GST_CLOCK_TIME_NONE;
  1659   nuv->priv->new_file = FALSE;
  1660 
  1661   //clear index list
  1662   g_slist_foreach (nuv->priv->index, gst_nuv_demux_index_free, NULL);
  1663   g_slist_free (nuv->priv->index);
  1664   nuv->priv->index = NULL;
  1665 
  1666   gst_adapter_clear (nuv->priv->adapter);
  1667 
  1668   if (nuv->priv->mpeg_buffer != NULL) {
  1669     gst_buffer_unref (nuv->priv->mpeg_buffer);
  1670     nuv->priv->mpeg_buffer = NULL;
  1671   }
  1672 }
  1673 
  1674 static void
  1675 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
  1676 {
  1677   if (nuv->priv->src_video_pad) {
  1678     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
  1679     nuv->priv->src_video_pad = NULL;
  1680   }
  1681 
  1682   if (nuv->priv->src_audio_pad) {
  1683     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
  1684     nuv->priv->src_audio_pad = NULL;
  1685   }
  1686 }
  1687 
  1688 static GstStateChangeReturn
  1689 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
  1690 {
  1691   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  1692 
  1693   g_debug ("Nuvdemux state_change");
  1694   switch (transition) {
  1695     case GST_STATE_CHANGE_NULL_TO_READY:
  1696       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1697       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1698       break;
  1699     default:
  1700       break;
  1701   }
  1702 
  1703   g_debug ("Nuvdemux state_change: 1");
  1704 
  1705   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1706   if (ret == GST_STATE_CHANGE_FAILURE) {
  1707     goto done;
  1708   }
  1709 
  1710 
  1711   g_debug ("Nuvdemux state_change: 2");
  1712 
  1713   switch (transition) {
  1714     case GST_STATE_CHANGE_READY_TO_NULL:
  1715       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1716       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1717       break;
  1718     default:
  1719       break;
  1720   }
  1721 
  1722 
  1723   g_debug ("Nuvdemux state_change: DONE");
  1724 
  1725 done:
  1726   return ret;
  1727 }
  1728 
  1729 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
  1730 GstBuffer *
  1731 gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
  1732 {
  1733   GstBuffer *buffer;
  1734   GstBuffer *cur;
  1735   guint8 *data;
  1736 
  1737   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
  1738   g_return_val_if_fail (nbytes > 0, NULL);
  1739 
  1740   GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);
  1741 
  1742   /* we don't have enough data, return NULL. This is unlikely
  1743    * as one usually does an _available() first instead of peeking a
  1744    * random size. */
  1745   if (G_UNLIKELY (nbytes > adapter->size))
  1746     return NULL;
  1747 
  1748   /* our head buffer has enough data left, return it */
  1749   cur = adapter->buflist->data;
  1750   if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) {
  1751     GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
  1752         nbytes);
  1753     buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes);
  1754 
  1755     gst_adapter_flush (adapter, nbytes);
  1756 
  1757     return buffer;
  1758   }
  1759 
  1760   data = gst_adapter_take (adapter, nbytes);
  1761   if (data == NULL)
  1762     return NULL;
  1763 
  1764   buffer = gst_buffer_new ();
  1765   GST_BUFFER_DATA (buffer) = data;
  1766   GST_BUFFER_MALLOCDATA (buffer) = data;
  1767   GST_BUFFER_SIZE (buffer) = nbytes;
  1768 
  1769   return buffer;
  1770 }
  1771 #endif
  1772 
  1773 static void
  1774 gst_nuv_typefind (GstTypeFind * tf, gpointer unused)
  1775 {
  1776   guint8 *data = gst_type_find_peek (tf, 0, 11);
  1777 
  1778   if (data) {
  1779     if (memcmp (data, "MythTVVideo", 11) == 0
  1780         || memcmp (data, "NuppelVideo", 11) == 0) {
  1781       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, 
  1782           gst_caps_new_simple ("video/x-nuv", NULL));
  1783     }
  1784   }
  1785 }
  1786 
  1787 static gboolean
  1788 plugin_init (GstPlugin * plugin)
  1789 {
  1790   static gchar *exts[] = { "nuv", NULL };    
  1791 #ifdef ENABLE_NLS
  1792   setlocale (LC_ALL, "");
  1793   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  1794 #endif /* ENABLE_NLS */
  1795 
  1796   if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
  1797           GST_TYPE_NUV_DEMUX)) {
  1798     return FALSE;
  1799   }
  1800 
  1801   if (!gst_type_find_register (plugin, "video/x-nuv", GST_RANK_SECONDARY,
  1802               gst_nuv_typefind, 
  1803               exts,
  1804               gst_caps_new_simple ("video/x-nuv", NULL), NULL, NULL)) {
  1805     GST_WARNING ("can't register typefind");
  1806     return FALSE;
  1807   }
  1808 
  1809   return TRUE;
  1810 }
  1811 
  1812 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1813     GST_VERSION_MINOR,
  1814     "nuvdemux",
  1815     			"Demuxes and muxes audio and video",
  1816      plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
  1817