gst-gmyth/nuvdemux/gstnuvdemux.c
author melunko
Wed May 30 19:52:46 2007 +0100 (2007-05-30)
branchtrunk
changeset 721 53ab6ef49c33
parent 716 1ee5df4c9ccd
child 725 a102805611e7
permissions -rw-r--r--
[svn r727] A very very small change in one debug message format
     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, TRUE, &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 
   531   h->i_type = GPOINTER_TO_INT (data[0]);
   532   h->i_compression = GPOINTER_TO_INT (data[1]);
   533   h->i_keyframe = GPOINTER_TO_INT (data[2]);
   534   h->i_filters = GPOINTER_TO_INT (data[3]);
   535   h->i_timecode = GST_READ_UINT32_LE (&data[4]);
   536   h->i_length = GST_READ_UINT32_LE (&data[8]);
   537 
   538   GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
   539       h->i_type,
   540       h->i_compression ? h->i_compression : ' ',
   541       h->i_keyframe ? h->i_keyframe : ' ',
   542       h->i_filters, h->i_timecode, h->i_length);
   543 
   544 done:  
   545   if (buf != NULL) {
   546       gst_buffer_unref (buf);
   547       buf = NULL;
   548   }
   549 
   550   return res;
   551 }
   552 
   553 static GstFlowReturn
   554 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
   555     nuv_extended_header * h)
   556 {
   557   unsigned char *data;
   558   GstBuffer *buff = NULL;
   559 
   560   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
   561 
   562   if ((res != GST_FLOW_OK) || (buff == NULL)) {
   563       goto done;
   564   }
   565 
   566   if (h == NULL)
   567     goto done;
   568 
   569   data = buff->data;
   570   h->i_version = GST_READ_UINT32_LE (&data[0]);
   571   h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
   572   h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
   573   h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
   574   h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
   575   h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
   576   h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
   577   h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
   578   h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
   579   h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
   580   h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
   581   h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
   582   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
   583   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
   584   h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
   585   h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
   586   h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
   587 
   588   GST_DEBUG_OBJECT (nuv,
   589       "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
   590       "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
   591       h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
   592       h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
   593       h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
   594       h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
   595       h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
   596       h->i_keyframe_adjust_offset);
   597 
   598 done:
   599   if (buff != NULL) {
   600     gst_buffer_unref (buff);
   601     buff = NULL;
   602   }
   603   return res;
   604 }
   605 
   606 
   607 /* Query Functions */
   608 static const GstQueryType *
   609 gst_nuv_demux_get_src_query_types (GstPad * pad)
   610 {
   611   static const GstQueryType src_types[] = {
   612     GST_QUERY_POSITION,
   613     GST_QUERY_DURATION,
   614     0
   615   };
   616 
   617   return src_types;
   618 }
   619 
   620 static gboolean
   621 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
   622 {
   623   gboolean res = FALSE;
   624   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   625 
   626 
   627   switch (GST_QUERY_TYPE (query)) 
   628   {
   629     case GST_QUERY_POSITION:
   630 	{
   631 		GstFormat format;
   632 		gst_query_parse_position (query, &format, NULL); 
   633 		switch (format) 
   634 		{
   635 			case GST_FORMAT_TIME:
   636 				if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
   637 			        gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
   638 			        res = TRUE;
   639 				}
   640 			  	break;
   641 			default:				
   642 				break;
   643 		}
   644 		break;
   645 	}		
   646     case GST_QUERY_DURATION:
   647 	{
   648 	  GstFormat format;		
   649 	  gst_query_parse_duration (query, &format, NULL); 
   650 	  switch (format)
   651 	  {
   652 			case GST_FORMAT_TIME:			 
   653 				if (nuv->priv->duration_time != GST_CLOCK_TIME_NONE) {
   654 			        gst_query_set_duration (query, GST_FORMAT_TIME, nuv->priv->duration_time);
   655 				    res = TRUE;
   656 			    }
   657 				break;
   658 			default:				
   659 			    break;
   660 	  }
   661 	  break;
   662   	}
   663 	default:
   664 	  break;
   665   }
   666 
   667   if (res==FALSE) {
   668 	res = gst_pad_query_default (pad, query);		  
   669   } 
   670 
   671   gst_object_unref (nuv);
   672   return res;
   673 }
   674 
   675 static GstPad*
   676 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
   677 {
   678     GstPad *pad = NULL;
   679     pad = gst_pad_new_from_static_template (template, name);
   680     gst_pad_set_caps (pad, caps);
   681     gst_pad_set_active (pad, TRUE);
   682     gst_pad_use_fixed_caps (pad);
   683     gst_element_add_pad (GST_ELEMENT (nuv), pad);
   684 
   685     gst_pad_set_event_function (pad,
   686       GST_DEBUG_FUNCPTR (gst_nuv_demux_srcpad_event));
   687 
   688     gst_pad_set_query_type_function (pad,
   689       GST_DEBUG_FUNCPTR (gst_nuv_demux_get_src_query_types));
   690 
   691     gst_pad_set_query_function (pad,
   692       GST_DEBUG_FUNCPTR (gst_nuv_demux_handle_src_query));
   693       
   694 
   695     return pad;
   696 }
   697 
   698 static void
   699 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
   700 {
   701   if (nuv->priv->h.i_video_blocks != 0) {
   702     GstCaps *video_caps = NULL;
   703 
   704     video_caps = gst_caps_new_simple ("video/x-divx",
   705         "divxversion", G_TYPE_INT, 4,
   706         "width", G_TYPE_INT, nuv->priv->h.i_width,
   707         "height", G_TYPE_INT, nuv->priv->h.i_height,
   708         "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
   709         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
   710         "pixel-aspect-ratio", GST_TYPE_FRACTION,
   711         (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
   712 
   713     nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
   714     gst_caps_unref (video_caps);
   715   }
   716 
   717   if (nuv->priv->h.i_audio_blocks != 0) {
   718     GstCaps *audio_caps = NULL;
   719 
   720     audio_caps = gst_caps_new_simple ("audio/mpeg",
   721         "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
   722         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
   723         "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
   724         "layer", G_TYPE_INT, 3, // fixme: magic number
   725         "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
   726     
   727     nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
   728     gst_caps_unref (audio_caps);
   729   }
   730 
   731   gst_element_no_more_pads (GST_ELEMENT (nuv));
   732 }
   733 
   734 static gboolean
   735 gst_nuv_demux_validate_header (nuv_frame_header *h)
   736 {
   737   gboolean valid = FALSE;
   738   //g_usleep (1 * G_USEC_PER_SEC );
   739   switch  (h->i_type) {
   740 /*
   741     case 'V':
   742       if (h->i_compression == 0 ||
   743           h->i_compression == 1 ||
   744           h->i_compression == 2 ||
   745           h->i_compression == 'N' ||
   746           h->i_compression == 'L') {
   747          valid = TRUE;
   748       }
   749       break;
   750     case 'A':
   751       if (h->i_compression == 0 ||
   752           h->i_compression == 1 ||
   753           h->i_compression == 2 ||
   754           h->i_compression == 3 ||
   755           h->i_compression == 'F' ||
   756           h->i_compression == 'S' ||
   757           h->i_compression == 'N' ||
   758           h->i_compression == 'L') {
   759          valid = TRUE;
   760       }
   761       break;
   762     case 'S':
   763       if (h->i_compression == 'B' ||
   764           h->i_compression == 'A' ||
   765           h->i_compression == 'V' ||
   766           h->i_compression == 'S') {
   767          valid = TRUE;
   768       }
   769       break;
   770 */
   771     case 'A':
   772     case 'V':
   773     case 'S':
   774     case 'R':
   775     case 'D':
   776     case 'Q':
   777       valid  = TRUE;
   778       break;
   779     default:
   780       valid = FALSE;
   781   }
   782 
   783   return valid;
   784 }
   785 
   786 static GstFlowReturn
   787 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
   788 {
   789   GstFlowReturn ret = GST_FLOW_OK;
   790   gboolean valid = FALSE;
   791 
   792   do {
   793     ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
   794     if (ret != GST_FLOW_OK) {
   795       return ret;
   796     }
   797 
   798     if (gst_nuv_demux_validate_header (&nuv->priv->fh) == TRUE)
   799       valid = TRUE;
   800 
   801   } while (valid == FALSE);
   802 
   803   nuv->priv->state = GST_NUV_DEMUX_MOVI;
   804   return ret;
   805 }
   806 
   807 static gboolean
   808 gst_nuv_combine_flow (GstNuvDemux *nuv)
   809 {
   810     GstFlowReturn ret_video = nuv->priv->last_video_return;
   811     GstFlowReturn ret_audio = nuv->priv->last_audio_return;
   812 
   813     if ((ret_video != GST_FLOW_OK) &&
   814         (ret_audio != GST_FLOW_OK))
   815         return FALSE;
   816 
   817     if (GST_FLOW_IS_FATAL (ret_video))
   818         return FALSE;
   819 
   820     if (GST_FLOW_IS_FATAL (ret_audio))
   821         return FALSE;
   822 
   823     return TRUE;
   824 }
   825 
   826 static GstFlowReturn
   827 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
   828 {
   829   GstFlowReturn ret = GST_FLOW_OK;
   830   GstPad *pad = NULL;
   831   guint64 timestamp;
   832   GstBuffer *buf = NULL;
   833   nuv_frame_header h;
   834 
   835   h = nuv->priv->fh;
   836 
   837   if (h.i_type == 'R') {
   838     goto done;
   839   }
   840 
   841   if (h.i_length > 0) {
   842     ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
   843     if ((ret != GST_FLOW_OK) || (buf == NULL)) {
   844       goto done;
   845     }
   846 
   847     if ((h.i_timecode < 0)) {
   848       h.i_timecode = 0;
   849       //goto done;
   850     }
   851 
   852     timestamp = h.i_timecode * GST_MSECOND;
   853     GST_BUFFER_TIMESTAMP (buf) = timestamp;
   854   }
   855   else {
   856     goto done;
   857   }
   858 
   859 
   860   switch (h.i_type) {
   861     case 'V':
   862     {
   863       pad = nuv->priv->src_video_pad;
   864 
   865       if (nuv->priv->new_video_segment) {
   866 
   867         /* send new segment event*/
   868         gst_pad_push_event (nuv->priv->src_video_pad,
   869           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   870                                      GST_CLOCK_TIME_NONE, 0));
   871 
   872         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   873             nuv->priv->time_start = timestamp;
   874         }
   875         nuv->priv->new_video_segment = FALSE;
   876       }
   877 
   878       break;
   879     }
   880     case 'A':
   881     {
   882       pad = nuv->priv->src_audio_pad;
   883 
   884       if (nuv->priv->new_audio_segment) {
   885         /* send new segment event*/
   886         gst_pad_push_event (nuv->priv->src_audio_pad,
   887           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   888                                      GST_CLOCK_TIME_NONE, 0));
   889 
   890         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   891             nuv->priv->time_start = timestamp;
   892         }
   893         nuv->priv->new_audio_segment = FALSE;
   894       }
   895 
   896       break;
   897     }
   898     case 'S':
   899    {
   900       switch (h.i_compression) {
   901         case 'V':
   902           GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
   903           gst_pad_push_event (nuv->priv->src_video_pad,
   904               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
   905               		GST_CLOCK_TIME_NONE, 0));
   906           break;
   907         case 'A':
   908           GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
   909           gst_pad_push_event (nuv->priv->src_audio_pad,
   910               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   911               		GST_CLOCK_TIME_NONE, 0));
   912           break;
   913         default:
   914           break;
   915       }
   916       goto done;
   917     }
   918     default:
   919       break;
   920   }
   921 
   922   if ((buf != NULL) && (pad != NULL)) {
   923       /* pushing the buffer */
   924       gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
   925       ret = gst_pad_push (pad, buf);
   926       buf = NULL;
   927 
   928       if (ret != GST_FLOW_OK) {
   929         GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
   930 
   931         if (pad == nuv->priv->src_video_pad) {
   932             nuv->priv->last_video_return = ret;
   933         }
   934         else if (pad == nuv->priv->src_audio_pad) {
   935             nuv->priv->last_audio_return = ret;
   936         }
   937 
   938         /* verify anothers flow if is necessary stop task */
   939         if (gst_nuv_combine_flow (nuv) != FALSE) {
   940           ret = GST_FLOW_OK;
   941         } else {
   942 	  GST_WARNING_OBJECT (nuv, "error: on push");
   943 	}
   944 
   945       }
   946   } 
   947 
   948 done:
   949   if (buf != NULL) {
   950       gst_buffer_unref (buf);
   951       buf = NULL;
   952   }
   953   if (ret == GST_FLOW_OK) {
   954      nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   955      memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
   956   }
   957   return ret;
   958 }
   959 
   960 static GstFlowReturn
   961 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
   962 {
   963   GstFlowReturn ret = GST_FLOW_OK;
   964 
   965   /* ffmpeg extra data */
   966   if (nuv->priv->new_file) {
   967 	GstBuffer *buf;
   968 	ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE, &buf);
   969 	gst_buffer_unref (buf);
   970   } else {
   971     ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
   972       &nuv->priv->mpeg_buffer);
   973   }
   974 
   975   if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
   976     return ret;
   977   }
   978 
   979   GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
   980   nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   981   return ret;
   982 }
   983 
   984 static GstFlowReturn
   985 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
   986 {
   987   GstFlowReturn ret = GST_FLOW_OK;
   988 
   989   /* Load 'D' */
   990   nuv_frame_header h;
   991 
   992   if (nuv->priv->new_file) 
   993 	  ret = gst_nuv_demux_frame_header_load (nuv, NULL);
   994   else		  
   995 	  ret = gst_nuv_demux_frame_header_load (nuv, &h);
   996 
   997   if (ret != GST_FLOW_OK)
   998     return ret;
   999 
  1000   if (h.i_type != 'D') {
  1001     GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
  1002     return GST_FLOW_NOT_SUPPORTED;
  1003   }
  1004 
  1005   if (h.i_length > 0) {
  1006     if (h.i_compression == 'F') {
  1007       nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
  1008     } else {
  1009       GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
  1010       return GST_FLOW_NOT_SUPPORTED;
  1011     }
  1012   } else {
  1013     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
  1014   }
  1015 
  1016   return ret;
  1017 }
  1018 
  1019 static GstFlowReturn
  1020 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
  1021 {
  1022   GstFlowReturn ret = GST_FLOW_OK;
  1023 
  1024   if (nuv->priv->new_file)
  1025 	  ret = gst_nuv_demux_extended_header_load (nuv, NULL);
  1026   else { 
  1027 	  ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
  1028 	  if (ret != GST_FLOW_OK)
  1029     	return ret;
  1030 	  gst_nuv_demux_create_pads (nuv);
  1031   }
  1032 
  1033   nuv->priv->state = GST_NUV_DEMUX_INDEX_CREATE;
  1034   return ret;
  1035 }
  1036 
  1037 static GstFlowReturn
  1038 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
  1039 {
  1040   GstBuffer *buf = NULL;
  1041   GstFlowReturn res = GST_FLOW_OK;
  1042 
  1043   res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
  1044   if ((res != GST_FLOW_OK) || (buf == NULL)) {
  1045     if (buf != NULL) {
  1046       gst_buffer_unref (buf);
  1047     }
  1048     return res;
  1049   }
  1050 
  1051   if (buf->data[0] == 'X') {
  1052     gst_buffer_unref (buf);
  1053     buf = NULL;
  1054     nuv_frame_header h;
  1055 
  1056     res = gst_nuv_demux_frame_header_load (nuv, &h);
  1057     if (res != GST_FLOW_OK)
  1058       return res;
  1059 
  1060     if (h.i_length != 512) {
  1061       return GST_FLOW_ERROR;
  1062     }
  1063     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
  1064   } else {
  1065     nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
  1066     g_object_unref (buf);
  1067     GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
  1068         (_("incomplete NUV support")), ("incomplete NUV support"));
  1069     return GST_FLOW_ERROR;
  1070   }
  1071   return res;
  1072 }
  1073 
  1074 static void
  1075 gst_nuv_demux_create_seek_index (GstNuvDemux * nuv)
  1076 {
  1077   GstMessage *msg;
  1078   nuv_frame_header h;
  1079 
  1080   while (gst_nuv_demux_frame_header_load (nuv, &h) == GST_FLOW_OK) {
  1081     if ((h.i_type == 'V') && (h.i_keyframe == 0)) {
  1082       frame_index_data *f = g_new0 (frame_index_data, 1);
  1083 
  1084       f->offset = nuv->priv->offset - 12;
  1085       f->timecode = h.i_timecode * GST_MSECOND;
  1086 
  1087       nuv->priv->index = g_slist_append (nuv->priv->index, f);
  1088     } 
  1089     if (h.i_type != 'R') {
  1090       nuv->priv->offset += h.i_length;
  1091 		  if (h.i_type == 'A' || h.i_type == 'V')
  1092   		  nuv->priv->duration_time = h.i_timecode * GST_MSECOND;
  1093     }
  1094   }
  1095   GST_DEBUG_OBJECT  (nuv, "CREATING INDEX: DONE : DURATION Bytes/Sec: %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, 
  1096     nuv->priv->offset, nuv->priv->duration_time);
  1097   
  1098   nuv->priv->duration_bytes = nuv->priv->offset;
  1099   nuv->priv->offset = nuv->priv->header_lengh;
  1100 
  1101   msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_time);
  1102   gst_element_post_message (GST_ELEMENT (nuv), msg);
  1103 }
  1104 
  1105 static GstFlowReturn
  1106 gst_nuv_demux_play (GstPad * pad)
  1107 {
  1108   GstFlowReturn res = GST_FLOW_OK;
  1109   GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
  1110 
  1111   switch (nuv->priv->state) {
  1112     case GST_NUV_DEMUX_START:
  1113       res = gst_nuv_demux_stream_file_header (nuv);
  1114       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1115         goto pause;
  1116       }
  1117       break;
  1118 
  1119     case GST_NUV_DEMUX_HEADER_DATA:
  1120       res = gst_nuv_demux_stream_header_data (nuv);
  1121       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1122         goto pause;
  1123       }
  1124       break;
  1125 
  1126     case GST_NUV_DEMUX_EXTRA_DATA:
  1127       res = gst_nuv_demux_stream_extra_data (nuv);
  1128       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1129         goto pause;
  1130       }
  1131       break;
  1132 
  1133     case GST_NUV_DEMUX_MPEG_DATA:
  1134       res = gst_nuv_demux_stream_mpeg_data (nuv);
  1135       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1136         goto pause;
  1137       }
  1138       break;
  1139 
  1140     case GST_NUV_DEMUX_EXTEND_HEADER:
  1141       res = gst_nuv_demux_stream_extend_header (nuv);
  1142       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1143         goto pause;
  1144       }
  1145       break;
  1146 
  1147     case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
  1148       res = gst_nuv_demux_stream_extend_header_data (nuv);
  1149       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1150         goto pause;
  1151       }
  1152       //store file header size
  1153       nuv->priv->header_lengh = nuv->priv->offset;
  1154       break;
  1155 
  1156     case GST_NUV_DEMUX_INDEX_CREATE:
  1157       if ((nuv->priv->mode == NUV_PULL_MODE) && (!nuv->priv->new_file)) {
  1158         gst_nuv_demux_create_seek_index (nuv);
  1159       }
  1160 
  1161     case GST_NUV_DEMUX_FRAME_HEADER:
  1162       res = gst_nuv_demux_read_head_frame (nuv);
  1163       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1164         goto pause;
  1165       }
  1166       break;
  1167 
  1168     case GST_NUV_DEMUX_MOVI: 
  1169       res = gst_nuv_demux_stream_data (nuv);
  1170       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1171         goto pause;
  1172       }
  1173       break;
  1174 
  1175     case GST_NUV_DEMUX_INVALID_DATA:
  1176       goto pause;
  1177       break;
  1178     default:
  1179       g_assert_not_reached ();
  1180   }
  1181 
  1182   return GST_FLOW_OK;
  1183 
  1184 pause:
  1185   GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
  1186   gst_pad_pause_task (nuv->priv->sinkpad);
  1187 
  1188   if (res == GST_FLOW_ERROR_EOS) {
  1189     gst_nuv_demux_send_eos (nuv);
  1190     nuv->priv->eos = TRUE;
  1191     res = GST_FLOW_OK;
  1192   }
  1193 
  1194   if (GST_FLOW_IS_FATAL (res)) {
  1195     GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
  1196         (_("Internal data stream error.")),
  1197         ("streaming stopped, reason %s", gst_flow_get_name (res)));
  1198 
  1199     gst_nuv_demux_send_eos (nuv);
  1200   }
  1201   return res;
  1202 }
  1203 
  1204 static void
  1205 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
  1206 {
  1207   gst_element_post_message (GST_ELEMENT (nuv),
  1208       gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
  1209 
  1210   if (nuv->priv->src_video_pad)
  1211     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
  1212   if (nuv->priv->src_audio_pad)
  1213     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
  1214 }
  1215 
  1216 static GstFlowReturn
  1217 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
  1218     GstBuffer ** buffer)
  1219 {
  1220   GstFlowReturn ret = GST_FLOW_OK;
  1221 
  1222   if (size == 0) {
  1223     return ret;
  1224   }
  1225 
  1226   if (nuv->priv->mode == NUV_PULL_MODE) {
  1227     ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
  1228     if (ret == GST_FLOW_OK) {
  1229       if (move) {
  1230         nuv->priv->offset += size;
  1231       }
  1232       /* got eos */
  1233     } else if (ret == GST_FLOW_UNEXPECTED) {
  1234       return GST_FLOW_ERROR_EOS;
  1235     }
  1236   } else {
  1237     if (gst_adapter_available (nuv->priv->adapter) < size) {
  1238       nuv->priv->more_data = TRUE;
  1239       return GST_FLOW_ERROR_NO_DATA;
  1240     }
  1241     if (move) {
  1242       *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
  1243     } else {
  1244       guint8 *data = NULL;
  1245       data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
  1246       *buffer = gst_buffer_new ();
  1247       gst_buffer_set_data (*buffer, data, size);
  1248     }
  1249   }
  1250   return ret;
  1251 }
  1252 
  1253 static GstFlowReturn
  1254 gst_nuv_demux_move_bytes (GstNuvDemux * nuv, guint64 size)
  1255 {
  1256   GstFlowReturn ret = GST_FLOW_OK;
  1257 
  1258   if (size == 0) {
  1259     return ret;
  1260   }
  1261 
  1262   if (nuv->priv->mode == NUV_PULL_MODE) {
  1263       nuv->priv->offset += size;
  1264   } else {
  1265     if (gst_adapter_available (nuv->priv->adapter) < size) {
  1266       nuv->priv->more_data = TRUE;
  1267       return GST_FLOW_ERROR_NO_DATA;
  1268     }
  1269     gst_adapter_flush (nuv->priv->adapter, size);
  1270   }
  1271   return ret;
  1272 }
  1273 
  1274 static gboolean
  1275 gst_nuv_demux_sink_activate (GstPad * sinkpad)
  1276 {
  1277   gboolean res = TRUE;
  1278   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1279  
  1280   if (gst_pad_check_pull_range (sinkpad)) {
  1281     gst_adapter_clear (nuv->priv->adapter);
  1282     res = gst_pad_activate_pull (sinkpad, TRUE);
  1283   } else {
  1284     gst_adapter_clear (nuv->priv->adapter);
  1285     res = gst_pad_activate_push (sinkpad, TRUE);
  1286   }
  1287   
  1288   g_object_unref (nuv);
  1289   return res;
  1290 }
  1291 
  1292 static gboolean
  1293 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
  1294 {
  1295   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1296 
  1297   if (active) {
  1298     GST_DEBUG_OBJECT (nuv, "activating pull function");
  1299     nuv->priv->mode = NUV_PULL_MODE;
  1300     gst_adapter_clear (nuv->priv->adapter);
  1301 
  1302     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
  1303   } else {
  1304     GST_DEBUG_OBJECT (nuv, "deactivating pull function");
  1305     gst_pad_stop_task (sinkpad);
  1306   }
  1307   gst_object_unref (nuv);
  1308 
  1309   return TRUE;
  1310 }
  1311 
  1312 static gboolean
  1313 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
  1314 {
  1315   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1316 
  1317   if (active) {
  1318     nuv->priv->mode = NUV_PUSH_MODE;
  1319     gst_adapter_clear (nuv->priv->adapter);
  1320 
  1321     GST_DEBUG_OBJECT (nuv, "activating push/chain function");
  1322   } else {
  1323     GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
  1324   }
  1325 
  1326   gst_object_unref (nuv);
  1327 
  1328   return TRUE;
  1329 }
  1330 
  1331 static frame_index_data *
  1332 gst_nuv_demux_do_seek_index (GstNuvDemux *nuv, gint64 seek_pos, 
  1333   gint64 segment_stop, GstFormat format)
  1334 {
  1335   GSList *l;
  1336   frame_index_data *ret = NULL;
  1337 
  1338   if (nuv->priv->index == NULL) {
  1339     return NULL;
  1340   }
  1341 
  1342   /* find keyframe closest to the requested position  */
  1343   for (l = nuv->priv->index; l != NULL; l = l->next) {
  1344     frame_index_data *f = (frame_index_data *) l->data;  
  1345     gint64 pos = 0;
  1346    
  1347     if (format == GST_FORMAT_BYTES) {     
  1348       pos = f->offset;
  1349     } else if (format == GST_FORMAT_TIME) {
  1350       pos = f->timecode;
  1351     } else {
  1352       return NULL;
  1353     }
  1354 
  1355     if (pos >= seek_pos) {
  1356       ret = f;
  1357       break;
  1358     }
  1359 
  1360     if ((segment_stop != -1) &&  (segment_stop != GST_CLOCK_TIME_NONE) && (pos > segment_stop)) {
  1361       break;
  1362     }
  1363   } 
  1364 
  1365   return ret;
  1366 }
  1367 
  1368 static gboolean
  1369 gst_nuv_demux_do_seek (GstNuvDemux *nuv, GstEvent * event)
  1370 {
  1371   gdouble rate;
  1372   GstFormat format;
  1373   GstSeekFlags flags;
  1374   GstSeekType cur_type;
  1375   gint64 cur;
  1376   GstSeekType stop_type;
  1377   gint64 stop;
  1378   gboolean flush;
  1379   frame_index_data *entry;
  1380   gint64 segment_start;
  1381   gint64 segment_stop;
  1382   GstEvent *newsegment_event;
  1383 
  1384   if (nuv->priv->eos) {
  1385     return FALSE;
  1386   }
  1387 
  1388   if (nuv->priv->mode == NUV_PUSH_MODE) {
  1389     return FALSE;
  1390   }
  1391 
  1392 
  1393   gst_event_parse_seek (event, &rate, &format, &flags,
  1394       &cur_type, &cur, &stop_type, &stop);
  1395 
  1396 
  1397 
  1398 /*
  1399   if (format == GST_FORMAT_TIME) {
  1400     GST_DEBUG_OBJECT (nuv, "Can only seek on BYTES");
  1401     return FALSE;
  1402   }
  1403 */
  1404 
  1405   if (rate <= 0.0) {
  1406     GST_DEBUG_OBJECT (nuv, "Can only seek with positive rate");
  1407     return FALSE;
  1408   }
  1409   
  1410   if (cur_type == GST_SEEK_TYPE_SET) {
  1411     GST_OBJECT_LOCK (nuv);
  1412     if (gst_nuv_demux_do_seek_index (nuv, cur, -1, format) == NULL) {
  1413       GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
  1414       GST_OBJECT_UNLOCK (nuv);
  1415       return FALSE;
  1416     }
  1417     GST_OBJECT_UNLOCK (nuv);
  1418   }
  1419 
  1420   flush = !!(flags & GST_SEEK_FLAG_FLUSH);
  1421   
  1422   if (flush) {
  1423     gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_start ());
  1424     if (nuv->priv->src_video_pad != NULL) {
  1425       gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_start ());
  1426     }
  1427 
  1428     if (nuv->priv->src_audio_pad != NULL) {
  1429       gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_start ());
  1430     }
  1431   }
  1432   else {
  1433     gst_pad_pause_task (nuv->priv->sinkpad);
  1434   }
  1435 
  1436   GST_PAD_STREAM_LOCK (nuv->priv->sinkpad);
  1437   GST_OBJECT_LOCK (nuv);
  1438 
  1439 
  1440   if (cur == GST_CLOCK_TIME_NONE)
  1441     cur = 0;
  1442   if (stop == GST_CLOCK_TIME_NONE)
  1443     stop = nuv->priv->duration_time;
  1444 
  1445   if (cur_type == GST_SEEK_TYPE_SET)
  1446     segment_start = cur;
  1447   else if (cur_type == GST_SEEK_TYPE_CUR)
  1448     segment_start = nuv->priv->segment_start + cur;
  1449   else
  1450     segment_start = nuv->priv->segment_start;
  1451 
  1452   if (stop_type == GST_SEEK_TYPE_SET)
  1453     segment_stop = stop;
  1454   else if (stop_type == GST_SEEK_TYPE_CUR)
  1455     segment_stop = nuv->priv->segment_stop + stop;
  1456   else
  1457     segment_stop = nuv->priv->segment_stop;
  1458 
  1459   segment_start = CLAMP (segment_start, 0, nuv->priv->duration_time);
  1460   segment_stop = CLAMP (segment_stop, 0, nuv->priv->duration_time);
  1461 
  1462   entry = gst_nuv_demux_do_seek_index (nuv, segment_start,
  1463       segment_stop, format);
  1464 
  1465   if (entry == NULL) {
  1466     GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
  1467     goto seek_error;
  1468   }
  1469 
  1470   segment_start = entry->timecode;
  1471 
  1472   nuv->priv->segment_start = segment_start;
  1473   nuv->priv->segment_stop = segment_stop;
  1474 
  1475   GST_OBJECT_UNLOCK (nuv);
  1476 
  1477   if (!nuv->priv->eos) {
  1478     GstMessage *msg;
  1479     msg = gst_message_new_segment_start (GST_OBJECT (nuv), GST_FORMAT_TIME,
  1480       nuv->priv->segment_start);
  1481   
  1482     gst_element_post_message (GST_ELEMENT (nuv), msg);
  1483   }
  1484 
  1485   GST_DEBUG_OBJECT (nuv, "NEW SEGMENT START %" G_GUINT64_FORMAT ", STOP %" G_GUINT64_FORMAT, 
  1486     segment_start, segment_stop);
  1487   newsegment_event = gst_event_new_new_segment (FALSE, rate,
  1488     GST_FORMAT_TIME, segment_start, segment_stop, segment_start);
  1489 
  1490 
  1491   if (flush) {
  1492     if (nuv->priv->src_video_pad != NULL) {
  1493       gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_stop ());
  1494     }
  1495 
  1496     if (nuv->priv->src_audio_pad != NULL) {
  1497       gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_stop ());
  1498     }
  1499 
  1500     gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_stop ());
  1501   }
  1502 
  1503   
  1504   if (nuv->priv->src_video_pad != NULL) {
  1505     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
  1506   }
  1507   if (nuv->priv->src_audio_pad != NULL) {
  1508     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
  1509   } 
  1510 
  1511   gst_event_unref (newsegment_event);
  1512 
  1513   nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
  1514   nuv->priv->offset = entry->offset;
  1515 
  1516   gst_pad_start_task (nuv->priv->sinkpad, (GstTaskFunction) gst_nuv_demux_loop,
  1517       nuv->priv->sinkpad);
  1518 
  1519   GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
  1520   return TRUE;
  1521 
  1522 seek_error:
  1523   GST_DEBUG_OBJECT (nuv, "Got a seek error");
  1524   GST_OBJECT_UNLOCK (nuv);
  1525   GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
  1526   return FALSE;
  1527 
  1528 }
  1529 
  1530 static gboolean
  1531 gst_nuv_demux_srcpad_event (GstPad * pad, GstEvent * event)
  1532 {
  1533   gboolean res = FALSE;
  1534   GstNuvDemux *nuv;
  1535 
  1536   nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1537   
  1538   switch (GST_EVENT_TYPE (event)) {
  1539     case GST_EVENT_SEEK:
  1540       res = gst_nuv_demux_do_seek (nuv, event);
  1541       break;
  1542     default:
  1543       res = FALSE;
  1544       break;
  1545   }
  1546   
  1547   gst_object_unref (nuv);
  1548   return res;
  1549 }
  1550 
  1551 static gboolean
  1552 gst_nuv_demux_sink_event (GstPad * pad, GstEvent * event)
  1553 {
  1554 	gboolean res = FALSE;
  1555 	GstNuvDemux *nuv;
  1556 
  1557 	nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1558 	
  1559 	switch  (GST_EVENT_TYPE (event)) {
  1560 	#if 0
  1561 		case GST_EVENT_NEWSEGMENT:
  1562 		{
  1563 			gboolean update;
  1564 			gdouble rate;
  1565 			GstFormat format;
  1566 			gint64 start;
  1567 			gint64 stop;
  1568 			gint64 position;
  1569 
  1570 			gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &position);				
  1571 			if ((format == GST_FORMAT_BYTES) && (start == 0)) {
  1572 				g_debug ("NEW SEGMENT 0");					
  1573 				if (nuv->priv->segment > 0) {
  1574 					nuv->priv->new_file = TRUE;					
  1575 					nuv->priv->state = GST_NUV_DEMUX_START;
  1576 					nuv->priv->offset = 0;
  1577 				}
  1578 				nuv->priv->segment++;					
  1579 
  1580 				/*
  1581 				newsegment_event = gst_event_new_new_segment (FALSE, rate,
  1582 				  GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
  1583 
  1584     			gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
  1585     			gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
  1586   				gst_event_unref (newsegment_event);
  1587 				*/
  1588 				res = gst_pad_event_default(pad, event);
  1589 			}
  1590 			break;
  1591 		}
  1592 	#endif
  1593 		case GST_EVENT_FLUSH_START:
  1594 		{
  1595 			nuv->priv->new_file = TRUE;					
  1596 			nuv->priv->state = GST_NUV_DEMUX_START;
  1597 			nuv->priv->offset = 0;
  1598 			res = gst_pad_event_default (pad, event);
  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