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