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