gst-plugins-nuvdemux/nuvdemux/gstnuvdemux.c
author rosfran
Thu Mar 22 18:13:13 2007 +0000 (2007-03-22)
branchtrunk
changeset 429 0ba50b995f75
parent 422 fc0a5721cd04
child 430 4476d11d6b9a
permissions -rw-r--r--
[svn r434] Fixes to the program info; prepare to the new Program Info change notification function calls.
     1 /* GStreamer
     2  * Copyright (C) <2006> Renato Araujo Oliveira Filho <renato.filho@indt.org.br>
     3  *                      Rosfran Borges <rosfran.borges@indt.org.br>
     4  *
     5  * This library is free software; you can redistribute it and/or
     6  * modify it under the terms of the GNU Library General Public
     7  * License as published by the Free Software Foundation; either
     8  * version 2 of the License, or (at your option) any later version.
     9  *
    10  * This library is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  * Library General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU Library General Public
    16  * License along with this library; if not, write to the
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    18  * Boston, MA 02111-1307, USA.
    19  */
    20 /* Element-Checklist-Version: 5 */
    21 
    22 /**
    23  * SECTION:element-nuvdemux
    24  *
    25  * <refsect2>
    26  * <para>
    27  * Demuxes an .nuv file into raw or compressed audio and/or video streams.
    28  * </para>
    29  * <para>
    30  * This element currently only supports pull-based scheduling.
    31  * </para>
    32  * <title>Example launch line</title>
    33  * <para>
    34  * <programlisting>
    35  * gst-launch filesrc test.nuv ! nuvdemux name=demux  demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink   demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
    36  * </programlisting>
    37  * Play (parse and decode) an .nuv file and try to output it to
    38  * an automatically detected soundcard and videosink. If the NUV file contains
    39  * compressed audio or video data, this will only work if you have the
    40  * right decoder elements/plugins installed.
    41  * </para>
    42  * </refsect2>
    43  *
    44  */
    45 
    46 #ifdef HAVE_CONFIG_H
    47 #include "config.h"
    48 #endif
    49 
    50 #include <gst/gst.h>
    51 #include <gst/gsterror.h>
    52 #include <gst/gstplugin.h>
    53 #include <string.h>
    54 #include <math.h>
    55 
    56 #include "glib/gi18n.h"
    57 #include "gstnuvdemux.h"
    58 
    59 #define GST_NUV_DEMUX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NUV_DEMUX, GstNuvDemuxPrivate))
    60 
    61 GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug);
    62 #define GST_CAT_DEFAULT nuvdemux_debug
    63 #define GST_FLOW_ERROR_NO_DATA  -101
    64 
    65 enum
    66 {
    67    NUV_PUSH_MODE = 0,
    68    NUV_PULL_MODE
    69 };
    70 
    71 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
    72 
    73 static const GstElementDetails gst_nuv_demux_details =
    74 GST_ELEMENT_DETAILS ("Nuv demuxer",
    75     "Codec/Demuxer",
    76     "Demultiplex a .nuv file into audio and video",
    77     "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
    78     "Rosfran Borges <rosfran.borges@indt.org.br>");
    79 
    80 
    81 /* file header */
    82 typedef struct
    83 {
    84     gchar id[12];       /* "NuppelVideo\0" or "MythTVVideo\0" */
    85     gchar version[5];    /* "x.xx\0" */
    86 
    87     gint  i_width;
    88     gint  i_height;
    89     gint  i_width_desired;
    90     gint  i_height_desired;
    91 
    92     gchar i_mode;            /* P progressive, I interlaced */
    93 
    94     gdouble  d_aspect;       /* 1.0 squared pixel */
    95     gdouble  d_fps;
    96     //fps num/denom
    97     gint     i_fpsn;
    98     gint     i_fpsd;
    99 
   100     gint     i_video_blocks; /* 0 no video, -1 unknown */
   101     gint     i_audio_blocks;
   102     gint     i_text_blocks;
   103 
   104     gint     i_keyframe_distance;
   105 
   106 } nuv_header;
   107 
   108 /* frame header */
   109 typedef struct
   110 {
   111     gchar i_type;        /* A: audio, V: video, S: sync; T: test
   112                            R: Seekpoint (string:RTjjjjjjjj)
   113                            D: Extra data for codec */
   114     gchar i_compression; /* V: 0 uncompressed
   115                               1 RTJpeg
   116                               2 RTJpeg+lzo
   117                               N black frame
   118                               L copy last
   119                            A: 0 uncompressed (44100 1-bits, 2ch)
   120                               1 lzo
   121                               2 layer 2
   122                               3 layer 3
   123                               F flac
   124                               S shorten
   125                               N null frame loudless
   126                               L copy last
   127                             S: B audio and vdeo sync point
   128                                A audio sync info (timecode == effective
   129                                     dsp frequency*100)
   130                                V next video sync (timecode == next video
   131                                     frame num)
   132                                S audio,video,text correlation */
   133     gchar i_keyframe;    /* 0 keyframe, else no no key frame */
   134     guint8 i_filters;  /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
   135                            0x02: gauss 5 pixel (8,1,1,1,1)/12
   136                            0x04: cartoon filter */
   137 
   138     gint32 i_timecode;     /* ms */
   139 
   140     gint i_length;       /* V,A,T: length of following data
   141                            S: length of packet correl */
   142 } nuv_frame_header;
   143 
   144 /* FIXME Not sure of this one */
   145 typedef struct
   146 {
   147     gint             i_version;
   148     guint32		     i_video_fcc;
   149 
   150     guint32		     i_audio_fcc;
   151     gint             i_audio_sample_rate;
   152     gint             i_audio_bits_per_sample;
   153     gint             i_audio_channels;
   154     gint             i_audio_compression_ratio;
   155     gint             i_audio_quality;
   156     gint             i_rtjpeg_quality;
   157     gint             i_rtjpeg_luma_filter;
   158     gint             i_rtjpeg_chroma_filter;
   159     gint             i_lavc_bitrate;
   160     gint             i_lavc_qmin;
   161     gint             i_lavc_qmax;
   162     gint             i_lavc_maxqdiff;
   163     gint64         	 i_seekable_offset;
   164     gint64           i_keyframe_adjust_offset;
   165 
   166 } nuv_extended_header;
   167 
   168 typedef enum {
   169   GST_NUV_DEMUX_START,
   170   GST_NUV_DEMUX_HEADER_DATA,
   171   GST_NUV_DEMUX_EXTRA_DATA,
   172   GST_NUV_DEMUX_MPEG_DATA,
   173   GST_NUV_DEMUX_EXTEND_HEADER,
   174   GST_NUV_DEMUX_EXTEND_HEADER_DATA,
   175   GST_NUV_DEMUX_FRAME_HEADER,
   176   GST_NUV_DEMUX_MOVI,
   177   GST_NUV_DEMUX_INVALID_DATA
   178 } GstNuvDemuxState;
   179 
   180 struct _GstNuvDemuxPrivate {
   181   /* used for indicate the mode */
   182   guint         mode;
   183 
   184   /* used on push mode */
   185   GstAdapter    *adapter;
   186 
   187   /* pads */
   188   GstPad        *sinkpad;
   189   GstPad        *src_video_pad;
   190   GstPad        *src_audio_pad;
   191 
   192   /* Flow control */
   193   GstFlowReturn last_video_return;
   194   GstFlowReturn last_audio_return;
   195   gboolean      more_data;
   196 
   197   /* NUV decoding state */
   198   GstNuvDemuxState  state;
   199   GstSegment        segment;
   200   guint64           last_update;
   201   guint64           offset;
   202   guint64           streamer_offset;
   203 
   204   /* duration information */
   205   gint64            duration_bytes;
   206   gint64            duration_time;
   207   gint64            duration_average;
   208 
   209   /* segment control info */
   210   gboolean          new_audio_segment;
   211   gboolean          new_video_segment;
   212 
   213   /* Mpeg ExtraData */
   214   guint64       mpeg_data_size;
   215   GstBuffer     *mpeg_buffer;
   216 
   217   /* Headers */
   218   nuv_header h;
   219   nuv_extended_header eh;
   220   nuv_frame_header fh;
   221 
   222   /* anothers info */
   223   gint64            time_start;
   224   gint64            time_diff;
   225   gint64            time_qos;
   226   guint64           last_frame_time;
   227 };
   228 
   229 
   230 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
   231     GST_PAD_SINK,
   232     GST_PAD_ALWAYS,
   233     GST_STATIC_CAPS ("video/x-nuv"));
   234 
   235 static GstStaticPadTemplate audio_src_template =
   236 GST_STATIC_PAD_TEMPLATE ("audio_src",
   237     GST_PAD_SRC,
   238     GST_PAD_SOMETIMES,
   239     GST_STATIC_CAPS_ANY);
   240 
   241 static GstStaticPadTemplate video_src_template =
   242 GST_STATIC_PAD_TEMPLATE ("video_src",
   243     GST_PAD_SRC,
   244     GST_PAD_SOMETIMES,
   245     GST_STATIC_CAPS_ANY);
   246 
   247 static void gst_nuv_demux_dispose (GObject * object);
   248 static void gst_nuv_demux_finalize (GObject * object);
   249 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
   250     GstStateChange transition);
   251 static void gst_nuv_demux_loop (GstPad * pad);
   252 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
   253 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
   254 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
   255     gboolean active);
   256 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
   257     gboolean active);
   258 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
   259 static gboolean gst_nuv_demux_sink_event    (GstPad *pad, GstEvent *event);
   260 static gboolean gst_nuv_demux_srcpad_event  (GstPad * pad, GstEvent * event);
   261 
   262 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
   263     gboolean move, GstBuffer ** buffer);
   264 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
   265 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
   266 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
   267 static void gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp);
   268 static gint64 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv);
   269 static gint64 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv);
   270 
   271 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
   272 GstBuffer * gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes);
   273 #endif
   274 
   275 
   276 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
   277 
   278 /******************************************************************************
   279  * Utils function
   280  ******************************************************************************/
   281 #if G_BYTE_ORDER == G_BIG_ENDIAN
   282 static inline gdouble
   283 _gdouble_swap_le_be (gdouble * d)
   284 {
   285   union
   286   {
   287     guint64 i;
   288     gdouble d;
   289   } u;
   290 
   291   u.d = *d;
   292   u.i = GUINT64_SWAP_LE_BE (u.i);
   293   return u.d;
   294 }
   295 
   296 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
   297 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
   298 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
   299 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
   300 
   301 static void
   302 double2fraction (double in, int *num, int *denom)
   303 {
   304     if (in == 29.97) {
   305         *num = 30000;
   306         *denom = 1001;
   307     } else if (in == 23.976) {
   308         *num = 24000;
   309         *denom = 1001;
   310     } else {
   311         *denom = 1;
   312         while (in - floor(in) >= 0.1) {
   313             *denom *= 10;
   314             in *= 10.0;
   315         }
   316         *num = (int)floor(in);
   317     }
   318 }
   319 
   320 /* GObject Functions */
   321 
   322 static void
   323 gst_nuv_demux_base_init (gpointer klass)
   324 {
   325   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
   326 
   327   gst_element_class_add_pad_template (element_class,
   328       gst_static_pad_template_get (&audio_src_template));
   329 
   330   gst_element_class_add_pad_template (element_class,
   331       gst_static_pad_template_get (&video_src_template));
   332 
   333   gst_element_class_add_pad_template (element_class,
   334       gst_static_pad_template_get (&sink_template));
   335   gst_element_class_set_details (element_class, &gst_nuv_demux_details);
   336 }
   337 
   338 static void
   339 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
   340 {
   341   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   342   GObjectClass *gobject_class = (GObjectClass *) klass;
   343 
   344   GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
   345       0, "Demuxer for NUV streams");
   346 
   347   parent_class = g_type_class_peek_parent (klass);
   348 
   349   gobject_class->dispose = gst_nuv_demux_dispose;
   350   gobject_class->finalize = gst_nuv_demux_finalize;
   351   gstelement_class->change_state = gst_nuv_demux_change_state;
   352 
   353   g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate));
   354 }
   355 
   356 static void
   357 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
   358 {
   359   nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv);
   360   nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
   361 
   362   /* creating adapter */
   363   nuv->priv->mode = NUV_PUSH_MODE;
   364   nuv->priv->adapter = gst_adapter_new ();
   365 
   366   g_debug ("Adapter %p", nuv->priv->adapter);
   367 
   368   nuv->priv->new_audio_segment = TRUE;
   369   nuv->priv->new_video_segment = TRUE;
   370 
   371   gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate);
   372   gst_pad_set_activatepull_function (nuv->priv->sinkpad,
   373       gst_nuv_demux_sink_activate_pull);
   374   gst_pad_set_activatepush_function (nuv->priv->sinkpad,
   375       gst_nuv_demux_sink_activate_push);
   376   gst_pad_set_chain_function (nuv->priv->sinkpad,
   377       GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
   378   gst_pad_set_event_function (nuv->priv->sinkpad,
   379       gst_nuv_demux_sink_event);
   380 
   381   gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad);
   382 
   383 }
   384 
   385 static void
   386 gst_nuv_demux_dispose (GObject * object)
   387 {
   388   GstNuvDemux *nuv = GST_NUV_DEMUX (object);
   389 
   390   g_debug ("NUV DISPOSE");
   391 
   392   if (nuv->priv->mpeg_buffer != NULL) {
   393     gst_buffer_unref (nuv->priv->mpeg_buffer);
   394   }
   395 
   396   gst_nuv_demux_reset (GST_NUV_DEMUX (object));
   397   gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (object));
   398 
   399   if (nuv->priv->adapter != NULL) {
   400     gst_object_unref (nuv->priv->adapter);
   401   }
   402 }
   403 
   404 static void
   405 gst_nuv_demux_finalize (GObject * object)
   406 {
   407   G_OBJECT_CLASS (parent_class)->finalize (object);
   408 }
   409 
   410 
   411 /* HeaderLoad:
   412  */
   413 static GstFlowReturn
   414 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header *h)
   415 {
   416   GstBuffer *buffer = NULL;
   417   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
   418 
   419   if ((res != GST_FLOW_OK) || (buffer == NULL)) {
   420       goto done;
   421   }
   422 
   423   memcpy (h->id, buffer->data, 12);
   424   memcpy (h->version, buffer->data + 12, 5);
   425   h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
   426   h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
   427   h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
   428   h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
   429   h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
   430   h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
   431   h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
   432   /* get the num and denom values from fps */
   433   double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
   434   h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
   435   h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
   436   h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
   437   h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
   438 
   439   GST_DEBUG_OBJECT (nuv,
   440       "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
   441       h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
   442       h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
   443       h->i_keyframe_distance);
   444 
   445 done:
   446   if (buffer != NULL) {
   447      gst_buffer_unref (buffer);
   448      buffer = NULL;
   449   }
   450   return res;
   451 }
   452 
   453 static GstFlowReturn
   454 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
   455 {
   456   GstFlowReturn res;
   457 
   458   res = gst_nuv_demux_header_load (nuv, &nuv->priv->h);
   459   if (res == GST_FLOW_OK)
   460     nuv->priv->state = GST_NUV_DEMUX_EXTRA_DATA;
   461   return res;
   462 }
   463 
   464 /*
   465  * Read NUV file tag
   466  */
   467 static GstFlowReturn
   468 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
   469 {
   470   GstFlowReturn res = GST_FLOW_OK;
   471   GstBuffer *file_header = NULL;
   472 
   473   res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
   474   if (res == GST_FLOW_OK) {
   475     if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
   476         strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
   477       nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
   478     } else {
   479       GST_DEBUG_OBJECT (nuv, "error parsing file header");
   480       nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
   481       res = GST_FLOW_ERROR;
   482     }
   483   }
   484 
   485   if (file_header != NULL) {
   486     gst_buffer_unref (file_header);
   487     file_header = NULL;
   488   }
   489   return res;
   490 }
   491 
   492 /* FrameHeaderLoad:
   493  */
   494 static GstFlowReturn
   495 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header *h)
   496 {
   497   unsigned char *data;
   498   GstBuffer *buf = NULL;
   499 
   500   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
   501 
   502   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   503       goto done;
   504   }
   505 
   506   data = buf->data;
   507 
   508   h->i_type = GPOINTER_TO_INT (data[0]);
   509   h->i_compression = GPOINTER_TO_INT (data[1]);
   510   h->i_keyframe = GPOINTER_TO_INT (data[2]);
   511   h->i_filters = GPOINTER_TO_INT (data[3]);
   512   h->i_timecode = GST_READ_UINT32_LE (&data[4]);
   513   h->i_length = GST_READ_UINT32_LE (&data[8]);
   514 
   515   GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
   516       h->i_type,
   517       h->i_compression ? h->i_compression : ' ',
   518       h->i_keyframe ? h->i_keyframe : ' ',
   519       h->i_filters, h->i_timecode, h->i_length);
   520 
   521 done:  
   522   if (buf != NULL) {
   523       gst_buffer_unref (buf);
   524       buf = NULL;
   525   }
   526   return res;
   527 }
   528 
   529 static GstFlowReturn
   530 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
   531     nuv_extended_header * h)
   532 {
   533   unsigned char *data;
   534   GstBuffer *buff = NULL;
   535 
   536   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
   537 
   538   if ((res != GST_FLOW_OK) || (buff == NULL)) {
   539       goto done;
   540   }
   541 
   542   data = buff->data;
   543   h->i_version = GST_READ_UINT32_LE (&data[0]);
   544   h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
   545   h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
   546   h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
   547   h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
   548   h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
   549   h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
   550   h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
   551   h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
   552   h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
   553   h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
   554   h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
   555   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
   556   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
   557   h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
   558   h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
   559   h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
   560 
   561   GST_DEBUG_OBJECT (nuv,
   562       "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
   563       "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
   564       h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
   565       h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
   566       h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
   567       h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
   568       h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
   569       h->i_keyframe_adjust_offset);
   570 
   571 done:
   572   if (buff != NULL) {
   573     gst_buffer_unref (buff);
   574     buff = NULL;
   575   }
   576   return res;
   577 }
   578 
   579 
   580 /* Query Functions */
   581 static const GstQueryType *
   582 gst_nuv_demux_get_src_query_types (GstPad * pad)
   583 {
   584   static const GstQueryType src_types[] = {
   585     GST_QUERY_POSITION,
   586     GST_QUERY_DURATION,
   587     0
   588   };
   589 
   590   return src_types;
   591 }
   592 
   593 static gboolean
   594 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
   595 {
   596   gboolean res = FALSE;
   597   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   598 
   599   switch (GST_QUERY_TYPE (query)) {
   600     case GST_QUERY_POSITION:
   601       if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
   602 
   603         gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
   604         res = TRUE;
   605       }
   606       break;
   607     case GST_QUERY_DURATION:
   608       {
   609         gint64 duration = 0;
   610         duration = gst_nuv_demux_get_time_duration (nuv);
   611         if (duration == GST_CLOCK_TIME_NONE) {
   612             duration = nuv->priv->duration_average;
   613         }
   614         if (duration != GST_CLOCK_TIME_NONE) {
   615             gst_query_set_duration (query, GST_FORMAT_TIME, duration);
   616             res = TRUE;
   617         }
   618       }
   619       break;
   620     default:
   621       res = FALSE;
   622       break;
   623   }
   624 
   625   gst_object_unref (nuv);
   626 
   627   return res;
   628 }
   629 
   630 static GstPad*
   631 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
   632 {
   633     GstPad *pad = NULL;
   634     pad = gst_pad_new_from_static_template (template, name);
   635     gst_pad_set_caps (pad, caps);
   636     gst_pad_set_active (pad, TRUE);
   637     gst_pad_use_fixed_caps (pad);
   638     gst_element_add_pad (GST_ELEMENT (nuv), pad);
   639     gst_pad_set_event_function (pad, 
   640         GST_DEBUG_FUNCPTR (gst_nuv_demux_srcpad_event));
   641 
   642     return pad;
   643 }
   644 
   645 static void
   646 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
   647 {
   648   if (nuv->priv->h.i_video_blocks != 0) {
   649     GstCaps *video_caps = NULL;
   650 
   651     video_caps = gst_caps_new_simple ("video/x-divx",
   652         "divxversion", G_TYPE_INT, 4,
   653         "width", G_TYPE_INT, nuv->priv->h.i_width,
   654         "height", G_TYPE_INT, nuv->priv->h.i_height,
   655         "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
   656         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
   657         "pixel-aspect-ratio", GST_TYPE_FRACTION,
   658         (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
   659 
   660     nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
   661     gst_caps_unref (video_caps);
   662   }
   663 
   664   if (nuv->priv->h.i_audio_blocks != 0) {
   665     GstCaps *audio_caps = NULL;
   666 
   667     audio_caps = gst_caps_new_simple ("audio/mpeg",
   668         "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
   669         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
   670         "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
   671         "layer", G_TYPE_INT, 3, // fixme: magic number
   672         "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
   673     
   674     nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
   675     gst_caps_unref (audio_caps);
   676   }
   677 
   678   gst_element_no_more_pads (GST_ELEMENT (nuv));
   679 }
   680 
   681 static gboolean
   682 gst_nuv_demux_validate_header (nuv_frame_header *h)
   683 {
   684   gboolean valid = FALSE;
   685   //g_debug ("Type %d   =  Compression %d", h->i_type, h->i_compression);
   686   //g_usleep (1 * G_USEC_PER_SEC );
   687   switch  (h->i_type) {
   688 /*
   689     case 'V':
   690       if (h->i_compression == 0 ||
   691           h->i_compression == 1 ||
   692           h->i_compression == 2 ||
   693           h->i_compression == 'N' ||
   694           h->i_compression == 'L') {
   695          valid = TRUE;
   696       }
   697       break;
   698     case 'A':
   699       if (h->i_compression == 0 ||
   700           h->i_compression == 1 ||
   701           h->i_compression == 2 ||
   702           h->i_compression == 3 ||
   703           h->i_compression == 'F' ||
   704           h->i_compression == 'S' ||
   705           h->i_compression == 'N' ||
   706           h->i_compression == 'L') {
   707          valid = TRUE;
   708       }
   709       break;
   710     case 'S':
   711       if (h->i_compression == 'B' ||
   712           h->i_compression == 'A' ||
   713           h->i_compression == 'V' ||
   714           h->i_compression == 'S') {
   715          valid = TRUE;
   716       }
   717       break;
   718 */
   719     case 'A':
   720     case 'V':
   721     case 'S':
   722     case 'R':
   723     case 'D':
   724       valid  = TRUE;
   725       break;
   726     default:
   727       valid = FALSE;
   728   }
   729 
   730   return valid;
   731 }
   732 
   733 static GstFlowReturn
   734 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
   735 {
   736   GstFlowReturn ret = GST_FLOW_OK;
   737   gboolean valid = FALSE;
   738 
   739   do {
   740     ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
   741     if (ret != GST_FLOW_OK) {
   742       g_debug ("FAIL TO READ HEADER");
   743       return ret;
   744     }
   745 
   746     if (gst_nuv_demux_validate_header (&nuv->priv->fh) == TRUE)
   747 	valid = TRUE;
   748     else 
   749        g_debug ("Invalid frame header");
   750 
   751   } while (valid == FALSE);
   752 
   753 //  } while (gst_nuv_demux_validate_header (nuv->priv->fh));    
   754 
   755   nuv->priv->state = GST_NUV_DEMUX_MOVI;
   756   return ret;
   757 }
   758 
   759 static gboolean
   760 gst_nuv_combine_flow (GstNuvDemux *nuv)
   761 {
   762     GstFlowReturn ret_video = nuv->priv->last_video_return;
   763     GstFlowReturn ret_audio = nuv->priv->last_audio_return;
   764 
   765     if ((ret_video != GST_FLOW_OK) &&
   766         (ret_audio != GST_FLOW_OK))
   767         return FALSE;
   768 
   769     if (GST_FLOW_IS_FATAL (ret_video))
   770         return FALSE;
   771 
   772     if (GST_FLOW_IS_FATAL (ret_audio))
   773         return FALSE;
   774 
   775     return TRUE;
   776 }
   777 
   778 static GstFlowReturn
   779 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
   780 {
   781   GstFlowReturn ret = GST_FLOW_OK;
   782   GstPad *pad = NULL;
   783   guint64 timestamp;
   784   GstBuffer *buf = NULL;
   785   nuv_frame_header h;
   786 
   787   h = nuv->priv->fh;
   788 
   789   if (h.i_type == 'R')
   790     goto done;
   791 
   792   if (h.i_length > 0) {
   793     ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
   794     if ((ret != GST_FLOW_OK) || (buf == NULL)) {
   795       goto done;
   796     }
   797 
   798     if ((h.i_timecode < 0)) {
   799       h.i_timecode = 0;
   800       //goto done;
   801     }
   802 
   803     timestamp = h.i_timecode * GST_MSECOND;
   804     GST_BUFFER_TIMESTAMP (buf) = timestamp;
   805   }
   806   else {
   807     goto done;
   808   }
   809 
   810 
   811   switch (h.i_type) {
   812     case 'V':
   813     {
   814       pad = nuv->priv->src_video_pad;
   815 
   816       if (nuv->priv->new_video_segment) {
   817 
   818         /* send new segment event*/
   819         gst_pad_push_event (nuv->priv->src_video_pad,
   820           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   821                                      GST_CLOCK_TIME_NONE, 0));
   822 
   823         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   824             nuv->priv->time_start = timestamp;
   825         }
   826         nuv->priv->new_video_segment = FALSE;
   827       }
   828 
   829       break;
   830     }
   831     case 'A':
   832     {
   833       pad = nuv->priv->src_audio_pad;
   834 
   835       if (nuv->priv->new_audio_segment) {
   836         /* send new segment event*/
   837         gst_pad_push_event (nuv->priv->src_audio_pad,
   838           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   839                                      GST_CLOCK_TIME_NONE, 0));
   840 
   841         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   842             nuv->priv->time_start = timestamp;
   843         }
   844         nuv->priv->new_audio_segment = FALSE;
   845       }
   846 
   847       break;
   848     }
   849     case 'S':
   850    {
   851       switch (h.i_compression) {
   852         case 'V':
   853           GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
   854           gst_pad_push_event (nuv->priv->src_video_pad,
   855               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
   856               		GST_CLOCK_TIME_NONE, 0));
   857           break;
   858         case 'A':
   859           GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
   860           gst_pad_push_event (nuv->priv->src_audio_pad,
   861               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   862               		GST_CLOCK_TIME_NONE, 0));
   863           break;
   864         default:
   865           break;
   866       }
   867       goto done;
   868     }
   869     default:
   870       break;
   871   }
   872 
   873   if ((buf != NULL) && (pad != NULL)) {
   874       /* update average time */
   875       nuv->priv->streamer_offset += h.i_length;
   876       gst_segment_set_last_stop (&nuv->priv->segment, GST_FORMAT_TIME, timestamp);
   877       gst_nuv_demux_update_duration (nuv, timestamp);
   878 
   879       /* pushing the buffer */
   880       gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
   881       ret = gst_pad_push (pad, buf);
   882       buf = NULL;
   883 
   884       if (ret != GST_FLOW_OK) {
   885         GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
   886 
   887         if (pad == nuv->priv->src_video_pad) {
   888             nuv->priv->last_video_return = ret;
   889         }
   890         else if (pad == nuv->priv->src_audio_pad) {
   891             nuv->priv->last_audio_return = ret;
   892         }
   893 
   894         /* verify anothers flow if is necessary stop task */
   895         if (gst_nuv_combine_flow (nuv) != FALSE) {
   896           ret = GST_FLOW_OK;
   897         } else {
   898 	  GST_WARNING_OBJECT (nuv, "error: on push");
   899 	}
   900 
   901       }
   902   } 
   903 
   904 done:
   905   if (buf != NULL) {
   906       gst_buffer_unref (buf);
   907       buf = NULL;
   908   }
   909   if (ret == GST_FLOW_OK) {
   910      nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   911      memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
   912   }
   913   return ret;
   914 }
   915 
   916 static GstFlowReturn
   917 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
   918 {
   919   GstFlowReturn ret = GST_FLOW_OK;
   920 
   921   /* ffmpeg extra data */
   922   ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
   923       &nuv->priv->mpeg_buffer);
   924   if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
   925     return ret;
   926   }
   927 
   928   GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
   929   nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   930   return ret;
   931 }
   932 
   933 static GstFlowReturn
   934 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
   935 {
   936   GstFlowReturn ret = GST_FLOW_OK;
   937 
   938   /* Load 'D' */
   939   nuv_frame_header h;
   940 
   941   ret = gst_nuv_demux_frame_header_load (nuv, &h);
   942   if (ret != GST_FLOW_OK)
   943     return ret;
   944 
   945   if (h.i_type != 'D') {
   946     GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
   947     return GST_FLOW_NOT_SUPPORTED;
   948   }
   949 
   950   if (h.i_length > 0) {
   951     if (h.i_compression == 'F') {
   952       nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
   953     } else {
   954       GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
   955       return GST_FLOW_NOT_SUPPORTED;
   956     }
   957   } else {
   958     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   959   }
   960 
   961   return ret;
   962 }
   963 
   964 static GstFlowReturn
   965 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
   966 {
   967   GstFlowReturn ret = GST_FLOW_OK;
   968 
   969   ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
   970   if (ret != GST_FLOW_OK)
   971     return ret;
   972 
   973   gst_nuv_demux_create_pads (nuv);
   974   nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   975   return ret;
   976 }
   977 
   978 static GstFlowReturn
   979 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
   980 {
   981   GstBuffer *buf = NULL;
   982   GstFlowReturn res = GST_FLOW_OK;
   983 
   984   res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
   985   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   986     if (buf != NULL) {
   987       gst_buffer_unref (buf);
   988     }
   989     return res;
   990   }
   991 
   992   if (buf->data[0] == 'X') {
   993     gst_buffer_unref (buf);
   994     buf = NULL;
   995     nuv_frame_header h;
   996 
   997     res = gst_nuv_demux_frame_header_load (nuv, &h);
   998     if (res != GST_FLOW_OK)
   999       return res;
  1000 
  1001     if (h.i_length != 512) {
  1002       return GST_FLOW_ERROR;
  1003     }
  1004     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
  1005   } else {
  1006     nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
  1007     g_object_unref (buf);
  1008     GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
  1009         (_("incomplete NUV support")), ("incomplete NUV support"));
  1010     return GST_FLOW_ERROR;
  1011   }
  1012   return res;
  1013 }
  1014 
  1015 
  1016 static GstFlowReturn
  1017 gst_nuv_demux_play (GstPad * pad)
  1018 {
  1019   GstFlowReturn res = GST_FLOW_OK;
  1020   GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
  1021 
  1022   switch (nuv->priv->state) {
  1023     case GST_NUV_DEMUX_START:
  1024       res = gst_nuv_demux_stream_file_header (nuv);
  1025       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1026         goto pause;
  1027       }
  1028       break;
  1029 
  1030     case GST_NUV_DEMUX_HEADER_DATA:
  1031       res = gst_nuv_demux_stream_header_data (nuv);
  1032       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1033         goto pause;
  1034       }
  1035       break;
  1036 
  1037     case GST_NUV_DEMUX_EXTRA_DATA:
  1038       res = gst_nuv_demux_stream_extra_data (nuv);
  1039       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1040         goto pause;
  1041       }
  1042       break;
  1043 
  1044     case GST_NUV_DEMUX_MPEG_DATA:
  1045       res = gst_nuv_demux_stream_mpeg_data (nuv);
  1046       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1047         goto pause;
  1048       }
  1049       break;
  1050 
  1051     case GST_NUV_DEMUX_EXTEND_HEADER:
  1052       res = gst_nuv_demux_stream_extend_header (nuv);
  1053       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1054         goto pause;
  1055       }
  1056       break;
  1057 
  1058     case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
  1059       res = gst_nuv_demux_stream_extend_header_data (nuv);
  1060       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1061         goto pause;
  1062       }
  1063       break;
  1064 
  1065     case GST_NUV_DEMUX_FRAME_HEADER:
  1066       res = gst_nuv_demux_read_head_frame (nuv);
  1067       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1068         goto pause;
  1069       }
  1070       break;
  1071 
  1072     case GST_NUV_DEMUX_MOVI: 
  1073       res = gst_nuv_demux_stream_data (nuv);
  1074       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1075         goto pause;
  1076       }
  1077       break;
  1078 
  1079     case GST_NUV_DEMUX_INVALID_DATA:
  1080       goto pause;
  1081       break;
  1082     default:
  1083       g_assert_not_reached ();
  1084   }
  1085 
  1086   return GST_FLOW_OK;
  1087 
  1088 pause:
  1089   GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
  1090   gst_pad_pause_task (nuv->priv->sinkpad);
  1091   if (GST_FLOW_IS_FATAL (res)) {
  1092     GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
  1093         (_("Internal data stream error.")),
  1094         ("streaming stopped, reason %s", gst_flow_get_name (res)));
  1095 
  1096     gst_nuv_demux_send_eos (nuv);
  1097   }
  1098   return res;
  1099 }
  1100 
  1101 static void
  1102 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
  1103 {
  1104   gst_element_post_message (GST_ELEMENT (nuv),
  1105       gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
  1106 
  1107   if (nuv->priv->src_video_pad)
  1108     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
  1109   if (nuv->priv->src_audio_pad)
  1110     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
  1111 }
  1112 
  1113 static GstFlowReturn
  1114 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
  1115     GstBuffer ** buffer)
  1116 {
  1117   GstFlowReturn ret = GST_FLOW_OK;
  1118 
  1119   if (size == 0) {
  1120     return ret;
  1121   }
  1122 
  1123   if (nuv->priv->mode == NUV_PULL_MODE) {
  1124     ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
  1125     if (ret == GST_FLOW_OK) {
  1126         if (move) {
  1127 		    nuv->priv->offset += size;
  1128 		}
  1129       /* got eos */
  1130     } else if (ret == GST_FLOW_UNEXPECTED) {
  1131       if (buffer != NULL)
  1132           gst_buffer_unref (buffer);
  1133 
  1134       gst_nuv_demux_send_eos (nuv);
  1135       return GST_FLOW_WRONG_STATE;
  1136     }
  1137   } else {
  1138     if (gst_adapter_available (nuv->priv->adapter) < size) {
  1139       nuv->priv->more_data = TRUE;
  1140       return GST_FLOW_ERROR_NO_DATA;
  1141     }
  1142     if (move) {
  1143       *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
  1144     } else {
  1145       guint8 *data = NULL;
  1146       data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
  1147       *buffer = gst_buffer_new ();
  1148       gst_buffer_set_data (*buffer, data, size);
  1149     }
  1150   }
  1151   return ret;
  1152 }
  1153 
  1154 static gboolean
  1155 gst_nuv_demux_sink_activate (GstPad * sinkpad)
  1156 {
  1157   gboolean res = TRUE;
  1158   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1159  
  1160   g_debug ("SINK ACTIVATE");
  1161 
  1162   if (gst_pad_check_pull_range (sinkpad)) {
  1163     g_debug ("PULL");
  1164     gst_adapter_clear (nuv->priv->adapter);
  1165     res = gst_pad_activate_pull (sinkpad, TRUE);
  1166   } else {
  1167     g_debug ("PUSH");
  1168     gst_adapter_clear (nuv->priv->adapter);
  1169     res = gst_pad_activate_push (sinkpad, TRUE);
  1170   }
  1171   
  1172   g_object_unref (nuv);
  1173   return res;
  1174 }
  1175 
  1176 static gboolean
  1177 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
  1178 {
  1179   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1180 
  1181   if (active) {
  1182     GST_DEBUG_OBJECT (nuv, "activating pull function");
  1183     nuv->priv->mode = NUV_PULL_MODE;
  1184     gst_adapter_clear (nuv->priv->adapter);
  1185 
  1186     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
  1187   } else {
  1188     GST_DEBUG_OBJECT (nuv, "deactivating pull function");
  1189     g_debug ("STOP TASK");
  1190     gst_pad_stop_task (sinkpad);
  1191   }
  1192   gst_object_unref (nuv);
  1193 
  1194   return TRUE;
  1195 }
  1196 
  1197 static gboolean
  1198 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
  1199 {
  1200   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1201 
  1202   if (active) {
  1203     nuv->priv->mode = NUV_PUSH_MODE;
  1204     gst_adapter_clear (nuv->priv->adapter);
  1205 
  1206     GST_DEBUG_OBJECT (nuv, "activating push/chain function");
  1207   } else {
  1208     GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
  1209   }
  1210 
  1211   gst_object_unref (nuv);
  1212 
  1213   return TRUE;
  1214 }
  1215 
  1216 static gboolean
  1217 gst_nuv_demux_do_seek (GstNuvDemux *nuv, GstEvent * event)
  1218 {
  1219   gdouble rate;
  1220   GstFormat format;
  1221   GstSeekFlags flags;
  1222   GstSeekType cur_type;
  1223   gint64 cur;
  1224   GstSeekType stop_type;
  1225   gint64 stop;
  1226   gboolean flush;
  1227 
  1228   g_debug ("DEMUX SEEK");
  1229   gst_event_parse_seek (event, &rate, &format, &flags,
  1230       &cur_type, &cur, &stop_type, &stop);
  1231 
  1232   if (format == GST_FORMAT_BYTES &&
  1233           nuv->priv->state > GST_NUV_DEMUX_EXTEND_HEADER_DATA &&
  1234           gst_pad_is_linked (nuv->priv->sinkpad)) {
  1235 
  1236     flush = flags & GST_SEEK_FLAG_FLUSH;
  1237     if (flush) {
  1238       gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_start ());
  1239 
  1240       if (nuv->priv->src_video_pad != NULL) {
  1241         gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_start ());
  1242       }
  1243 
  1244       if (nuv->priv->src_audio_pad != NULL) {
  1245         gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_start ());
  1246       }
  1247     }
  1248     else {
  1249 	gst_pad_pause_task (nuv->priv->sinkpad);
  1250     }
  1251 
  1252     GST_PAD_STREAM_LOCK (nuv->priv->sinkpad);
  1253 
  1254     g_debug ("offset %ld, cur %ld", nuv->priv->offset, cur);
  1255 
  1256     nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
  1257 
  1258     if (flush) {
  1259       gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_stop ());
  1260 
  1261       if (nuv->priv->src_video_pad != NULL) {
  1262         gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_stop ());
  1263       }
  1264 
  1265       if (nuv->priv->src_audio_pad != NULL) {
  1266         gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_stop ());
  1267       }
  1268     }
  1269 
  1270     g_debug ("STARTING TASK AGAIN");
  1271     gst_pad_start_task (nuv->priv->sinkpad, (GstTaskFunction) gst_nuv_demux_loop,
  1272         nuv->priv->sinkpad);
  1273 
  1274     GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
  1275     return TRUE;
  1276   }
  1277   return FALSE;
  1278 }
  1279 
  1280 static gboolean
  1281 gst_nuv_demux_srcpad_event (GstPad * pad, GstEvent * event)
  1282 {
  1283   gboolean res = FALSE;
  1284   GstNuvDemux *nuv;
  1285 
  1286   nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1287   
  1288   switch (GST_EVENT_TYPE (event)) {
  1289     case GST_EVENT_SEEK:
  1290       res = gst_nuv_demux_do_seek (nuv, event);
  1291       break;
  1292     default:
  1293       res = FALSE;
  1294       break;
  1295   }
  1296   return res;
  1297 }
  1298 
  1299 static gboolean
  1300 gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event)
  1301 {
  1302   gboolean res = FALSE;
  1303   GstNuvDemux *nuv;
  1304 
  1305   nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1306 
  1307   switch (GST_EVENT_TYPE (event)) {
  1308     case GST_EVENT_SEEK:
  1309     {
  1310       gdouble rate;
  1311       GstFormat format;
  1312       GstSeekFlags flags;
  1313       GstSeekType cur_type;
  1314       gint64 cur;
  1315       GstSeekType stop_type;
  1316       gint64 stop;
  1317 
  1318       gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur, &stop_type, &stop);
  1319       GST_DEBUG_OBJECT (nuv, "got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop);
  1320       g_debug ("got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop);
  1321       res = gst_pad_event_default (pad, event);
  1322       break;
  1323     }
  1324     case GST_EVENT_NEWSEGMENT:
  1325     {
  1326       gint64 start, stop, time;
  1327       gdouble rate;
  1328       GstFormat format;
  1329       gboolean update;
  1330 
  1331       gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &time);
  1332       GST_DEBUG_OBJECT (nuv, "got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop);
  1333       g_debug ("got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop);
  1334       //nuv->priv->duration_bytes = stop - start;
  1335       res = gst_pad_event_default (pad, event);
  1336       break;
  1337     }
  1338     case GST_EVENT_EOS:
  1339     default:
  1340       res = gst_pad_event_default (pad, event);
  1341       break;
  1342   }
  1343 
  1344   gst_object_unref (nuv);
  1345   return res;
  1346 }
  1347 
  1348 static GstFlowReturn
  1349 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
  1350 {
  1351   GstFlowReturn ret = GST_FLOW_OK;
  1352   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1353   
  1354   if (nuv->priv->mode != NUV_PUSH_MODE)
  1355     return ret;
  1356 
  1357   g_debug ("CHAIN ADP %p", nuv->priv->adapter);
  1358 
  1359   gst_adapter_push (nuv->priv->adapter, buf);  
  1360   
  1361   while ((ret == GST_FLOW_OK) && (nuv->priv->more_data == FALSE)) {
  1362       ret = gst_nuv_demux_play (pad);
  1363   }
  1364 
  1365   nuv->priv->more_data = FALSE;
  1366   gst_object_unref (nuv);
  1367 
  1368   g_debug ("CHAIN END");
  1369   
  1370   return ret;
  1371 }
  1372 
  1373 static void
  1374 gst_nuv_demux_loop (GstPad * pad)
  1375 {
  1376   gst_nuv_demux_play (pad);
  1377 }
  1378 
  1379 static void
  1380 gst_nuv_demux_reset (GstNuvDemux * nuv)
  1381 {
  1382   nuv->priv->more_data = FALSE;
  1383   nuv->priv->state = GST_NUV_DEMUX_START;
  1384   nuv->priv->mode = NUV_PUSH_MODE;
  1385   nuv->priv->offset = 0;
  1386   nuv->priv->streamer_offset = 0;
  1387   nuv->priv->time_start = 0;
  1388   nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
  1389   nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
  1390   nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
  1391   nuv->priv->duration_average = GST_CLOCK_TIME_NONE;
  1392   gst_segment_init (&nuv->priv->segment, GST_FORMAT_TIME);
  1393 
  1394   gst_adapter_clear (nuv->priv->adapter);
  1395 
  1396   if (nuv->priv->mpeg_buffer != NULL) {
  1397     gst_buffer_unref (nuv->priv->mpeg_buffer);
  1398     nuv->priv->mpeg_buffer = NULL;
  1399   }
  1400 }
  1401 
  1402 static void
  1403 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
  1404 {
  1405   if (nuv->priv->src_video_pad) {
  1406     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
  1407     nuv->priv->src_video_pad = NULL;
  1408   }
  1409 
  1410   if (nuv->priv->src_audio_pad) {
  1411     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
  1412     nuv->priv->src_audio_pad = NULL;
  1413   }
  1414 }
  1415 
  1416 static GstStateChangeReturn
  1417 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
  1418 {
  1419   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  1420 
  1421   switch (transition) {
  1422     case GST_STATE_CHANGE_NULL_TO_READY:
  1423 	g_debug ("NULL_TO_READY");
  1424       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1425       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1426       break;
  1427     default:
  1428       break;
  1429   }
  1430 
  1431   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1432   if (ret == GST_STATE_CHANGE_FAILURE) {
  1433     goto done;
  1434   }
  1435 
  1436   switch (transition) {
  1437     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1438     //case GST_STATE_CHANGE_READY_TO_NULL:
  1439       g_debug ("PAUSED_TO_READY");
  1440       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1441       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1442       break;
  1443     default:
  1444       break;
  1445   }
  1446 
  1447 done:
  1448   return ret;
  1449 }
  1450 
  1451 static void
  1452 gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp)
  1453 {
  1454     guint64 interval = 0;
  1455 
  1456     if (gst_nuv_demux_get_time_duration (nuv) != GST_CLOCK_TIME_NONE)
  1457         return;
  1458 
  1459     interval  = current_timestamp - nuv->priv->last_update;
  1460 
  1461     if (interval > (10 * GST_SECOND)) {
  1462         GstMessage* msg = NULL;
  1463         gint64 average = 0;
  1464         gint64 duration_bytes = gst_nuv_demux_get_bytes_duration (nuv);
  1465 
  1466         if (duration_bytes == GST_CLOCK_TIME_NONE) {
  1467             return;
  1468         }
  1469 
  1470         interval = gst_util_uint64_scale (1, current_timestamp - nuv->priv->time_start, GST_SECOND);
  1471         if (interval > 0) {
  1472             average = gst_util_uint64_scale (1 , nuv->priv->streamer_offset, interval);
  1473         }
  1474         if (average > 0) {
  1475             nuv->priv->duration_average = gst_util_uint64_scale (GST_SECOND, duration_bytes, average);
  1476         }
  1477         
  1478         nuv->priv->last_update = current_timestamp;
  1479         msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_average);
  1480         gst_element_post_message (GST_ELEMENT (nuv), msg);
  1481         GST_DEBUG_OBJECT (nuv, "New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
  1482         g_debug ("New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
  1483     }
  1484 }
  1485 
  1486 static gint64
  1487 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv)
  1488 {
  1489     if (nuv->priv->duration_bytes == GST_CLOCK_TIME_NONE) {
  1490         GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
  1491         GstQuery *query = gst_query_new_duration (GST_FORMAT_BYTES);
  1492         if (gst_pad_query (peer, query)) {
  1493             gint64 duration;
  1494 
  1495             gst_query_parse_duration (query, NULL, &duration);
  1496             nuv->priv->duration_bytes = duration;
  1497         }
  1498         gst_object_unref (peer);
  1499         gst_query_unref (query);
  1500     }
  1501     return nuv->priv->duration_bytes;
  1502 }
  1503 
  1504 static gint64
  1505 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv)
  1506 {
  1507     if (nuv->priv->duration_time == GST_CLOCK_TIME_NONE) {
  1508         GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
  1509         GstQuery *query = gst_query_new_duration (GST_FORMAT_TIME);
  1510         if (gst_pad_query (peer, query)) {
  1511             gint64 duration;
  1512             gst_query_parse_duration (query, NULL, &duration);
  1513             nuv->priv->duration_time = duration;
  1514         }
  1515         gst_object_unref (peer);
  1516         gst_query_unref (query);
  1517     }
  1518     return nuv->priv->duration_time;
  1519 }
  1520 
  1521 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
  1522 GstBuffer *
  1523 gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
  1524 {
  1525   GstBuffer *buffer;
  1526   GstBuffer *cur;
  1527   guint8 *data;
  1528 
  1529   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
  1530   g_return_val_if_fail (nbytes > 0, NULL);
  1531 
  1532   GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);
  1533 
  1534   /* we don't have enough data, return NULL. This is unlikely
  1535    * as one usually does an _available() first instead of peeking a
  1536    * random size. */
  1537   if (G_UNLIKELY (nbytes > adapter->size))
  1538     return NULL;
  1539 
  1540   /* our head buffer has enough data left, return it */
  1541   cur = adapter->buflist->data;
  1542   if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) {
  1543     GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
  1544         nbytes);
  1545     buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes);
  1546 
  1547     gst_adapter_flush (adapter, nbytes);
  1548 
  1549     return buffer;
  1550   }
  1551 
  1552   data = gst_adapter_take (adapter, nbytes);
  1553   if (data == NULL)
  1554     return NULL;
  1555 
  1556   buffer = gst_buffer_new ();
  1557   GST_BUFFER_DATA (buffer) = data;
  1558   GST_BUFFER_MALLOCDATA (buffer) = data;
  1559   GST_BUFFER_SIZE (buffer) = nbytes;
  1560 
  1561   return buffer;
  1562 }
  1563 #endif
  1564 
  1565 
  1566 static gboolean
  1567 plugin_init (GstPlugin * plugin)
  1568 {
  1569 #ifdef ENABLE_NLS
  1570   setlocale (LC_ALL, "");
  1571   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  1572 #endif /* ENABLE_NLS */
  1573 
  1574   if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
  1575           GST_TYPE_NUV_DEMUX)) {
  1576     return FALSE;
  1577   }
  1578   return TRUE;
  1579 }
  1580 
  1581 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1582     GST_VERSION_MINOR,
  1583     "nuvdemux",
  1584     "Demuxes and muxes audio and video",
  1585      plugin_init, VERSION, "LGPL", "NuvDemux", "")
  1586