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