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