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