gst-plugins-nuvdemux/nuvdemux/gstnuvdemux.c
author renatofilho
Fri Dec 01 14:52:05 2006 +0000 (2006-12-01)
branchtrunk
changeset 164 81e9da9ad7db
parent 156 4b24a328c655
child 165 9726f9ebd4cb
permissions -rw-r--r--
[svn r165] code cleanup
     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_ret)
   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   nuv_header *h = g_new0 (nuv_header, 1);
   411 
   412   memcpy (h->id, buffer->data, 12);
   413   memcpy (h->version, buffer->data + 12, 5);
   414   h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
   415   h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
   416   h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
   417   h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
   418   h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
   419   h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
   420   h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
   421   /* get the num and denom values from fps */
   422   double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
   423   h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
   424   h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
   425   h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
   426   h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
   427 
   428   GST_DEBUG_OBJECT (nuv,
   429       "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
   430       h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
   431       h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
   432       h->i_keyframe_distance);
   433 
   434   *h_ret = h;
   435   gst_buffer_unref (buffer);
   436   return res;
   437 }
   438 
   439 static GstFlowReturn
   440 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
   441 {
   442   GstFlowReturn res;
   443 
   444   res = gst_nuv_demux_header_load (nuv, &nuv->priv->h);
   445   if (res == GST_FLOW_OK)
   446     nuv->priv->state = GST_NUV_DEMUX_EXTRA_DATA;
   447   return res;
   448 }
   449 
   450 /*
   451  * Read NUV file tag
   452  */
   453 static GstFlowReturn
   454 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
   455 {
   456   GstFlowReturn res = GST_FLOW_OK;
   457   GstBuffer *file_header = NULL;
   458 
   459   res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
   460   if ((res != GST_FLOW_OK) || (file_header == NULL)) {
   461     return res;
   462   } else {
   463     if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
   464         strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
   465       nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
   466     } else {
   467       GST_DEBUG_OBJECT (nuv, "error parsing file header");
   468       nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
   469       res = GST_FLOW_ERROR;
   470     }
   471   }
   472 
   473   if (file_header != NULL) {
   474     gst_buffer_unref (file_header);
   475   }
   476   
   477   return res;
   478 }
   479 
   480 /* FrameHeaderLoad:
   481  */
   482 static GstFlowReturn
   483 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header ** h_ret)
   484 {
   485   unsigned char *data;
   486   nuv_frame_header *h;
   487   GstBuffer *buf = NULL;
   488 
   489   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
   490 
   491   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   492     if (buf != NULL) {
   493       gst_buffer_unref (buf);
   494     }
   495     return res;
   496   }
   497 
   498   h = g_new0 (nuv_frame_header, 1);
   499   data = buf->data;
   500 
   501   h->i_type = GPOINTER_TO_INT (data[0]);
   502   h->i_compression = GPOINTER_TO_INT (data[1]);
   503   h->i_keyframe = GPOINTER_TO_INT (data[2]);
   504   h->i_filters = GPOINTER_TO_INT (data[3]);
   505   h->i_timecode = GST_READ_UINT32_LE (&data[4]);
   506   h->i_length = GST_READ_UINT32_LE (&data[8]);
   507 
   508   GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
   509       h->i_type,
   510       h->i_compression ? h->i_compression : ' ',
   511       h->i_keyframe ? h->i_keyframe : ' ',
   512       h->i_filters, h->i_timecode, h->i_length);
   513 
   514   *h_ret = h;
   515   gst_buffer_unref (buf);
   516   return GST_FLOW_OK;
   517 }
   518 
   519 static GstFlowReturn
   520 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
   521     nuv_extended_header ** h_ret)
   522 {
   523   unsigned char *data;
   524   GstBuffer *buff = NULL;
   525   nuv_extended_header *h;
   526 
   527 
   528   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
   529 
   530   if ((res != GST_FLOW_OK) || (buff == NULL)) {
   531     if (buff != NULL) {
   532       gst_buffer_unref (buff);
   533     }
   534     return res;
   535   }
   536 
   537   h = g_new0 (nuv_extended_header, 1);
   538   data = buff->data;
   539   h->i_version = GST_READ_UINT32_LE (&data[0]);
   540   h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
   541   h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
   542   h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
   543   h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
   544   h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
   545   h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
   546   h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
   547   h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
   548   h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
   549   h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
   550   h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
   551   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
   552   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
   553   h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
   554   h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
   555   h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
   556 
   557   GST_DEBUG_OBJECT (nuv,
   558       "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
   559       "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
   560       h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
   561       h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
   562       h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
   563       h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
   564       h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
   565       h->i_keyframe_adjust_offset);
   566 
   567   *h_ret = h;
   568   gst_buffer_unref (buff);
   569   return res;
   570 }
   571 
   572 
   573 /* Query Functions */
   574 static const GstQueryType *
   575 gst_nuv_demux_get_src_query_types (GstPad * pad)
   576 {
   577   static const GstQueryType src_types[] = {
   578     GST_QUERY_POSITION,
   579     GST_QUERY_DURATION,
   580     0
   581   };
   582 
   583   return src_types;
   584 }
   585 
   586 static gboolean
   587 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
   588 {
   589   gboolean res = FALSE;
   590   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   591 
   592   switch (GST_QUERY_TYPE (query)) {
   593     case GST_QUERY_POSITION:
   594       if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
   595 
   596         gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
   597         res = TRUE;
   598       }
   599       break;
   600     case GST_QUERY_DURATION:
   601       {
   602         gint64 duration = 0;
   603         duration = gst_nuv_demux_get_time_duration (nuv);
   604         if (duration == GST_CLOCK_TIME_NONE) {
   605             duration = nuv->priv->duration_average;
   606         }
   607         if (duration != GST_CLOCK_TIME_NONE) {
   608             gst_query_set_duration (query, GST_FORMAT_TIME, duration);
   609             res = TRUE;
   610         }
   611       }
   612       break;
   613     default:
   614       res = FALSE;
   615       break;
   616   }
   617 
   618   gst_object_unref (nuv);
   619 
   620   return res;
   621 }
   622 
   623 static GstPad*
   624 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
   625 {
   626     GstPad *pad = NULL;
   627     pad = gst_pad_new_from_static_template (template, name);
   628     gst_pad_set_caps (pad, caps);
   629     gst_pad_set_active (pad, TRUE);
   630     gst_pad_use_fixed_caps (pad);
   631     gst_element_add_pad (GST_ELEMENT (nuv), pad);
   632 
   633     return pad;
   634 }
   635 
   636 static void
   637 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
   638 {
   639   if (nuv->priv->h->i_video_blocks != 0) {
   640     GstCaps *video_caps = NULL;
   641 
   642     video_caps = gst_caps_new_simple ("video/x-divx",
   643         "divxversion", G_TYPE_INT, 4,
   644         "width", G_TYPE_INT, nuv->priv->h->i_width,
   645         "height", G_TYPE_INT, nuv->priv->h->i_height,
   646         "framerate", GST_TYPE_FRACTION, nuv->priv->h->i_fpsn, nuv->priv->h->i_fpsd,
   647         "format", GST_TYPE_FOURCC, nuv->priv->eh->i_video_fcc,
   648         "pixel-aspect-ratio", GST_TYPE_FRACTION,
   649         (gint) (nuv->priv->h->d_aspect * 1000.0f), 1000, NULL);
   650 
   651     nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
   652     gst_caps_unref (video_caps);
   653   }
   654 
   655   if (nuv->priv->h->i_audio_blocks != 0) {
   656     GstCaps *audio_caps = NULL;
   657 
   658     audio_caps = gst_caps_new_simple ("audio/mpeg",
   659         "rate", G_TYPE_INT, nuv->priv->eh->i_audio_sample_rate,
   660         "format", GST_TYPE_FOURCC, nuv->priv->eh->i_audio_fcc,
   661         "channels", G_TYPE_INT, nuv->priv->eh->i_audio_channels,
   662         "mpegversion", G_TYPE_INT, nuv->priv->eh->i_version, NULL);
   663 
   664     nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
   665     gst_caps_unref (audio_caps);
   666   }
   667 
   668   gst_element_no_more_pads (GST_ELEMENT (nuv));
   669 }
   670 
   671 static GstFlowReturn
   672 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
   673 {
   674   GstFlowReturn ret = GST_FLOW_OK;
   675 
   676   if (nuv->priv->fh != NULL)
   677   {
   678     g_free (nuv->priv->fh);
   679     nuv->priv->fh = NULL;
   680   }
   681 
   682   ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
   683   if (ret != GST_FLOW_OK)
   684     return ret;
   685 
   686   nuv->priv->state = GST_NUV_DEMUX_MOVI;
   687   return ret;
   688 }
   689 
   690 static gboolean
   691 gst_nuv_combine_flow (GstNuvDemux *nuv)
   692 {
   693     GstFlowReturn ret_video = nuv->priv->last_video_return;
   694     GstFlowReturn ret_audio = nuv->priv->last_audio_return;
   695 
   696     if ((ret_video != GST_FLOW_OK) &&
   697         (ret_audio != GST_FLOW_OK))
   698         return FALSE;
   699 
   700     if (GST_FLOW_IS_FATAL (ret_video))
   701         return FALSE;
   702 
   703     if (GST_FLOW_IS_FATAL (ret_audio))
   704         return FALSE;
   705 
   706     return TRUE;
   707 }
   708 
   709 static GstFlowReturn
   710 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
   711 {
   712   GstFlowReturn ret = GST_FLOW_OK;
   713   GstPad *pad = NULL;
   714   guint64 timestamp;
   715   GstBuffer *buf = NULL;
   716   nuv_frame_header *h = NULL;
   717 
   718   h = nuv->priv->fh;
   719 
   720   if (h->i_type == 'R')
   721     goto done;
   722 
   723   if (h->i_length > 0) {
   724 	  ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
   725 	  if ((ret != GST_FLOW_OK) || (buf == NULL))
   726 		  return ret;
   727 
   728       if (h->i_timecode < 0) {
   729           h->i_timecode = h->i_timecode * -1;
   730           nuv->priv->time_diff = h->i_timecode;
   731       }
   732       else
   733           h->i_timecode += nuv->priv->time_diff;
   734 
   735       timestamp = h->i_timecode * GST_MSECOND;
   736 
   737       //GST_BUFFER_SIZE (buf) = h->i_length;
   738       GST_BUFFER_TIMESTAMP (buf) = timestamp;
   739   }
   740   else {
   741     goto done;
   742   }
   743 
   744 
   745   switch (h->i_type) {
   746     case 'V':
   747     {
   748       pad = nuv->priv->src_video_pad;
   749 
   750       if (nuv->priv->new_video_segment) {
   751 
   752         /* send new segment event*/
   753         gst_pad_push_event (nuv->priv->src_video_pad,
   754           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   755                                      GST_CLOCK_TIME_NONE, 0));
   756 
   757         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   758             nuv->priv->time_start = timestamp;
   759         }
   760         nuv->priv->new_video_segment = FALSE;
   761       }
   762 
   763       break;
   764     }
   765     case 'A':
   766     {
   767       pad = nuv->priv->src_audio_pad;
   768 
   769       if (nuv->priv->new_audio_segment) {
   770         /* send new segment event*/
   771         gst_pad_push_event (nuv->priv->src_audio_pad,
   772           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   773                                      GST_CLOCK_TIME_NONE, 0));
   774 
   775         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   776             nuv->priv->time_start = timestamp;
   777         }
   778         nuv->priv->new_audio_segment = FALSE;
   779       }
   780 
   781       break;
   782     }
   783     case 'S':
   784     {
   785       switch (h->i_compression) {
   786         case 'V':
   787           GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h->i_timecode);
   788           gst_pad_push_event (nuv->priv->src_video_pad,
   789               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h->i_timecode * GST_MSECOND,
   790               		GST_CLOCK_TIME_NONE, 0));
   791           break;
   792         case 'A':
   793           GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h->i_timecode);
   794           gst_pad_push_event (nuv->priv->src_audio_pad,
   795               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   796               		GST_CLOCK_TIME_NONE, 0));
   797           break;
   798         default:
   799           break;
   800       }
   801       if (buf != NULL) {
   802         gst_buffer_unref (buf);
   803         buf = NULL;
   804       }
   805 
   806       goto done;
   807     }
   808     default:
   809       if (buf != NULL) {
   810         gst_buffer_unref (buf);
   811         buf = NULL;
   812       }
   813       break;
   814   }
   815 
   816   if ((buf != NULL) && (pad != NULL)) {
   817       /* update average time */
   818       nuv->priv->streamer_offset += h->i_length;
   819       gst_segment_set_last_stop (&nuv->priv->segment, GST_FORMAT_TIME, timestamp);
   820       gst_nuv_demux_update_duration (nuv, timestamp);
   821 
   822       /* pushing the buffer */
   823       gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
   824       ret = gst_pad_push (pad, buf);
   825 
   826       if (ret != GST_FLOW_OK) {
   827         GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
   828 
   829         if (pad == nuv->priv->src_video_pad) {
   830             nuv->priv->last_video_return = ret;
   831         }
   832         else if (pad == nuv->priv->src_audio_pad) {
   833             nuv->priv->last_audio_return = ret;
   834         }
   835 
   836         /* verify anothers flow if is necessary stop task */
   837         if (gst_nuv_combine_flow (nuv) != FALSE) {
   838             ret = GST_FLOW_OK;
   839         }
   840 
   841         //GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
   842       }
   843   } else if (buf != NULL) {
   844       gst_buffer_unref (buf);
   845       buf = NULL;
   846   }
   847 
   848 done:
   849   nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   850   g_free (nuv->priv->fh);
   851   nuv->priv->fh = NULL;
   852   return ret;
   853 }
   854 
   855 static GstFlowReturn
   856 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
   857 {
   858   GstFlowReturn ret = GST_FLOW_OK;
   859 
   860   /* ffmpeg extra data */
   861   ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
   862       &nuv->priv->mpeg_buffer);
   863   if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
   864     return ret;
   865   }
   866 
   867   GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
   868   nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   869   return ret;
   870 }
   871 
   872 static GstFlowReturn
   873 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
   874 {
   875   GstFlowReturn ret = GST_FLOW_OK;
   876 
   877   /* Load 'D' */
   878   nuv_frame_header *h;
   879 
   880   ret = gst_nuv_demux_frame_header_load (nuv, &h);
   881   if (ret != GST_FLOW_OK)
   882     return ret;
   883 
   884   if (h->i_type != 'D') {
   885     g_free (h);
   886     GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
   887     return GST_FLOW_NOT_SUPPORTED;
   888   }
   889 
   890   if (h->i_length > 0) {
   891     if (h->i_compression == 'F') {
   892       nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
   893     } else {
   894       g_free (h);
   895       GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
   896       return GST_FLOW_NOT_SUPPORTED;
   897     }
   898   } else {
   899     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   900   }
   901 
   902   g_free (h);
   903   h = NULL;
   904   return ret;
   905 }
   906 
   907 static GstFlowReturn
   908 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
   909 {
   910   GstFlowReturn ret = GST_FLOW_OK;
   911 
   912   ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
   913   if (ret != GST_FLOW_OK)
   914     return ret;
   915 
   916   gst_nuv_demux_create_pads (nuv);
   917   nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   918   return ret;
   919 }
   920 
   921 static GstFlowReturn
   922 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
   923 {
   924   GstBuffer *buf = NULL;
   925   GstFlowReturn res = GST_FLOW_OK;
   926 
   927   res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
   928   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   929     if (buf != NULL) {
   930       gst_buffer_unref (buf);
   931     }
   932     return res;
   933   }
   934 
   935   if (buf->data[0] == 'X') {
   936     gst_buffer_unref (buf);
   937     buf = NULL;
   938     nuv_frame_header *h = NULL;
   939 
   940     res = gst_nuv_demux_frame_header_load (nuv, &h);
   941     if (res != GST_FLOW_OK)
   942       return res;
   943 
   944     if (h->i_length != 512) {
   945       g_free (h);
   946       return GST_FLOW_ERROR;
   947     }
   948     g_free (h);
   949     h = NULL;
   950     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
   951   } else {
   952     nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
   953     g_object_unref (buf);
   954     GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
   955         (_("incomplete NUV support")), ("incomplete NUV support"));
   956     return GST_FLOW_ERROR;
   957   }
   958   return res;
   959 }
   960 
   961 static GstFlowReturn
   962 gst_nuv_demux_play (GstPad * pad)
   963 {
   964   GstFlowReturn res = GST_FLOW_OK;
   965   GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
   966 
   967   switch (nuv->priv->state) {
   968     case GST_NUV_DEMUX_START:
   969       res = gst_nuv_demux_stream_file_header (nuv);
   970       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   971         goto pause;
   972       }
   973       break;
   974 
   975     case GST_NUV_DEMUX_HEADER_DATA:
   976       res = gst_nuv_demux_stream_header_data (nuv);
   977       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   978         goto pause;
   979       }
   980       break;
   981 
   982     case GST_NUV_DEMUX_EXTRA_DATA:
   983       res = gst_nuv_demux_stream_extra_data (nuv);
   984       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   985         goto pause;
   986       }
   987       break;
   988 
   989     case GST_NUV_DEMUX_MPEG_DATA:
   990       res = gst_nuv_demux_stream_mpeg_data (nuv);
   991       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   992         goto pause;
   993       }
   994       break;
   995 
   996     case GST_NUV_DEMUX_EXTEND_HEADER:
   997       res = gst_nuv_demux_stream_extend_header (nuv);
   998       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   999         goto pause;
  1000       }
  1001       break;
  1002 
  1003     case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
  1004       res = gst_nuv_demux_stream_extend_header_data (nuv);
  1005       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1006         goto pause;
  1007       }
  1008       break;
  1009 
  1010     case GST_NUV_DEMUX_FRAME_HEADER:
  1011       res = gst_nuv_demux_read_head_frame (nuv);
  1012       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1013         goto pause;
  1014       }
  1015       break;
  1016 
  1017     case GST_NUV_DEMUX_MOVI:
  1018       res = gst_nuv_demux_stream_data (nuv);
  1019       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1020         goto pause;
  1021       }
  1022       break;
  1023 
  1024     case GST_NUV_DEMUX_INVALID_DATA:
  1025       goto pause;
  1026       break;
  1027     default:
  1028       g_assert_not_reached ();
  1029   }
  1030 
  1031   GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->priv->state, gst_flow_get_name (res));
  1032 
  1033   return GST_FLOW_OK;
  1034 
  1035 pause:
  1036   GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
  1037   gst_pad_pause_task (nuv->priv->sinkpad);
  1038   if (GST_FLOW_IS_FATAL (res)) {
  1039     GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
  1040         (_("Internal data stream error.")),
  1041         ("streaming stopped, reason %s", gst_flow_get_name (res)));
  1042 
  1043     gst_nuv_demux_send_eos (nuv);
  1044   }
  1045   return res;
  1046 }
  1047 
  1048 static void
  1049 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
  1050 {
  1051   gst_element_post_message (GST_ELEMENT (nuv),
  1052       gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
  1053 
  1054   if (nuv->priv->src_video_pad)
  1055     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
  1056   if (nuv->priv->src_audio_pad)
  1057     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
  1058 }
  1059 
  1060 static GstFlowReturn
  1061 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
  1062     GstBuffer ** buffer)
  1063 {
  1064   GstFlowReturn ret = GST_FLOW_OK;
  1065 
  1066   if (size == 0) {
  1067     return ret;
  1068   }
  1069 
  1070   if (nuv->priv->mode == NUV_PULL_MODE) {
  1071     ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
  1072     if (ret == GST_FLOW_OK) {
  1073         if (move) {
  1074 		    nuv->priv->offset += size;
  1075 		}
  1076       /* got eos */
  1077     } else if (ret == GST_FLOW_UNEXPECTED) {
  1078       if (buffer != NULL)
  1079           gst_buffer_unref (buffer);
  1080 
  1081       gst_nuv_demux_send_eos (nuv);
  1082       return GST_FLOW_WRONG_STATE;
  1083     }
  1084   } else {
  1085     if (gst_adapter_available (nuv->priv->adapter) < size)
  1086       return GST_FLOW_ERROR_NO_DATA;
  1087 
  1088     if (move) {
  1089       guint8 *data = NULL;
  1090       data = (guint8 *) gst_adapter_take (nuv->priv->adapter, size);
  1091       *buffer = gst_buffer_new ();
  1092       GST_BUFFER_SIZE (buffer) = size;
  1093       GST_BUFFER_MALLOCDATA (buffer) = g_malloc (size);
  1094       GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
  1095       g_free (data);
  1096     } else {
  1097       guint8 *data = NULL;
  1098       data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
  1099       *buffer = gst_buffer_new ();
  1100       GST_BUFFER_SIZE (buffer) = size;
  1101       GST_BUFFER_MALLOCDATA (buffer) = g_malloc (size);
  1102       GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
  1103     }
  1104   }
  1105   return ret;
  1106 }
  1107 
  1108 static gboolean
  1109 gst_nuv_demux_sink_activate (GstPad * sinkpad)
  1110 {
  1111   gboolean res = TRUE;
  1112   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1113 
  1114   if (gst_pad_check_pull_range (sinkpad)) {
  1115     res = gst_pad_activate_pull (sinkpad, TRUE);
  1116   } else {
  1117     res = gst_pad_activate_push (sinkpad, TRUE);
  1118   }
  1119   g_object_unref (nuv);
  1120   return res;
  1121 }
  1122 
  1123 static gboolean
  1124 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
  1125 {
  1126   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1127 
  1128   if (active) {
  1129     GST_DEBUG_OBJECT (nuv, "activating pull function");
  1130     nuv->priv->mode = NUV_PULL_MODE;
  1131     if (nuv->priv->adapter) {
  1132       gst_adapter_clear (nuv->priv->adapter);
  1133       g_object_unref (nuv->priv->adapter);
  1134       nuv->priv->adapter = NULL;
  1135     }
  1136     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
  1137   } else {
  1138     GST_DEBUG_OBJECT (nuv, "deactivating pull function");
  1139     gst_pad_stop_task (sinkpad);
  1140   }
  1141   gst_object_unref (nuv);
  1142 
  1143   return TRUE;
  1144 }
  1145 
  1146 static gboolean
  1147 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
  1148 {
  1149   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1150 
  1151 
  1152   if (active) {
  1153     GST_DEBUG_OBJECT (nuv, "activating push/chain function");
  1154   } else {
  1155     GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
  1156   }
  1157 
  1158   gst_object_unref (nuv);
  1159 
  1160   return TRUE;
  1161 }
  1162 
  1163 
  1164 static gboolean
  1165 gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event)
  1166 {
  1167   gboolean res;
  1168   GstNuvDemux *nuv;
  1169 
  1170   nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1171 
  1172   switch (GST_EVENT_TYPE (event)) {
  1173     case GST_EVENT_SEEK:
  1174     {
  1175       gdouble rate;
  1176       GstFormat format;
  1177       GstSeekFlags flags;
  1178       GstSeekType cur_type;
  1179       gint64 cur;
  1180       GstSeekType stop_type;
  1181       gint64 stop;
  1182 
  1183       gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur, &stop_type, &stop);
  1184 
  1185       GST_DEBUG_OBJECT (nuv, "got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop);
  1186 
  1187       break;
  1188     }
  1189     case GST_EVENT_NEWSEGMENT:
  1190     {
  1191       gint64 start, stop, time;
  1192       gdouble rate, arate;
  1193       GstFormat format;
  1194       gboolean update;
  1195 
  1196       GST_DEBUG_OBJECT (nuv, "got a new segment event");
  1197       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
  1198           &start, &stop, &time);
  1199 
  1200       GST_DEBUG_OBJECT (nuv, "got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop);
  1201       nuv->priv->duration_bytes = stop - start;
  1202       gst_event_unref (event);
  1203       res = TRUE;
  1204       break;
  1205     }
  1206     case GST_EVENT_QOS:
  1207     {
  1208 /*
  1209       gdouble proportion;
  1210       GstClockTimeDiff diff;
  1211       GstClockTime timestamp;
  1212 
  1213       gst_event_parse_qos (event, &proportion, &diff, &timestamp);
  1214       if (diff > 0)
  1215         nuv->time_qos = timecode + diff;
  1216       else
  1217         nuv->time_qos = -1;
  1218 */
  1219 
  1220       break;
  1221     }
  1222 
  1223     case GST_EVENT_EOS:
  1224         g_debug ("EEEEEEEEEEEEEEEEEOOOOOOOOOOOOOOOOOOOOOOSSSSSSSSSS");
  1225     default:
  1226       res = gst_pad_event_default (pad, event);
  1227       break;
  1228   }
  1229 
  1230   gst_object_unref (nuv);
  1231   return res;
  1232 }
  1233 
  1234 static GstFlowReturn
  1235 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
  1236 {
  1237   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1238 
  1239   GST_DEBUG_OBJECT (nuv, " gst_nuv_demux_chain function");
  1240   gst_adapter_push (nuv->priv->adapter, buf);
  1241 
  1242   gst_object_unref (nuv);
  1243 
  1244   return gst_nuv_demux_play (pad);
  1245 }
  1246 
  1247 static void
  1248 gst_nuv_demux_loop (GstPad * pad)
  1249 {
  1250   gst_nuv_demux_play (pad);
  1251 }
  1252 
  1253 static void
  1254 gst_nuv_demux_reset (GstNuvDemux * nuv)
  1255 {
  1256   nuv->priv->state = GST_NUV_DEMUX_START;
  1257   nuv->priv->mode = 0;
  1258   nuv->priv->offset = 0;
  1259   nuv->priv->streamer_offset = 0;
  1260   nuv->priv->time_start = 0;
  1261   nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
  1262   nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
  1263   nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
  1264   nuv->priv->duration_average = GST_CLOCK_TIME_NONE;
  1265   gst_segment_init (&nuv->priv->segment, GST_FORMAT_TIME);
  1266 
  1267   if (nuv->priv->adapter != NULL)
  1268     gst_adapter_clear (nuv->priv->adapter);
  1269 
  1270   if (nuv->priv->mpeg_buffer != NULL) {
  1271     gst_buffer_unref (nuv->priv->mpeg_buffer);
  1272     nuv->priv->mpeg_buffer = NULL;
  1273   }
  1274 
  1275   g_free (nuv->priv->h);
  1276   nuv->priv->h = NULL;
  1277 
  1278   g_free (nuv->priv->eh);
  1279   nuv->priv->eh = NULL;
  1280 
  1281   g_free (nuv->priv->fh);
  1282   nuv->priv->fh = NULL;
  1283 }
  1284 
  1285 static void
  1286 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
  1287 {
  1288   if (nuv->priv->src_video_pad) {
  1289     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
  1290     nuv->priv->src_video_pad = NULL;
  1291   }
  1292 
  1293   if (nuv->priv->src_audio_pad) {
  1294     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
  1295     nuv->priv->src_audio_pad = NULL;
  1296   }
  1297 }
  1298 
  1299 static GstStateChangeReturn
  1300 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
  1301 {
  1302   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  1303 
  1304   switch (transition) {
  1305     case GST_STATE_CHANGE_NULL_TO_READY:
  1306       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1307       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1308       break;
  1309     default:
  1310       break;
  1311   }
  1312 
  1313   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1314   if (ret == GST_STATE_CHANGE_FAILURE) {
  1315     goto done;
  1316   }
  1317 
  1318   switch (transition) {
  1319     case GST_STATE_CHANGE_READY_TO_NULL:
  1320       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1321       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1322       break;
  1323     default:
  1324       break;
  1325   }
  1326 
  1327 done:
  1328   return ret;
  1329 }
  1330 
  1331 static void
  1332 gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp)
  1333 {
  1334     guint64 interval = 0;
  1335 
  1336     if (gst_nuv_demux_get_time_duration (nuv) != GST_CLOCK_TIME_NONE)
  1337         return;
  1338 
  1339     interval  = current_timestamp - nuv->priv->last_update;
  1340 
  1341     if (interval > (10 * GST_SECOND)) {
  1342         GstMessage* msg = NULL;
  1343         gint64 average = 0;
  1344         gint64 duration_bytes = gst_nuv_demux_get_bytes_duration (nuv);
  1345 
  1346         if (duration_bytes == GST_CLOCK_TIME_NONE)
  1347             return;
  1348 
  1349         if (current_timestamp > 0) {
  1350             interval = gst_util_uint64_scale (1, current_timestamp - nuv->priv->time_start, GST_SECOND);
  1351             if (interval > 0) {
  1352                 average = gst_util_uint64_scale (1 , nuv->priv->streamer_offset, interval);
  1353             }
  1354             if (average > 0) {
  1355                 nuv->priv->duration_average = gst_util_uint64_scale (GST_SECOND, duration_bytes, average);
  1356             }
  1357             nuv->priv->last_update = current_timestamp;
  1358             msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_average);
  1359             gst_element_post_message (GST_ELEMENT (nuv), msg);
  1360             GST_DEBUG_OBJECT (nuv, "New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
  1361         }
  1362     }
  1363 }
  1364 
  1365 static gint64
  1366 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv)
  1367 {
  1368     if (nuv->priv->duration_bytes == GST_CLOCK_TIME_NONE) {
  1369         GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
  1370         GstQuery *query = gst_query_new_duration (GST_FORMAT_BYTES);
  1371         if (gst_pad_query (peer, query)) {
  1372             gint64 duration;
  1373 
  1374             gst_query_parse_duration (query, NULL, &duration);
  1375             nuv->priv->duration_bytes = duration;
  1376         }
  1377         gst_object_unref (peer);
  1378         gst_query_unref (query);
  1379     }
  1380     return nuv->priv->duration_bytes;
  1381 }
  1382 
  1383 static gint64
  1384 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv)
  1385 {
  1386     if (nuv->priv->duration_time == GST_CLOCK_TIME_NONE) {
  1387         GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
  1388         GstQuery *query = gst_query_new_duration (GST_FORMAT_TIME);
  1389         if (gst_pad_query (peer, query)) {
  1390             gint64 duration;
  1391             gst_query_parse_duration (query, NULL, &duration);
  1392             nuv->priv->duration_time = duration;
  1393         }
  1394         gst_object_unref (peer);
  1395         gst_query_unref (query);
  1396     }
  1397     return nuv->priv->duration_time;
  1398 }
  1399 
  1400 
  1401 static gboolean
  1402 plugin_init (GstPlugin * plugin)
  1403 {
  1404 #ifdef ENABLE_NLS
  1405   setlocale (LC_ALL, "");
  1406   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  1407 #endif /* ENABLE_NLS */
  1408 
  1409   if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
  1410           GST_TYPE_NUV_DEMUX)) {
  1411     return FALSE;
  1412   }
  1413   return TRUE;
  1414 }
  1415 
  1416 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1417     GST_VERSION_MINOR,
  1418     "nuvdemux",
  1419     "Demuxes and muxes audio and video",
  1420      plugin_init, VERSION, "LGPL", "NuvDemux", "")
  1421