gst-plugins-nuvdemux/nuvdemux/gstnuvdemux.c
author melunko
Wed Mar 21 18:14:11 2007 +0000 (2007-03-21)
branchtrunk
changeset 424 66f318a2f4b2
parent 318 0944bea397f9
child 425 9638cccfa1fe
permissions -rw-r--r--
[svn r429] Fixed link error with gmyth_program_info_print() when debug is disabled
     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 /* FIXME Not sure of this one */
   145 typedef struct
   146 {
   147     gint             i_version;
   148     guint32		     i_video_fcc;
   149 
   150     guint32		     i_audio_fcc;
   151     gint             i_audio_sample_rate;
   152     gint             i_audio_bits_per_sample;
   153     gint             i_audio_channels;
   154     gint             i_audio_compression_ratio;
   155     gint             i_audio_quality;
   156     gint             i_rtjpeg_quality;
   157     gint             i_rtjpeg_luma_filter;
   158     gint             i_rtjpeg_chroma_filter;
   159     gint             i_lavc_bitrate;
   160     gint             i_lavc_qmin;
   161     gint             i_lavc_qmax;
   162     gint             i_lavc_maxqdiff;
   163     gint64         	 i_seekable_offset;
   164     gint64           i_keyframe_adjust_offset;
   165 
   166 } nuv_extended_header;
   167 
   168 typedef enum {
   169   GST_NUV_DEMUX_START,
   170   GST_NUV_DEMUX_HEADER_DATA,
   171   GST_NUV_DEMUX_EXTRA_DATA,
   172   GST_NUV_DEMUX_MPEG_DATA,
   173   GST_NUV_DEMUX_EXTEND_HEADER,
   174   GST_NUV_DEMUX_EXTEND_HEADER_DATA,
   175   GST_NUV_DEMUX_FRAME_HEADER,
   176   GST_NUV_DEMUX_MOVI,
   177   GST_NUV_DEMUX_INVALID_DATA
   178 } GstNuvDemuxState;
   179 
   180 struct _GstNuvDemuxPrivate {
   181   /* used for indicate the mode */
   182   guint         mode;
   183 
   184   /* used on push mode */
   185   GstAdapter    *adapter;
   186 
   187   /* pads */
   188   GstPad        *sinkpad;
   189   GstPad        *src_video_pad;
   190   GstPad        *src_audio_pad;
   191 
   192   /* Flow control */
   193   GstFlowReturn last_video_return;
   194   GstFlowReturn last_audio_return;
   195   gboolean      more_data;
   196 
   197   /* NUV decoding state */
   198   GstNuvDemuxState  state;
   199   GstSegment        segment;
   200   guint64           last_update;
   201   guint64           offset;
   202   guint64           streamer_offset;
   203 
   204   /* duration information */
   205   gint64            duration_bytes;
   206   gint64            duration_time;
   207   gint64            duration_average;
   208 
   209   /* segment control info */
   210   gboolean          new_audio_segment;
   211   gboolean          new_video_segment;
   212 
   213   /* Mpeg ExtraData */
   214   guint64       mpeg_data_size;
   215   GstBuffer     *mpeg_buffer;
   216 
   217   /* Headers */
   218   nuv_header h;
   219   nuv_extended_header eh;
   220   nuv_frame_header fh;
   221 
   222   /* anothers info */
   223   gint64            time_start;
   224   gint64            time_diff;
   225   gint64            time_qos;
   226   guint64           last_frame_time;
   227 };
   228 
   229 
   230 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
   231     GST_PAD_SINK,
   232     GST_PAD_ALWAYS,
   233     GST_STATIC_CAPS ("video/x-nuv"));
   234 
   235 static GstStaticPadTemplate audio_src_template =
   236 GST_STATIC_PAD_TEMPLATE ("audio_src",
   237     GST_PAD_SRC,
   238     GST_PAD_SOMETIMES,
   239     GST_STATIC_CAPS_ANY);
   240 
   241 static GstStaticPadTemplate video_src_template =
   242 GST_STATIC_PAD_TEMPLATE ("video_src",
   243     GST_PAD_SRC,
   244     GST_PAD_SOMETIMES,
   245     GST_STATIC_CAPS_ANY);
   246 
   247 static void gst_nuv_demux_dispose (GObject * object);
   248 static void gst_nuv_demux_finalize (GObject * object);
   249 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
   250     GstStateChange transition);
   251 static void gst_nuv_demux_loop (GstPad * pad);
   252 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
   253 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
   254 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
   255     gboolean active);
   256 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
   257     gboolean active);
   258 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
   259 static gboolean gst_nuv_demux_sink_event    (GstPad *pad, GstEvent *event);
   260 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
   261     gboolean move, GstBuffer ** buffer);
   262 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
   263 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
   264 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
   265 static void gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp);
   266 static gint64 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv);
   267 static gint64 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv);
   268 
   269 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
   270 GstBuffer * gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes);
   271 #endif
   272 
   273 
   274 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
   275 
   276 /******************************************************************************
   277  * Utils function
   278  ******************************************************************************/
   279 #if G_BYTE_ORDER == G_BIG_ENDIAN
   280 static inline gdouble
   281 _gdouble_swap_le_be (gdouble * d)
   282 {
   283   union
   284   {
   285     guint64 i;
   286     gdouble d;
   287   } u;
   288 
   289   u.d = *d;
   290   u.i = GUINT64_SWAP_LE_BE (u.i);
   291   return u.d;
   292 }
   293 
   294 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
   295 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
   296 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
   297 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
   298 
   299 static void
   300 double2fraction (double in, int *num, int *denom)
   301 {
   302     if (in == 29.97) {
   303         *num = 30000;
   304         *denom = 1001;
   305     } else if (in == 23.976) {
   306         *num = 24000;
   307         *denom = 1001;
   308     } else {
   309         *denom = 1;
   310         while (in - floor(in) >= 0.1) {
   311             *denom *= 10;
   312             in *= 10.0;
   313         }
   314         *num = (int)floor(in);
   315     }
   316 }
   317 
   318 /* GObject Functions */
   319 
   320 static void
   321 gst_nuv_demux_base_init (gpointer klass)
   322 {
   323   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
   324 
   325   gst_element_class_add_pad_template (element_class,
   326       gst_static_pad_template_get (&audio_src_template));
   327 
   328   gst_element_class_add_pad_template (element_class,
   329       gst_static_pad_template_get (&video_src_template));
   330 
   331   gst_element_class_add_pad_template (element_class,
   332       gst_static_pad_template_get (&sink_template));
   333   gst_element_class_set_details (element_class, &gst_nuv_demux_details);
   334 }
   335 
   336 static void
   337 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
   338 {
   339   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   340   GObjectClass *gobject_class = (GObjectClass *) klass;
   341 
   342   GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
   343       0, "Demuxer for NUV streams");
   344 
   345   parent_class = g_type_class_peek_parent (klass);
   346 
   347   gobject_class->dispose = gst_nuv_demux_dispose;
   348   gobject_class->finalize = gst_nuv_demux_finalize;
   349   gstelement_class->change_state = gst_nuv_demux_change_state;
   350 
   351   g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate));
   352 }
   353 
   354 static void
   355 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
   356 {
   357   nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv);
   358   nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
   359 
   360   /* creating adapter */
   361   nuv->priv->mode = NUV_PUSH_MODE;
   362   nuv->priv->adapter = gst_adapter_new ();
   363 
   364   g_debug ("Adapter %p", nuv->priv->adapter);
   365 
   366   nuv->priv->new_audio_segment = TRUE;
   367   nuv->priv->new_video_segment = TRUE;
   368 
   369   gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate);
   370   gst_pad_set_activatepull_function (nuv->priv->sinkpad,
   371       gst_nuv_demux_sink_activate_pull);
   372   gst_pad_set_activatepush_function (nuv->priv->sinkpad,
   373       gst_nuv_demux_sink_activate_push);
   374   gst_pad_set_chain_function (nuv->priv->sinkpad,
   375       GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
   376   gst_pad_set_event_function (nuv->priv->sinkpad,
   377       gst_nuv_demux_sink_event);
   378 
   379   gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad);
   380 
   381 }
   382 
   383 static void
   384 gst_nuv_demux_dispose (GObject * object)
   385 {
   386   GstNuvDemux *nuv = GST_NUV_DEMUX (object);
   387 
   388   g_debug ("NUV DISPOSE");
   389 
   390   if (nuv->priv->mpeg_buffer != NULL) {
   391     gst_buffer_unref (nuv->priv->mpeg_buffer);
   392   }
   393 
   394   gst_nuv_demux_reset (GST_NUV_DEMUX (object));
   395   gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (object));
   396 
   397   if (nuv->priv->adapter != NULL) {
   398     gst_object_unref (nuv->priv->adapter);
   399   }
   400 }
   401 
   402 static void
   403 gst_nuv_demux_finalize (GObject * object)
   404 {
   405   G_OBJECT_CLASS (parent_class)->finalize (object);
   406 }
   407 
   408 
   409 /* HeaderLoad:
   410  */
   411 static GstFlowReturn
   412 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header *h)
   413 {
   414   GstBuffer *buffer = NULL;
   415   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
   416 
   417   if ((res != GST_FLOW_OK) || (buffer == NULL)) {
   418       goto done;
   419   }
   420 
   421   memcpy (h->id, buffer->data, 12);
   422   memcpy (h->version, buffer->data + 12, 5);
   423   h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
   424   h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
   425   h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
   426   h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
   427   h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
   428   h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
   429   h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
   430   /* get the num and denom values from fps */
   431   double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
   432   h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
   433   h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
   434   h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
   435   h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
   436 
   437   GST_DEBUG_OBJECT (nuv,
   438       "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
   439       h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
   440       h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
   441       h->i_keyframe_distance);
   442 
   443 done:
   444   if (buffer != NULL) {
   445      gst_buffer_unref (buffer);
   446      buffer = NULL;
   447   }
   448   return res;
   449 }
   450 
   451 static GstFlowReturn
   452 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
   453 {
   454   GstFlowReturn res;
   455 
   456   res = gst_nuv_demux_header_load (nuv, &nuv->priv->h);
   457   if (res == GST_FLOW_OK)
   458     nuv->priv->state = GST_NUV_DEMUX_EXTRA_DATA;
   459   return res;
   460 }
   461 
   462 /*
   463  * Read NUV file tag
   464  */
   465 static GstFlowReturn
   466 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
   467 {
   468   GstFlowReturn res = GST_FLOW_OK;
   469   GstBuffer *file_header = NULL;
   470 
   471   res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
   472   if (res == GST_FLOW_OK) {
   473     if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
   474         strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
   475       nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
   476     } else {
   477       GST_DEBUG_OBJECT (nuv, "error parsing file header");
   478       nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
   479       res = GST_FLOW_ERROR;
   480     }
   481   }
   482 
   483   if (file_header != NULL) {
   484     gst_buffer_unref (file_header);
   485     file_header = NULL;
   486   }
   487   return res;
   488 }
   489 
   490 /* FrameHeaderLoad:
   491  */
   492 static GstFlowReturn
   493 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header *h)
   494 {
   495   unsigned char *data;
   496   GstBuffer *buf = NULL;
   497 
   498   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
   499 
   500   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   501       goto done;
   502   }
   503 
   504   data = buf->data;
   505 
   506   h->i_type = GPOINTER_TO_INT (data[0]);
   507   h->i_compression = GPOINTER_TO_INT (data[1]);
   508   h->i_keyframe = GPOINTER_TO_INT (data[2]);
   509   h->i_filters = GPOINTER_TO_INT (data[3]);
   510   h->i_timecode = GST_READ_UINT32_LE (&data[4]);
   511   h->i_length = GST_READ_UINT32_LE (&data[8]);
   512 
   513   GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
   514       h->i_type,
   515       h->i_compression ? h->i_compression : ' ',
   516       h->i_keyframe ? h->i_keyframe : ' ',
   517       h->i_filters, h->i_timecode, h->i_length);
   518 
   519 done:  
   520   if (buf != NULL) {
   521       gst_buffer_unref (buf);
   522       buf = NULL;
   523   }
   524   return res;
   525 }
   526 
   527 static GstFlowReturn
   528 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
   529     nuv_extended_header * h)
   530 {
   531   unsigned char *data;
   532   GstBuffer *buff = NULL;
   533 
   534   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
   535 
   536   if ((res != GST_FLOW_OK) || (buff == NULL)) {
   537       goto done;
   538   }
   539 
   540   data = buff->data;
   541   h->i_version = GST_READ_UINT32_LE (&data[0]);
   542   h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
   543   h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
   544   h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
   545   h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
   546   h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
   547   h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
   548   h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
   549   h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
   550   h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
   551   h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
   552   h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
   553   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
   554   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
   555   h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
   556   h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
   557   h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
   558 
   559   GST_DEBUG_OBJECT (nuv,
   560       "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
   561       "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
   562       h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
   563       h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
   564       h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
   565       h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
   566       h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
   567       h->i_keyframe_adjust_offset);
   568 
   569 done:
   570   if (buff != NULL) {
   571     gst_buffer_unref (buff);
   572     buff = NULL;
   573   }
   574   return res;
   575 }
   576 
   577 
   578 /* Query Functions */
   579 static const GstQueryType *
   580 gst_nuv_demux_get_src_query_types (GstPad * pad)
   581 {
   582   static const GstQueryType src_types[] = {
   583     GST_QUERY_POSITION,
   584     GST_QUERY_DURATION,
   585     0
   586   };
   587 
   588   return src_types;
   589 }
   590 
   591 static gboolean
   592 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
   593 {
   594   gboolean res = FALSE;
   595   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   596 
   597   switch (GST_QUERY_TYPE (query)) {
   598     case GST_QUERY_POSITION:
   599       if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
   600 
   601         gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
   602         res = TRUE;
   603       }
   604       break;
   605     case GST_QUERY_DURATION:
   606       {
   607         gint64 duration = 0;
   608         duration = gst_nuv_demux_get_time_duration (nuv);
   609         if (duration == GST_CLOCK_TIME_NONE) {
   610             duration = nuv->priv->duration_average;
   611         }
   612         if (duration != GST_CLOCK_TIME_NONE) {
   613             gst_query_set_duration (query, GST_FORMAT_TIME, duration);
   614             res = TRUE;
   615         }
   616       }
   617       break;
   618     default:
   619       res = FALSE;
   620       break;
   621   }
   622 
   623   gst_object_unref (nuv);
   624 
   625   return res;
   626 }
   627 
   628 static GstPad*
   629 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
   630 {
   631     GstPad *pad = NULL;
   632     pad = gst_pad_new_from_static_template (template, name);
   633     gst_pad_set_caps (pad, caps);
   634     gst_pad_set_active (pad, TRUE);
   635     gst_pad_use_fixed_caps (pad);
   636     gst_element_add_pad (GST_ELEMENT (nuv), pad);
   637 
   638     return pad;
   639 }
   640 
   641 static void
   642 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
   643 {
   644   if (nuv->priv->h.i_video_blocks != 0) {
   645     GstCaps *video_caps = NULL;
   646 
   647     video_caps = gst_caps_new_simple ("video/x-divx",
   648         "divxversion", G_TYPE_INT, 4,
   649         "width", G_TYPE_INT, nuv->priv->h.i_width,
   650         "height", G_TYPE_INT, nuv->priv->h.i_height,
   651         "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
   652         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
   653         "pixel-aspect-ratio", GST_TYPE_FRACTION,
   654         (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
   655 
   656     nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
   657     gst_caps_unref (video_caps);
   658   }
   659 
   660   if (nuv->priv->h.i_audio_blocks != 0) {
   661     GstCaps *audio_caps = NULL;
   662 
   663     audio_caps = gst_caps_new_simple ("audio/mpeg",
   664         "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
   665         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
   666         "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
   667         "layer", G_TYPE_INT, 3, // fixme: magic number
   668         "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
   669     
   670     nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
   671     gst_caps_unref (audio_caps);
   672   }
   673 
   674   gst_element_no_more_pads (GST_ELEMENT (nuv));
   675 }
   676 
   677 static GstFlowReturn
   678 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
   679 {
   680   GstFlowReturn ret = GST_FLOW_OK;
   681 
   682   ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
   683   if (ret != GST_FLOW_OK)
   684     return ret;
   685 
   686   nuv->priv->state = GST_NUV_DEMUX_MOVI;
   687   return ret;
   688 }
   689 
   690 static gboolean
   691 gst_nuv_combine_flow (GstNuvDemux *nuv)
   692 {
   693     GstFlowReturn ret_video = nuv->priv->last_video_return;
   694     GstFlowReturn ret_audio = nuv->priv->last_audio_return;
   695 
   696     if ((ret_video != GST_FLOW_OK) &&
   697         (ret_audio != GST_FLOW_OK))
   698         return FALSE;
   699 
   700     if (GST_FLOW_IS_FATAL (ret_video))
   701         return FALSE;
   702 
   703     if (GST_FLOW_IS_FATAL (ret_audio))
   704         return FALSE;
   705 
   706     return TRUE;
   707 }
   708 
   709 static GstFlowReturn
   710 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
   711 {
   712   GstFlowReturn ret = GST_FLOW_OK;
   713   GstPad *pad = NULL;
   714   guint64 timestamp;
   715   GstBuffer *buf = NULL;
   716   nuv_frame_header h;
   717 
   718   h = nuv->priv->fh;
   719 
   720   if (h.i_type == 'R')
   721     goto done;
   722 
   723   if (h.i_length > 0) {
   724 	  ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
   725 	  if ((ret != GST_FLOW_OK) || (buf == NULL)) {
   726           goto done;
   727       }
   728 
   729       if ((h.i_timecode < 0)) {
   730         h.i_timecode = 0;
   731 	    //goto done;
   732       }
   733 
   734       timestamp = h.i_timecode * GST_MSECOND;
   735       GST_BUFFER_TIMESTAMP (buf) = timestamp;
   736   }
   737   else {
   738     goto done;
   739   }
   740 
   741 
   742   switch (h.i_type) {
   743     case 'V':
   744     {
   745       pad = nuv->priv->src_video_pad;
   746 
   747       if (nuv->priv->new_video_segment) {
   748 
   749         /* send new segment event*/
   750         gst_pad_push_event (nuv->priv->src_video_pad,
   751           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   752                                      GST_CLOCK_TIME_NONE, 0));
   753 
   754         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   755             nuv->priv->time_start = timestamp;
   756         }
   757         nuv->priv->new_video_segment = FALSE;
   758       }
   759 
   760       break;
   761     }
   762     case 'A':
   763     {
   764       pad = nuv->priv->src_audio_pad;
   765 
   766       if (nuv->priv->new_audio_segment) {
   767         /* send new segment event*/
   768         gst_pad_push_event (nuv->priv->src_audio_pad,
   769           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   770                                      GST_CLOCK_TIME_NONE, 0));
   771 
   772         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   773             nuv->priv->time_start = timestamp;
   774         }
   775         nuv->priv->new_audio_segment = FALSE;
   776       }
   777 
   778       break;
   779     }
   780     case 'S':
   781    {
   782       switch (h.i_compression) {
   783         case 'V':
   784           GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
   785           gst_pad_push_event (nuv->priv->src_video_pad,
   786               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
   787               		GST_CLOCK_TIME_NONE, 0));
   788           break;
   789         case 'A':
   790           GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
   791           gst_pad_push_event (nuv->priv->src_audio_pad,
   792               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   793               		GST_CLOCK_TIME_NONE, 0));
   794           break;
   795         default:
   796           break;
   797       }
   798       goto done;
   799     }
   800     default:
   801       break;
   802   }
   803 
   804   if ((buf != NULL) && (pad != NULL)) {
   805       /* update average time */
   806       nuv->priv->streamer_offset += h.i_length;
   807       gst_segment_set_last_stop (&nuv->priv->segment, GST_FORMAT_TIME, timestamp);
   808       gst_nuv_demux_update_duration (nuv, timestamp);
   809 
   810       /* pushing the buffer */
   811       gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
   812       ret = gst_pad_push (pad, buf);
   813       buf = NULL;
   814 
   815       if (ret != GST_FLOW_OK) {
   816         GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
   817 
   818         if (pad == nuv->priv->src_video_pad) {
   819             nuv->priv->last_video_return = ret;
   820         }
   821         else if (pad == nuv->priv->src_audio_pad) {
   822             nuv->priv->last_audio_return = ret;
   823         }
   824 
   825         /* verify anothers flow if is necessary stop task */
   826         if (gst_nuv_combine_flow (nuv) != FALSE) {
   827             ret = GST_FLOW_OK;
   828         }
   829 
   830         //GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
   831       }
   832   } 
   833 
   834 done:
   835   if (buf != NULL) {
   836       gst_buffer_unref (buf);
   837       buf = NULL;
   838   }
   839   if (ret == GST_FLOW_OK) {
   840      nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   841      memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
   842   }
   843   return ret;
   844 }
   845 
   846 static GstFlowReturn
   847 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
   848 {
   849   GstFlowReturn ret = GST_FLOW_OK;
   850 
   851   /* ffmpeg extra data */
   852   ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
   853       &nuv->priv->mpeg_buffer);
   854   if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
   855     return ret;
   856   }
   857 
   858   GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
   859   nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   860   return ret;
   861 }
   862 
   863 static GstFlowReturn
   864 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
   865 {
   866   GstFlowReturn ret = GST_FLOW_OK;
   867 
   868   /* Load 'D' */
   869   nuv_frame_header h;
   870 
   871   ret = gst_nuv_demux_frame_header_load (nuv, &h);
   872   if (ret != GST_FLOW_OK)
   873     return ret;
   874 
   875   if (h.i_type != 'D') {
   876     GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
   877     return GST_FLOW_NOT_SUPPORTED;
   878   }
   879 
   880   if (h.i_length > 0) {
   881     if (h.i_compression == 'F') {
   882       nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
   883     } else {
   884       GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
   885       return GST_FLOW_NOT_SUPPORTED;
   886     }
   887   } else {
   888     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   889   }
   890 
   891   return ret;
   892 }
   893 
   894 static GstFlowReturn
   895 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
   896 {
   897   GstFlowReturn ret = GST_FLOW_OK;
   898 
   899   ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
   900   if (ret != GST_FLOW_OK)
   901     return ret;
   902 
   903   gst_nuv_demux_create_pads (nuv);
   904   nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   905   return ret;
   906 }
   907 
   908 static GstFlowReturn
   909 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
   910 {
   911   GstBuffer *buf = NULL;
   912   GstFlowReturn res = GST_FLOW_OK;
   913 
   914   res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
   915   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   916     if (buf != NULL) {
   917       gst_buffer_unref (buf);
   918     }
   919     return res;
   920   }
   921 
   922   if (buf->data[0] == 'X') {
   923     gst_buffer_unref (buf);
   924     buf = NULL;
   925     nuv_frame_header h;
   926 
   927     res = gst_nuv_demux_frame_header_load (nuv, &h);
   928     if (res != GST_FLOW_OK)
   929       return res;
   930 
   931     if (h.i_length != 512) {
   932       return GST_FLOW_ERROR;
   933     }
   934     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
   935   } else {
   936     nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
   937     g_object_unref (buf);
   938     GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
   939         (_("incomplete NUV support")), ("incomplete NUV support"));
   940     return GST_FLOW_ERROR;
   941   }
   942   return res;
   943 }
   944 
   945 static GstFlowReturn
   946 gst_nuv_demux_play (GstPad * pad)
   947 {
   948   GstFlowReturn res = GST_FLOW_OK;
   949   GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
   950 
   951   switch (nuv->priv->state) {
   952     case GST_NUV_DEMUX_START:
   953       res = gst_nuv_demux_stream_file_header (nuv);
   954       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   955         goto pause;
   956       }
   957       break;
   958 
   959     case GST_NUV_DEMUX_HEADER_DATA:
   960       res = gst_nuv_demux_stream_header_data (nuv);
   961       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   962         goto pause;
   963       }
   964       break;
   965 
   966     case GST_NUV_DEMUX_EXTRA_DATA:
   967       res = gst_nuv_demux_stream_extra_data (nuv);
   968       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   969         goto pause;
   970       }
   971       break;
   972 
   973     case GST_NUV_DEMUX_MPEG_DATA:
   974       res = gst_nuv_demux_stream_mpeg_data (nuv);
   975       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   976         goto pause;
   977       }
   978       break;
   979 
   980     case GST_NUV_DEMUX_EXTEND_HEADER:
   981       res = gst_nuv_demux_stream_extend_header (nuv);
   982       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   983         goto pause;
   984       }
   985       break;
   986 
   987     case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
   988       res = gst_nuv_demux_stream_extend_header_data (nuv);
   989       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   990         goto pause;
   991       }
   992       break;
   993 
   994     case GST_NUV_DEMUX_FRAME_HEADER:
   995       res = gst_nuv_demux_read_head_frame (nuv);
   996       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   997         goto pause;
   998       }
   999       break;
  1000 
  1001     case GST_NUV_DEMUX_MOVI:
  1002       res = gst_nuv_demux_stream_data (nuv);
  1003       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1004         goto pause;
  1005       }
  1006       break;
  1007 
  1008     case GST_NUV_DEMUX_INVALID_DATA:
  1009       goto pause;
  1010       break;
  1011     default:
  1012       g_assert_not_reached ();
  1013   }
  1014 
  1015   return GST_FLOW_OK;
  1016 
  1017 pause:
  1018   GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
  1019   gst_pad_pause_task (nuv->priv->sinkpad);
  1020   if (GST_FLOW_IS_FATAL (res)) {
  1021     GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
  1022         (_("Internal data stream error.")),
  1023         ("streaming stopped, reason %s", gst_flow_get_name (res)));
  1024 
  1025     gst_nuv_demux_send_eos (nuv);
  1026   }
  1027   return res;
  1028 }
  1029 
  1030 static void
  1031 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
  1032 {
  1033   gst_element_post_message (GST_ELEMENT (nuv),
  1034       gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
  1035 
  1036   if (nuv->priv->src_video_pad)
  1037     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
  1038   if (nuv->priv->src_audio_pad)
  1039     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
  1040 }
  1041 
  1042 static GstFlowReturn
  1043 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
  1044     GstBuffer ** buffer)
  1045 {
  1046   GstFlowReturn ret = GST_FLOW_OK;
  1047 
  1048   if (size == 0) {
  1049     return ret;
  1050   }
  1051 
  1052   if (nuv->priv->mode == NUV_PULL_MODE) {
  1053     ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
  1054     if (ret == GST_FLOW_OK) {
  1055         if (move) {
  1056 		    nuv->priv->offset += size;
  1057 		}
  1058       /* got eos */
  1059     } else if (ret == GST_FLOW_UNEXPECTED) {
  1060       if (buffer != NULL)
  1061           gst_buffer_unref (buffer);
  1062 
  1063       gst_nuv_demux_send_eos (nuv);
  1064       return GST_FLOW_WRONG_STATE;
  1065     }
  1066   } else {
  1067     if (gst_adapter_available (nuv->priv->adapter) < size) {
  1068       nuv->priv->more_data = TRUE;
  1069       return GST_FLOW_ERROR_NO_DATA;
  1070     }
  1071     if (move) {
  1072       *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
  1073     } else {
  1074       guint8 *data = NULL;
  1075       data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
  1076       *buffer = gst_buffer_new ();
  1077       gst_buffer_set_data (*buffer, data, size);
  1078     }
  1079   }
  1080   return ret;
  1081 }
  1082 
  1083 static gboolean
  1084 gst_nuv_demux_sink_activate (GstPad * sinkpad)
  1085 {
  1086   gboolean res = TRUE;
  1087   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1088  
  1089   g_debug ("SINK ACTIVATE");
  1090 
  1091   if (gst_pad_check_pull_range (sinkpad)) {
  1092     g_debug ("PULL");
  1093     gst_adapter_clear (nuv->priv->adapter);
  1094     res = gst_pad_activate_pull (sinkpad, TRUE);
  1095   } else {
  1096     g_debug ("PUSH");
  1097     gst_adapter_clear (nuv->priv->adapter);
  1098     res = gst_pad_activate_push (sinkpad, TRUE);
  1099   }
  1100   
  1101   g_object_unref (nuv);
  1102   return res;
  1103 }
  1104 
  1105 static gboolean
  1106 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
  1107 {
  1108   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1109 
  1110   if (active) {
  1111     GST_DEBUG_OBJECT (nuv, "activating pull function");
  1112     nuv->priv->mode = NUV_PULL_MODE;
  1113     gst_adapter_clear (nuv->priv->adapter);
  1114 
  1115     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
  1116   } else {
  1117     GST_DEBUG_OBJECT (nuv, "deactivating pull function");
  1118     g_debug ("STOP TASK");
  1119     gst_pad_stop_task (sinkpad);
  1120   }
  1121   gst_object_unref (nuv);
  1122 
  1123   return TRUE;
  1124 }
  1125 
  1126 static gboolean
  1127 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
  1128 {
  1129   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1130 
  1131   if (active) {
  1132     nuv->priv->mode = NUV_PUSH_MODE;
  1133     gst_adapter_clear (nuv->priv->adapter);
  1134 
  1135     GST_DEBUG_OBJECT (nuv, "activating push/chain function");
  1136   } else {
  1137     GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
  1138   }
  1139 
  1140   gst_object_unref (nuv);
  1141 
  1142   return TRUE;
  1143 }
  1144 
  1145 
  1146 static gboolean
  1147 gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event)
  1148 {
  1149   gboolean res = FALSE;
  1150   GstNuvDemux *nuv;
  1151 
  1152   nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1153 
  1154   switch (GST_EVENT_TYPE (event)) {
  1155     case GST_EVENT_SEEK:
  1156     {
  1157       gdouble rate;
  1158       GstFormat format;
  1159       GstSeekFlags flags;
  1160       GstSeekType cur_type;
  1161       gint64 cur;
  1162       GstSeekType stop_type;
  1163       gint64 stop;
  1164 
  1165       gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur, &stop_type, &stop);
  1166       GST_DEBUG_OBJECT (nuv, "got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop);
  1167       g_debug ("got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop);
  1168       break;
  1169     }
  1170     case GST_EVENT_NEWSEGMENT:
  1171     {
  1172       gint64 start, stop, time;
  1173       gdouble rate;
  1174       GstFormat format;
  1175       gboolean update;
  1176 
  1177       GST_DEBUG_OBJECT (nuv, "got a new segment event");
  1178       gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &time);
  1179 
  1180       GST_DEBUG_OBJECT (nuv, "got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop);
  1181       g_debug ("got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop);
  1182       nuv->priv->duration_bytes = stop - start;
  1183       gst_event_unref (event);
  1184       res = TRUE;
  1185       break;
  1186     }
  1187     case GST_EVENT_QOS:
  1188     {
  1189 /*
  1190       gdouble proportion;
  1191       GstClockTimeDiff diff;
  1192       GstClockTime timestamp;
  1193 
  1194       gst_event_parse_qos (event, &proportion, &diff, &timestamp);
  1195       if (diff > 0)
  1196         nuv->time_qos = timecode + diff;
  1197       else
  1198         nuv->time_qos = -1;
  1199 */
  1200       break;
  1201     }
  1202 
  1203     case GST_EVENT_EOS:
  1204     default:
  1205       res = gst_pad_event_default (pad, event);
  1206       break;
  1207   }
  1208 
  1209   gst_object_unref (nuv);
  1210   return res;
  1211 }
  1212 
  1213 static GstFlowReturn
  1214 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
  1215 {
  1216   GstFlowReturn ret = GST_FLOW_OK;
  1217   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1218   
  1219   if (nuv->priv->mode != NUV_PUSH_MODE)
  1220     return ret;
  1221 
  1222   g_debug ("CHAIN ADP %p", nuv->priv->adapter);
  1223 
  1224   gst_adapter_push (nuv->priv->adapter, buf);  
  1225   
  1226   while ((ret == GST_FLOW_OK) && (nuv->priv->more_data == FALSE)) {
  1227       ret = gst_nuv_demux_play (pad);
  1228   }
  1229 
  1230   nuv->priv->more_data = FALSE;
  1231   gst_object_unref (nuv);
  1232 
  1233   g_debug ("CHAIN END");
  1234   
  1235   return ret;
  1236 }
  1237 
  1238 static void
  1239 gst_nuv_demux_loop (GstPad * pad)
  1240 {
  1241   gst_nuv_demux_play (pad);
  1242 }
  1243 
  1244 static void
  1245 gst_nuv_demux_reset (GstNuvDemux * nuv)
  1246 {
  1247   nuv->priv->more_data = FALSE;
  1248   nuv->priv->state = GST_NUV_DEMUX_START;
  1249   nuv->priv->mode = NUV_PUSH_MODE;
  1250   nuv->priv->offset = 0;
  1251   nuv->priv->streamer_offset = 0;
  1252   nuv->priv->time_start = 0;
  1253   nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
  1254   nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
  1255   nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
  1256   nuv->priv->duration_average = GST_CLOCK_TIME_NONE;
  1257   gst_segment_init (&nuv->priv->segment, GST_FORMAT_TIME);
  1258 
  1259   gst_adapter_clear (nuv->priv->adapter);
  1260 
  1261   if (nuv->priv->mpeg_buffer != NULL) {
  1262     gst_buffer_unref (nuv->priv->mpeg_buffer);
  1263     nuv->priv->mpeg_buffer = NULL;
  1264   }
  1265 }
  1266 
  1267 static void
  1268 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
  1269 {
  1270   if (nuv->priv->src_video_pad) {
  1271     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
  1272     nuv->priv->src_video_pad = NULL;
  1273   }
  1274 
  1275   if (nuv->priv->src_audio_pad) {
  1276     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
  1277     nuv->priv->src_audio_pad = NULL;
  1278   }
  1279 }
  1280 
  1281 static GstStateChangeReturn
  1282 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
  1283 {
  1284   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  1285 
  1286   switch (transition) {
  1287     case GST_STATE_CHANGE_NULL_TO_READY:
  1288 	g_debug ("NULL_TO_READY");
  1289       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1290       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1291       break;
  1292     default:
  1293       break;
  1294   }
  1295 
  1296   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1297   if (ret == GST_STATE_CHANGE_FAILURE) {
  1298     goto done;
  1299   }
  1300 
  1301   switch (transition) {
  1302     case GST_STATE_CHANGE_READY_TO_NULL:
  1303 	g_debug ("READY_TO_NULL");
  1304       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1305       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1306       break;
  1307     default:
  1308       break;
  1309   }
  1310 
  1311 done:
  1312   return ret;
  1313 }
  1314 
  1315 static void
  1316 gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp)
  1317 {
  1318     guint64 interval = 0;
  1319 
  1320     if (gst_nuv_demux_get_time_duration (nuv) != GST_CLOCK_TIME_NONE)
  1321         return;
  1322 
  1323     interval  = current_timestamp - nuv->priv->last_update;
  1324 
  1325     if (interval > (10 * GST_SECOND)) {
  1326         GstMessage* msg = NULL;
  1327         gint64 average = 0;
  1328         gint64 duration_bytes = gst_nuv_demux_get_bytes_duration (nuv);
  1329 
  1330         if (duration_bytes == GST_CLOCK_TIME_NONE) {
  1331             return;
  1332         }
  1333 
  1334         interval = gst_util_uint64_scale (1, current_timestamp - nuv->priv->time_start, GST_SECOND);
  1335         if (interval > 0) {
  1336             average = gst_util_uint64_scale (1 , nuv->priv->streamer_offset, interval);
  1337         }
  1338         if (average > 0) {
  1339             nuv->priv->duration_average = gst_util_uint64_scale (GST_SECOND, duration_bytes, average);
  1340         }
  1341         
  1342         nuv->priv->last_update = current_timestamp;
  1343         msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_average);
  1344         gst_element_post_message (GST_ELEMENT (nuv), msg);
  1345         GST_DEBUG_OBJECT (nuv, "New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
  1346         g_debug ("New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
  1347     }
  1348 }
  1349 
  1350 static gint64
  1351 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv)
  1352 {
  1353     if (nuv->priv->duration_bytes == GST_CLOCK_TIME_NONE) {
  1354         GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
  1355         GstQuery *query = gst_query_new_duration (GST_FORMAT_BYTES);
  1356         if (gst_pad_query (peer, query)) {
  1357             gint64 duration;
  1358 
  1359             gst_query_parse_duration (query, NULL, &duration);
  1360             nuv->priv->duration_bytes = duration;
  1361         }
  1362         gst_object_unref (peer);
  1363         gst_query_unref (query);
  1364     }
  1365     return nuv->priv->duration_bytes;
  1366 }
  1367 
  1368 static gint64
  1369 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv)
  1370 {
  1371     if (nuv->priv->duration_time == GST_CLOCK_TIME_NONE) {
  1372         GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
  1373         GstQuery *query = gst_query_new_duration (GST_FORMAT_TIME);
  1374         if (gst_pad_query (peer, query)) {
  1375             gint64 duration;
  1376             gst_query_parse_duration (query, NULL, &duration);
  1377             nuv->priv->duration_time = duration;
  1378         }
  1379         gst_object_unref (peer);
  1380         gst_query_unref (query);
  1381     }
  1382     return nuv->priv->duration_time;
  1383 }
  1384 
  1385 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
  1386 GstBuffer *
  1387 gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
  1388 {
  1389   GstBuffer *buffer;
  1390   GstBuffer *cur;
  1391   guint8 *data;
  1392 
  1393   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
  1394   g_return_val_if_fail (nbytes > 0, NULL);
  1395 
  1396   GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);
  1397 
  1398   /* we don't have enough data, return NULL. This is unlikely
  1399    * as one usually does an _available() first instead of peeking a
  1400    * random size. */
  1401   if (G_UNLIKELY (nbytes > adapter->size))
  1402     return NULL;
  1403 
  1404   /* our head buffer has enough data left, return it */
  1405   cur = adapter->buflist->data;
  1406   if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) {
  1407     GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
  1408         nbytes);
  1409     buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes);
  1410 
  1411     gst_adapter_flush (adapter, nbytes);
  1412 
  1413     return buffer;
  1414   }
  1415 
  1416   data = gst_adapter_take (adapter, nbytes);
  1417   if (data == NULL)
  1418     return NULL;
  1419 
  1420   buffer = gst_buffer_new ();
  1421   GST_BUFFER_DATA (buffer) = data;
  1422   GST_BUFFER_MALLOCDATA (buffer) = data;
  1423   GST_BUFFER_SIZE (buffer) = nbytes;
  1424 
  1425   return buffer;
  1426 }
  1427 #endif
  1428 
  1429 
  1430 static gboolean
  1431 plugin_init (GstPlugin * plugin)
  1432 {
  1433 #ifdef ENABLE_NLS
  1434   setlocale (LC_ALL, "");
  1435   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  1436 #endif /* ENABLE_NLS */
  1437 
  1438   if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
  1439           GST_TYPE_NUV_DEMUX)) {
  1440     return FALSE;
  1441   }
  1442   return TRUE;
  1443 }
  1444 
  1445 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1446     GST_VERSION_MINOR,
  1447     "nuvdemux",
  1448     "Demuxes and muxes audio and video",
  1449      plugin_init, VERSION, "LGPL", "NuvDemux", "")
  1450