gst-plugins-nuvdemux/nuvdemux/gstnuvdemux.c
author renatofilho
Mon Apr 02 16:02:03 2007 +0100 (2007-04-02)
branchtrunk
changeset 476 f9330272e09d
parent 442 db17a60b9b42
child 477 88bf59e8da5f
permissions -rw-r--r--
[svn r481] implemented dynamic index table
     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 #define GST_FLOW_ERROR_EOS	-102
    65 
    66 enum
    67 {
    68    NUV_PUSH_MODE = 0,
    69    NUV_PULL_MODE
    70 };
    71 
    72 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
    73 
    74 static const GstElementDetails gst_nuv_demux_details =
    75 GST_ELEMENT_DETAILS ("Nuv demuxer",
    76     "Codec/Demuxer",
    77     "Demultiplex a .nuv file into audio and video",
    78     "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
    79     "Rosfran Borges <rosfran.borges@indt.org.br>");
    80 
    81 
    82 /* file header */
    83 typedef struct
    84 {
    85     gchar id[12];       /* "NuppelVideo\0" or "MythTVVideo\0" */
    86     gchar version[5];    /* "x.xx\0" */
    87 
    88     gint  i_width;
    89     gint  i_height;
    90     gint  i_width_desired;
    91     gint  i_height_desired;
    92 
    93     gchar i_mode;            /* P progressive, I interlaced */
    94 
    95     gdouble  d_aspect;       /* 1.0 squared pixel */
    96     gdouble  d_fps;
    97     //fps num/denom
    98     gint     i_fpsn;
    99     gint     i_fpsd;
   100 
   101     gint     i_video_blocks; /* 0 no video, -1 unknown */
   102     gint     i_audio_blocks;
   103     gint     i_text_blocks;
   104 
   105     gint     i_keyframe_distance;
   106 
   107 } nuv_header;
   108 
   109 /* frame header */
   110 typedef struct
   111 {
   112     gchar i_type;        /* A: audio, V: video, S: sync; T: test
   113                            R: Seekpoint (string:RTjjjjjjjj)
   114                            D: Extra data for codec */
   115     gchar i_compression; /* V: 0 uncompressed
   116                               1 RTJpeg
   117                               2 RTJpeg+lzo
   118                               N black frame
   119                               L copy last
   120                            A: 0 uncompressed (44100 1-bits, 2ch)
   121                               1 lzo
   122                               2 layer 2
   123                               3 layer 3
   124                               F flac
   125                               S shorten
   126                               N null frame loudless
   127                               L copy last
   128                             S: B audio and vdeo sync point
   129                                A audio sync info (timecode == effective
   130                                     dsp frequency*100)
   131                                V next video sync (timecode == next video
   132                                     frame num)
   133                                S audio,video,text correlation */
   134     gchar i_keyframe;    /* 0 keyframe, else no no key frame */
   135     guint8 i_filters;  /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
   136                            0x02: gauss 5 pixel (8,1,1,1,1)/12
   137                            0x04: cartoon filter */
   138 
   139     gint32 i_timecode;     /* ms */
   140 
   141     gint i_length;       /* V,A,T: length of following data
   142                            S: length of packet correl */
   143 } nuv_frame_header;
   144 
   145 
   146 /* FIXME Not sure of this one */
   147 typedef struct
   148 {
   149     gint             i_version;
   150     guint32	     i_video_fcc;
   151 
   152     guint32	     i_audio_fcc;
   153     gint             i_audio_sample_rate;
   154     gint             i_audio_bits_per_sample;
   155     gint             i_audio_channels;
   156     gint             i_audio_compression_ratio;
   157     gint             i_audio_quality;
   158     gint             i_rtjpeg_quality;
   159     gint             i_rtjpeg_luma_filter;
   160     gint             i_rtjpeg_chroma_filter;
   161     gint             i_lavc_bitrate;
   162     gint             i_lavc_qmin;
   163     gint             i_lavc_qmax;
   164     gint             i_lavc_maxqdiff;
   165     gint64           i_seekable_offset;
   166     gint64           i_keyframe_adjust_offset;
   167 
   168 } nuv_extended_header;
   169 
   170 typedef struct
   171 {
   172   gint64 timecode;
   173   gint64 offset;
   174 
   175 } frame_index_data;
   176 
   177 typedef enum {
   178   GST_NUV_DEMUX_START,
   179   GST_NUV_DEMUX_HEADER_DATA,
   180   GST_NUV_DEMUX_EXTRA_DATA,
   181   GST_NUV_DEMUX_MPEG_DATA,
   182   GST_NUV_DEMUX_EXTEND_HEADER,
   183   GST_NUV_DEMUX_EXTEND_HEADER_DATA,
   184   GST_NUV_DEMUX_INDEX_CREATE,
   185   GST_NUV_DEMUX_FRAME_HEADER,
   186   GST_NUV_DEMUX_MOVI,
   187   GST_NUV_DEMUX_INVALID_DATA
   188 } GstNuvDemuxState;
   189 
   190 struct _GstNuvDemuxPrivate {
   191   /* used for indicate the mode */
   192   guint         mode;
   193 
   194   /* used on push mode */
   195   GstAdapter    *adapter;
   196 
   197   /* pads */
   198   GstPad        *sinkpad;
   199   GstPad        *src_video_pad;
   200   GstPad        *src_audio_pad;
   201 
   202   /* Flow control */
   203   GstFlowReturn     last_video_return;
   204   GstFlowReturn     last_audio_return;
   205   gboolean          more_data;
   206   gboolean	    eos;
   207 
   208   /* NUV decoding state */
   209   GstNuvDemuxState  state;
   210   guint64           offset;
   211 
   212   /* duration information */
   213   guint64            duration_bytes;
   214   guint64            duration_time;
   215   guint64            segment_stop;
   216   guint64            segment_start;
   217 
   218   /* segment control info */
   219   gboolean          new_audio_segment;
   220   gboolean          new_video_segment;
   221 
   222   /* Mpeg ExtraData */
   223   guint64           mpeg_data_size;
   224   GstBuffer         *mpeg_buffer;
   225 
   226   /* Headers */
   227   nuv_header h;
   228   nuv_extended_header eh;
   229   nuv_frame_header fh;
   230 
   231   /* anothers info */
   232   gint64            header_lengh;
   233   gint64            time_start;
   234   gint64            time_diff;
   235   gint64            time_qos;
   236   guint64           last_frame_time;
   237   GSList            *index;
   238 };
   239 
   240 
   241 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
   242     GST_PAD_SINK,
   243     GST_PAD_ALWAYS,
   244     GST_STATIC_CAPS ("video/x-nuv"));
   245 
   246 static GstStaticPadTemplate audio_src_template =
   247 GST_STATIC_PAD_TEMPLATE ("audio_src",
   248     GST_PAD_SRC,
   249     GST_PAD_SOMETIMES,
   250     GST_STATIC_CAPS_ANY);
   251 
   252 static GstStaticPadTemplate video_src_template =
   253 GST_STATIC_PAD_TEMPLATE ("video_src",
   254     GST_PAD_SRC,
   255     GST_PAD_SOMETIMES,
   256     GST_STATIC_CAPS_ANY);
   257 
   258 static void gst_nuv_demux_dispose (GObject * object);
   259 static void gst_nuv_demux_finalize (GObject * object);
   260 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
   261     GstStateChange transition);
   262 static void gst_nuv_demux_loop (GstPad * pad);
   263 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
   264 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
   265 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
   266     gboolean active);
   267 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
   268     gboolean active);
   269 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
   270 //static gboolean gst_nuv_demux_sink_event    (GstPad *pad, GstEvent *event);
   271 static gboolean gst_nuv_demux_srcpad_event  (GstPad * pad, GstEvent * event);
   272 static frame_index_data * gst_nuv_demux_do_seek_index (GstNuvDemux *nuv, gint64 seek_pos, 
   273   gint64 segment_stop, GstFormat format);
   274 
   275 
   276 
   277 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
   278     gboolean move, GstBuffer ** buffer);
   279 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
   280 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
   281 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
   282 static void gst_nuv_demux_create_seek_index (GstNuvDemux * nuv);
   283 static void gst_nuv_demux_store_keyframe (GstNuvDemux * nuv, nuv_frame_header *h);
   284 
   285 
   286 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
   287 GstBuffer * gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes);
   288 #endif
   289 
   290 
   291 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
   292 
   293 /******************************************************************************
   294  * Utils function
   295  ******************************************************************************/
   296 #if G_BYTE_ORDER == G_BIG_ENDIAN
   297 static inline gdouble
   298 _gdouble_swap_le_be (gdouble * d)
   299 {
   300   union
   301   {
   302     guint64 i;
   303     gdouble d;
   304   } u;
   305 
   306   u.d = *d;
   307   u.i = GUINT64_SWAP_LE_BE (u.i);
   308   return u.d;
   309 }
   310 
   311 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
   312 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
   313 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
   314 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
   315 
   316 static void
   317 double2fraction (double in, int *num, int *denom)
   318 {
   319     if (in == 29.97) {
   320         *num = 30000;
   321         *denom = 1001;
   322     } else if (in == 23.976) {
   323         *num = 24000;
   324         *denom = 1001;
   325     } else {
   326         *denom = 1;
   327         while (in - floor(in) >= 0.1) {
   328             *denom *= 10;
   329             in *= 10.0;
   330         }
   331         *num = (int)floor(in);
   332     }
   333 }
   334 
   335 /* GObject Functions */
   336 
   337 static void
   338 gst_nuv_demux_base_init (gpointer klass)
   339 {
   340   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
   341 
   342   gst_element_class_add_pad_template (element_class,
   343       gst_static_pad_template_get (&audio_src_template));
   344 
   345   gst_element_class_add_pad_template (element_class,
   346       gst_static_pad_template_get (&video_src_template));
   347 
   348   gst_element_class_add_pad_template (element_class,
   349       gst_static_pad_template_get (&sink_template));
   350   gst_element_class_set_details (element_class, &gst_nuv_demux_details);
   351 }
   352 
   353 static void
   354 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
   355 {
   356   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   357   GObjectClass *gobject_class = (GObjectClass *) klass;
   358 
   359   GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
   360       0, "Demuxer for NUV streams");
   361 
   362   parent_class = g_type_class_peek_parent (klass);
   363 
   364   gobject_class->dispose = gst_nuv_demux_dispose;
   365   gobject_class->finalize = gst_nuv_demux_finalize;
   366   gstelement_class->change_state = gst_nuv_demux_change_state;
   367 
   368   g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate));
   369 }
   370 
   371 static void
   372 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
   373 {
   374   nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv);
   375   nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
   376 
   377   /* creating adapter */
   378   nuv->priv->mode = NUV_PUSH_MODE;
   379   nuv->priv->adapter = gst_adapter_new ();
   380 
   381   nuv->priv->new_audio_segment = TRUE;
   382   nuv->priv->new_video_segment = TRUE;
   383 
   384   gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate);
   385   gst_pad_set_activatepull_function (nuv->priv->sinkpad,
   386       gst_nuv_demux_sink_activate_pull);
   387   gst_pad_set_activatepush_function (nuv->priv->sinkpad,
   388       gst_nuv_demux_sink_activate_push);
   389   gst_pad_set_chain_function (nuv->priv->sinkpad,
   390       GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
   391 /*
   392   gst_pad_set_event_function (nuv->priv->sinkpad,
   393       gst_nuv_demux_sink_event);
   394 */
   395 
   396   gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad);
   397 
   398 }
   399 
   400 static void
   401 gst_nuv_demux_dispose (GObject * object)
   402 {
   403   GstNuvDemux *nuv = GST_NUV_DEMUX (object);
   404 
   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         if (nuv->priv->duration_time != GST_CLOCK_TIME_NONE) {
   624             gst_query_set_duration (query, GST_FORMAT_TIME, nuv->priv->duration_time);
   625 	    res = TRUE;
   626         }
   627       }
   628       break;
   629     default:
   630       res = FALSE;
   631       break;
   632   }
   633 
   634   gst_object_unref (nuv);
   635 
   636   return res;
   637 }
   638 
   639 static GstPad*
   640 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
   641 {
   642     GstPad *pad = NULL;
   643     pad = gst_pad_new_from_static_template (template, name);
   644     gst_pad_set_caps (pad, caps);
   645     gst_pad_set_active (pad, TRUE);
   646     gst_pad_use_fixed_caps (pad);
   647     gst_element_add_pad (GST_ELEMENT (nuv), pad);
   648 
   649     gst_pad_set_event_function (pad,
   650       GST_DEBUG_FUNCPTR (gst_nuv_demux_srcpad_event));
   651 
   652     gst_pad_set_query_type_function (pad,
   653       GST_DEBUG_FUNCPTR (gst_nuv_demux_get_src_query_types));
   654 
   655     gst_pad_set_query_function (pad,
   656       GST_DEBUG_FUNCPTR (gst_nuv_demux_handle_src_query));
   657       
   658 
   659     return pad;
   660 }
   661 
   662 static void
   663 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
   664 {
   665   if (nuv->priv->h.i_video_blocks != 0) {
   666     GstCaps *video_caps = NULL;
   667 
   668     video_caps = gst_caps_new_simple ("video/x-divx",
   669         "divxversion", G_TYPE_INT, 4,
   670         "width", G_TYPE_INT, nuv->priv->h.i_width,
   671         "height", G_TYPE_INT, nuv->priv->h.i_height,
   672         "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
   673         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
   674         "pixel-aspect-ratio", GST_TYPE_FRACTION,
   675         (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
   676 
   677     nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
   678     gst_caps_unref (video_caps);
   679   }
   680 
   681   if (nuv->priv->h.i_audio_blocks != 0) {
   682     GstCaps *audio_caps = NULL;
   683 
   684     audio_caps = gst_caps_new_simple ("audio/mpeg",
   685         "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
   686         "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
   687         "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
   688         "layer", G_TYPE_INT, 3, // fixme: magic number
   689         "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
   690     
   691     nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
   692     gst_caps_unref (audio_caps);
   693   }
   694 
   695   gst_element_no_more_pads (GST_ELEMENT (nuv));
   696 }
   697 
   698 static gboolean
   699 gst_nuv_demux_validate_header (nuv_frame_header *h)
   700 {
   701   gboolean valid = FALSE;
   702   //g_debug ("Type %d   =  Compression %d", h->i_type, h->i_compression);
   703   //g_usleep (1 * G_USEC_PER_SEC );
   704   switch  (h->i_type) {
   705 /*
   706     case 'V':
   707       if (h->i_compression == 0 ||
   708           h->i_compression == 1 ||
   709           h->i_compression == 2 ||
   710           h->i_compression == 'N' ||
   711           h->i_compression == 'L') {
   712          valid = TRUE;
   713       }
   714       break;
   715     case 'A':
   716       if (h->i_compression == 0 ||
   717           h->i_compression == 1 ||
   718           h->i_compression == 2 ||
   719           h->i_compression == 3 ||
   720           h->i_compression == 'F' ||
   721           h->i_compression == 'S' ||
   722           h->i_compression == 'N' ||
   723           h->i_compression == 'L') {
   724          valid = TRUE;
   725       }
   726       break;
   727     case 'S':
   728       if (h->i_compression == 'B' ||
   729           h->i_compression == 'A' ||
   730           h->i_compression == 'V' ||
   731           h->i_compression == 'S') {
   732          valid = TRUE;
   733       }
   734       break;
   735 */
   736     case 'A':
   737     case 'V':
   738     case 'S':
   739     case 'R':
   740     case 'D':
   741     case 'Q':
   742       valid  = TRUE;
   743       break;
   744     default:
   745       valid = FALSE;
   746   }
   747 
   748   return valid;
   749 }
   750 
   751 static GstFlowReturn
   752 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
   753 {
   754   GstFlowReturn ret = GST_FLOW_OK;
   755   gboolean valid = FALSE;
   756 
   757   do {
   758     ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
   759     if (ret != GST_FLOW_OK) {
   760       return ret;
   761     }
   762 
   763     if (gst_nuv_demux_validate_header (&nuv->priv->fh) == TRUE)
   764       valid = TRUE;
   765 
   766   } while (valid == FALSE);
   767 
   768   nuv->priv->state = GST_NUV_DEMUX_MOVI;
   769   return ret;
   770 }
   771 
   772 static gboolean
   773 gst_nuv_combine_flow (GstNuvDemux *nuv)
   774 {
   775     GstFlowReturn ret_video = nuv->priv->last_video_return;
   776     GstFlowReturn ret_audio = nuv->priv->last_audio_return;
   777 
   778     if ((ret_video != GST_FLOW_OK) &&
   779         (ret_audio != GST_FLOW_OK))
   780         return FALSE;
   781 
   782     if (GST_FLOW_IS_FATAL (ret_video))
   783         return FALSE;
   784 
   785     if (GST_FLOW_IS_FATAL (ret_audio))
   786         return FALSE;
   787 
   788     return TRUE;
   789 }
   790 
   791 static GstFlowReturn
   792 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
   793 {
   794   GstFlowReturn ret = GST_FLOW_OK;
   795   GstPad *pad = NULL;
   796   guint64 timestamp;
   797   GstBuffer *buf = NULL;
   798   nuv_frame_header h;
   799 
   800   h = nuv->priv->fh;
   801 
   802   if (h.i_type == 'R')
   803     goto done;
   804 
   805   if (h.i_length > 0) {
   806     ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
   807     if ((ret != GST_FLOW_OK) || (buf == NULL)) {
   808       goto done;
   809     }
   810 
   811     if ((h.i_timecode < 0)) {
   812       h.i_timecode = 0;
   813       //goto done;
   814     }
   815 
   816     timestamp = h.i_timecode * GST_MSECOND;
   817     GST_BUFFER_TIMESTAMP (buf) = timestamp;
   818   }
   819   else {
   820     goto done;
   821   }
   822 
   823 
   824   switch (h.i_type) {
   825     case 'V':
   826     {
   827       pad = nuv->priv->src_video_pad;
   828 
   829       if (nuv->priv->new_video_segment) {
   830 
   831         /* send new segment event*/
   832         gst_pad_push_event (nuv->priv->src_video_pad,
   833           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   834                                      GST_CLOCK_TIME_NONE, 0));
   835 
   836         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   837             nuv->priv->time_start = timestamp;
   838         }
   839         nuv->priv->new_video_segment = FALSE;
   840       }
   841 
   842       break;
   843     }
   844     case 'A':
   845     {
   846       pad = nuv->priv->src_audio_pad;
   847 
   848       if (nuv->priv->new_audio_segment) {
   849         /* send new segment event*/
   850         gst_pad_push_event (nuv->priv->src_audio_pad,
   851           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   852                                      GST_CLOCK_TIME_NONE, 0));
   853 
   854         if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
   855             nuv->priv->time_start = timestamp;
   856         }
   857         nuv->priv->new_audio_segment = FALSE;
   858       }
   859 
   860       break;
   861     }
   862     case 'S':
   863    {
   864       switch (h.i_compression) {
   865         case 'V':
   866           GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
   867           gst_pad_push_event (nuv->priv->src_video_pad,
   868               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
   869               		GST_CLOCK_TIME_NONE, 0));
   870           break;
   871         case 'A':
   872           GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
   873           gst_pad_push_event (nuv->priv->src_audio_pad,
   874               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
   875               		GST_CLOCK_TIME_NONE, 0));
   876           break;
   877         default:
   878           break;
   879       }
   880       goto done;
   881     }
   882     default:
   883       break;
   884   }
   885 
   886   if ((buf != NULL) && (pad != NULL)) {
   887       /* pushing the buffer */
   888       gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
   889       ret = gst_pad_push (pad, buf);
   890       buf = NULL;
   891 
   892       if (ret != GST_FLOW_OK) {
   893         GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
   894 
   895         if (pad == nuv->priv->src_video_pad) {
   896             nuv->priv->last_video_return = ret;
   897         }
   898         else if (pad == nuv->priv->src_audio_pad) {
   899             nuv->priv->last_audio_return = ret;
   900         }
   901 
   902         /* verify anothers flow if is necessary stop task */
   903         if (gst_nuv_combine_flow (nuv) != FALSE) {
   904           ret = GST_FLOW_OK;
   905         } else {
   906 	  GST_WARNING_OBJECT (nuv, "error: on push");
   907 	}
   908 
   909       }
   910   } 
   911 
   912 done:
   913   if (buf != NULL) {
   914       gst_buffer_unref (buf);
   915       buf = NULL;
   916   }
   917   if (ret == GST_FLOW_OK) {
   918      nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
   919      memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
   920   }
   921   return ret;
   922 }
   923 
   924 static GstFlowReturn
   925 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
   926 {
   927   GstFlowReturn ret = GST_FLOW_OK;
   928 
   929   /* ffmpeg extra data */
   930   ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
   931       &nuv->priv->mpeg_buffer);
   932   if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
   933     return ret;
   934   }
   935 
   936   GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
   937   nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   938   return ret;
   939 }
   940 
   941 static GstFlowReturn
   942 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
   943 {
   944   GstFlowReturn ret = GST_FLOW_OK;
   945 
   946   /* Load 'D' */
   947   nuv_frame_header h;
   948 
   949   ret = gst_nuv_demux_frame_header_load (nuv, &h);
   950   if (ret != GST_FLOW_OK)
   951     return ret;
   952 
   953   if (h.i_type != 'D') {
   954     GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
   955     return GST_FLOW_NOT_SUPPORTED;
   956   }
   957 
   958   if (h.i_length > 0) {
   959     if (h.i_compression == 'F') {
   960       nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
   961     } else {
   962       GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
   963       return GST_FLOW_NOT_SUPPORTED;
   964     }
   965   } else {
   966     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   967   }
   968 
   969   return ret;
   970 }
   971 
   972 static GstFlowReturn
   973 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
   974 {
   975   GstFlowReturn ret = GST_FLOW_OK;
   976 
   977   ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
   978   if (ret != GST_FLOW_OK)
   979     return ret;
   980 
   981   gst_nuv_demux_create_pads (nuv);
   982   nuv->priv->state = GST_NUV_DEMUX_INDEX_CREATE;
   983   return ret;
   984 }
   985 
   986 static GstFlowReturn
   987 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
   988 {
   989   GstBuffer *buf = NULL;
   990   GstFlowReturn res = GST_FLOW_OK;
   991 
   992   res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
   993   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   994     if (buf != NULL) {
   995       gst_buffer_unref (buf);
   996     }
   997     return res;
   998   }
   999 
  1000   if (buf->data[0] == 'X') {
  1001     gst_buffer_unref (buf);
  1002     buf = NULL;
  1003     nuv_frame_header h;
  1004 
  1005     res = gst_nuv_demux_frame_header_load (nuv, &h);
  1006     if (res != GST_FLOW_OK)
  1007       return res;
  1008 
  1009     if (h.i_length != 512) {
  1010       return GST_FLOW_ERROR;
  1011     }
  1012     nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
  1013   } else {
  1014     nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
  1015     g_object_unref (buf);
  1016     GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
  1017         (_("incomplete NUV support")), ("incomplete NUV support"));
  1018     return GST_FLOW_ERROR;
  1019   }
  1020   return res;
  1021 }
  1022 
  1023 static void
  1024 gst_nuv_demux_create_seek_index (GstNuvDemux * nuv)
  1025 {
  1026   GstMessage *msg;
  1027   nuv_frame_header h;
  1028 
  1029   while (gst_nuv_demux_frame_header_load (nuv, &h) == GST_FLOW_OK) {
  1030     if ((h.i_type == 'V') && (h.i_keyframe == 0)) {
  1031       frame_index_data *f = g_new0 (frame_index_data, 1);
  1032 
  1033       f->offset = nuv->priv->offset - 12;
  1034       f->timecode = h.i_timecode * GST_MSECOND;
  1035 
  1036       nuv->priv->index = g_slist_append (nuv->priv->index, f);
  1037     } 
  1038     if (h.i_type != 'R') {
  1039       nuv->priv->offset += h.i_length;
  1040 		  if (h.i_type == 'A' || h.i_type == 'V')
  1041   		  nuv->priv->duration_time = h.i_timecode * GST_MSECOND;
  1042     }
  1043   }
  1044   GST_DEBUG_OBJECT  (nuv, "CREATING INDEX: DONE : DURATION Bytes/Sec: %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, 
  1045     nuv->priv->offset, nuv->priv->duration_time);
  1046 
  1047   nuv->priv->duration_bytes = nuv->priv->offset;
  1048   nuv->priv->offset = nuv->priv->header_lengh;
  1049 
  1050   msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_time);
  1051   gst_element_post_message (GST_ELEMENT (nuv), msg);
  1052 }
  1053 
  1054 
  1055 static void 
  1056 gst_nuv_demux_store_keyframe (GstNuvDemux * nuv, nuv_frame_header *h)
  1057 {
  1058    if ((h->i_type == 'V') && (h->i_keyframe == 0)) {
  1059       GSList *lst = g_slist_last (nuv->priv->index);
  1060       if (lst != NULL) {
  1061           frame_index_data *d = (frame_index_data *) lst->data;
  1062           if (d->timecode < h->i_timecode) {
  1063               frame_index_data *f = g_new0 (frame_index_data, 1);
  1064               f->offset = nuv->priv->offset - 12;
  1065               f->timecode = h->i_timecode * GST_MSECOND;
  1066               nuv->priv->index = g_slist_append (nuv->priv->index, f);
  1067           }
  1068       }
  1069    }
  1070 }
  1071 
  1072 
  1073 static GstFlowReturn
  1074 gst_nuv_demux_play (GstPad * pad)
  1075 {
  1076   GstFlowReturn res = GST_FLOW_OK;
  1077   GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
  1078 
  1079   switch (nuv->priv->state) {
  1080     case GST_NUV_DEMUX_START:
  1081       res = gst_nuv_demux_stream_file_header (nuv);
  1082       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1083         goto pause;
  1084       }
  1085       break;
  1086 
  1087     case GST_NUV_DEMUX_HEADER_DATA:
  1088       res = gst_nuv_demux_stream_header_data (nuv);
  1089       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1090         goto pause;
  1091       }
  1092       break;
  1093 
  1094     case GST_NUV_DEMUX_EXTRA_DATA:
  1095       res = gst_nuv_demux_stream_extra_data (nuv);
  1096       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1097         goto pause;
  1098       }
  1099       break;
  1100 
  1101     case GST_NUV_DEMUX_MPEG_DATA:
  1102       res = gst_nuv_demux_stream_mpeg_data (nuv);
  1103       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1104         goto pause;
  1105       }
  1106       break;
  1107 
  1108     case GST_NUV_DEMUX_EXTEND_HEADER:
  1109       res = gst_nuv_demux_stream_extend_header (nuv);
  1110       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1111         goto pause;
  1112       }
  1113       break;
  1114 
  1115     case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
  1116       res = gst_nuv_demux_stream_extend_header_data (nuv);
  1117       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1118         goto pause;
  1119       }
  1120       //store file header size
  1121       nuv->priv->header_lengh = nuv->priv->offset;
  1122       break;
  1123 
  1124     case GST_NUV_DEMUX_INDEX_CREATE:
  1125       if (nuv->priv->mode == NUV_PULL_MODE) {
  1126         gst_nuv_demux_create_seek_index (nuv);
  1127       }
  1128       nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
  1129       break;
  1130 
  1131     case GST_NUV_DEMUX_FRAME_HEADER:
  1132       res = gst_nuv_demux_read_head_frame (nuv);
  1133       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1134         goto pause;
  1135       }
  1136       gst_nuv_demux_store_keyframe (nuv, &nuv->priv->fh);
  1137       break;
  1138 
  1139     case GST_NUV_DEMUX_MOVI: 
  1140       res = gst_nuv_demux_stream_data (nuv);
  1141       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
  1142         goto pause;
  1143       }
  1144       break;
  1145 
  1146     case GST_NUV_DEMUX_INVALID_DATA:
  1147       goto pause;
  1148       break;
  1149     default:
  1150       g_assert_not_reached ();
  1151   }
  1152 
  1153   return GST_FLOW_OK;
  1154 
  1155 pause:
  1156   GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
  1157   gst_pad_pause_task (nuv->priv->sinkpad);
  1158 
  1159   if (res == GST_FLOW_ERROR_EOS) {
  1160     gst_nuv_demux_send_eos (nuv);
  1161     nuv->priv->eos = TRUE;
  1162     res = GST_FLOW_OK;
  1163   }
  1164 
  1165   if (GST_FLOW_IS_FATAL (res)) {
  1166     GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
  1167         (_("Internal data stream error.")),
  1168         ("streaming stopped, reason %s", gst_flow_get_name (res)));
  1169 
  1170     gst_nuv_demux_send_eos (nuv);
  1171   }
  1172   return res;
  1173 }
  1174 
  1175 static void
  1176 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
  1177 {
  1178   gst_element_post_message (GST_ELEMENT (nuv),
  1179       gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
  1180 
  1181   if (nuv->priv->src_video_pad)
  1182     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
  1183   if (nuv->priv->src_audio_pad)
  1184     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
  1185 }
  1186 
  1187 static GstFlowReturn
  1188 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
  1189     GstBuffer ** buffer)
  1190 {
  1191   GstFlowReturn ret = GST_FLOW_OK;
  1192 
  1193   if (size == 0) {
  1194     return ret;
  1195   }
  1196 
  1197   if (nuv->priv->mode == NUV_PULL_MODE) {
  1198     ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
  1199     if (ret == GST_FLOW_OK) {
  1200       if (move) {
  1201         nuv->priv->offset += size;
  1202       }
  1203       /* got eos */
  1204     } else if (ret == GST_FLOW_UNEXPECTED) {
  1205       return GST_FLOW_ERROR_EOS;
  1206     }
  1207   } else {
  1208     if (gst_adapter_available (nuv->priv->adapter) < size) {
  1209       nuv->priv->more_data = TRUE;
  1210       return GST_FLOW_ERROR_NO_DATA;
  1211     }
  1212     if (move) {
  1213       *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
  1214     } else {
  1215       guint8 *data = NULL;
  1216       data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
  1217       *buffer = gst_buffer_new ();
  1218       gst_buffer_set_data (*buffer, data, size);
  1219     }
  1220   }
  1221   return ret;
  1222 }
  1223 
  1224 static gboolean
  1225 gst_nuv_demux_sink_activate (GstPad * sinkpad)
  1226 {
  1227   gboolean res = TRUE;
  1228   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1229  
  1230   if (gst_pad_check_pull_range (sinkpad)) {
  1231     gst_adapter_clear (nuv->priv->adapter);
  1232     res = gst_pad_activate_pull (sinkpad, TRUE);
  1233   } else {
  1234     gst_adapter_clear (nuv->priv->adapter);
  1235     res = gst_pad_activate_push (sinkpad, TRUE);
  1236   }
  1237   
  1238   g_object_unref (nuv);
  1239   return res;
  1240 }
  1241 
  1242 static gboolean
  1243 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
  1244 {
  1245   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
  1246 
  1247   if (active) {
  1248     GST_DEBUG_OBJECT (nuv, "activating pull function");
  1249     nuv->priv->mode = NUV_PULL_MODE;
  1250     gst_adapter_clear (nuv->priv->adapter);
  1251 
  1252     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
  1253   } else {
  1254     GST_DEBUG_OBJECT (nuv, "deactivating pull function");
  1255     gst_pad_stop_task (sinkpad);
  1256   }
  1257   gst_object_unref (nuv);
  1258 
  1259   return TRUE;
  1260 }
  1261 
  1262 static gboolean
  1263 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
  1264 {
  1265   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1266 
  1267   if (active) {
  1268     nuv->priv->mode = NUV_PUSH_MODE;
  1269     gst_adapter_clear (nuv->priv->adapter);
  1270 
  1271     GST_DEBUG_OBJECT (nuv, "activating push/chain function");
  1272   } else {
  1273     GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
  1274   }
  1275 
  1276   gst_object_unref (nuv);
  1277 
  1278   return TRUE;
  1279 }
  1280 
  1281 static frame_index_data *
  1282 gst_nuv_demux_do_seek_index (GstNuvDemux *nuv, gint64 seek_pos, 
  1283   gint64 segment_stop, GstFormat format)
  1284 {
  1285   GSList *l;
  1286   frame_index_data *ret = NULL;
  1287 
  1288   if (nuv->priv->index == NULL) {
  1289     return NULL;
  1290   }
  1291 
  1292   /* find keyframe closest to the requested position  */
  1293   for (l = nuv->priv->index; l != NULL; l = l->next) {
  1294     frame_index_data *f = (frame_index_data *) l->data;  
  1295     gint64 pos = 0;
  1296    
  1297     if (format == GST_FORMAT_BYTES) {     
  1298       pos = f->offset;
  1299     } else if (format == GST_FORMAT_TIME) {
  1300       pos = f->timecode;
  1301     } else {
  1302       return NULL;
  1303     }
  1304 
  1305 	  if (pos >= seek_pos) {
  1306   	  ret = f;
  1307     	break;
  1308     }
  1309     if ((segment_stop != -1) && (pos > segment_stop))
  1310       break;
  1311   }  
  1312 
  1313   return ret;
  1314 }
  1315 
  1316 static gboolean
  1317 gst_nuv_demux_do_seek (GstNuvDemux *nuv, GstEvent * event)
  1318 {
  1319   gdouble rate;
  1320   GstFormat format;
  1321   GstSeekFlags flags;
  1322   GstSeekType cur_type;
  1323   gint64 cur;
  1324   GstSeekType stop_type;
  1325   gint64 stop;
  1326   gboolean flush;
  1327   frame_index_data *entry;
  1328   gint64 segment_start;
  1329   gint64 segment_stop;
  1330   GstEvent *newsegment_event;
  1331 
  1332   if (nuv->priv->eos) {
  1333     return FALSE;
  1334   }
  1335 
  1336 
  1337   gst_event_parse_seek (event, &rate, &format, &flags,
  1338       &cur_type, &cur, &stop_type, &stop);
  1339 
  1340 
  1341 
  1342 /*
  1343   if (format == GST_FORMAT_TIME) {
  1344     GST_DEBUG_OBJECT (nuv, "Can only seek on BYTES");
  1345     return FALSE;
  1346   }
  1347 */
  1348 
  1349   if (rate <= 0.0) {
  1350     GST_DEBUG_OBJECT (nuv, "Can only seek with positive rate");
  1351     return FALSE;
  1352   }
  1353   
  1354   if (cur_type == GST_SEEK_TYPE_SET) {
  1355     GST_OBJECT_LOCK (nuv);
  1356     if (gst_nuv_demux_do_seek_index (nuv, cur, -1, format) == NULL) {
  1357       GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
  1358       GST_OBJECT_UNLOCK (nuv);
  1359       return FALSE;
  1360     }
  1361     GST_OBJECT_UNLOCK (nuv);
  1362   }
  1363   flush = !!(flags & GST_SEEK_FLAG_FLUSH);
  1364   
  1365   if (flush) {
  1366     gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_start ());
  1367     if (nuv->priv->src_video_pad != NULL) {
  1368       gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_start ());
  1369     }
  1370 
  1371     if (nuv->priv->src_audio_pad != NULL) {
  1372       gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_start ());
  1373     }
  1374   }
  1375   else {
  1376 	  gst_pad_pause_task (nuv->priv->sinkpad);
  1377   }
  1378 
  1379   GST_PAD_STREAM_LOCK (nuv->priv->sinkpad);
  1380   GST_OBJECT_LOCK (nuv);
  1381 
  1382 
  1383   if (cur == GST_CLOCK_TIME_NONE)
  1384     cur = 0;
  1385   if (stop == GST_CLOCK_TIME_NONE)
  1386     stop = nuv->priv->duration_time;
  1387 
  1388   if (cur_type == GST_SEEK_TYPE_SET)
  1389     segment_start = cur;
  1390   else if (cur_type == GST_SEEK_TYPE_CUR)
  1391     segment_start = nuv->priv->segment_start + cur;
  1392   else
  1393     segment_start = nuv->priv->segment_start;
  1394 
  1395   if (stop_type == GST_SEEK_TYPE_SET)
  1396     segment_stop = stop;
  1397   else if (stop_type == GST_SEEK_TYPE_CUR)
  1398     segment_stop = nuv->priv->segment_stop + stop;
  1399   else
  1400     segment_stop = nuv->priv->segment_stop;
  1401 
  1402   segment_start = CLAMP (segment_start, 0, nuv->priv->duration_time);
  1403   segment_stop = CLAMP (segment_stop, 0, nuv->priv->duration_time);
  1404 
  1405   entry = gst_nuv_demux_do_seek_index (nuv, segment_start,
  1406       segment_stop, format);
  1407 
  1408   if (entry == NULL) {
  1409     GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
  1410     goto seek_error;
  1411   }
  1412 
  1413   segment_start = entry->timecode;
  1414 
  1415   nuv->priv->segment_start = segment_start;
  1416   nuv->priv->segment_stop = segment_stop;
  1417 
  1418   GST_OBJECT_UNLOCK (nuv);
  1419 
  1420   if (!nuv->priv->eos) {
  1421     GstMessage *msg;
  1422     msg = gst_message_new_segment_start (GST_OBJECT (nuv), GST_FORMAT_TIME,
  1423       nuv->priv->segment_start);
  1424   
  1425     gst_element_post_message (GST_ELEMENT (nuv), msg);
  1426   }
  1427 
  1428 
  1429   GST_DEBUG_OBJECT (nuv, "NEW SEGMENT START %" G_GUINT64_FORMAT ", STOP %" G_GUINT64_FORMAT, 
  1430     segment_start, segment_stop);
  1431   newsegment_event = gst_event_new_new_segment (FALSE, rate,
  1432     GST_FORMAT_TIME, segment_start, segment_stop, segment_start);
  1433 
  1434 
  1435   if (flush) {
  1436     if (nuv->priv->src_video_pad != NULL) {
  1437       gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_stop ());
  1438     }
  1439 
  1440     if (nuv->priv->src_audio_pad != NULL) {
  1441       gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_stop ());
  1442     }
  1443 
  1444     gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_stop ());
  1445   }
  1446 
  1447   
  1448   if (nuv->priv->src_video_pad != NULL) {
  1449     gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
  1450   }
  1451   if (nuv->priv->src_audio_pad != NULL) {
  1452     gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
  1453   } 
  1454 
  1455   gst_event_unref (newsegment_event);
  1456 
  1457   nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
  1458   nuv->priv->offset = entry->offset;
  1459 
  1460   gst_pad_start_task (nuv->priv->sinkpad, (GstTaskFunction) gst_nuv_demux_loop,
  1461       nuv->priv->sinkpad);
  1462 
  1463   GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
  1464   return TRUE;
  1465 
  1466 seek_error:
  1467   GST_DEBUG_OBJECT (nuv, "Got a seek error");
  1468   GST_OBJECT_UNLOCK (nuv);
  1469   GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
  1470   return FALSE;
  1471 
  1472 }
  1473 
  1474 static gboolean
  1475 gst_nuv_demux_srcpad_event (GstPad * pad, GstEvent * event)
  1476 {
  1477   gboolean res = FALSE;
  1478   GstNuvDemux *nuv;
  1479 
  1480   nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1481   
  1482   switch (GST_EVENT_TYPE (event)) {
  1483     case GST_EVENT_SEEK:
  1484       res = gst_nuv_demux_do_seek (nuv, event);
  1485       break;
  1486     default:
  1487       res = FALSE;
  1488       break;
  1489   }
  1490   
  1491   gst_object_unref (nuv);
  1492   return res;
  1493 }
  1494 
  1495 static GstFlowReturn
  1496 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
  1497 {
  1498   GstFlowReturn ret = GST_FLOW_OK;
  1499   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
  1500   
  1501   if (nuv->priv->mode != NUV_PUSH_MODE)
  1502     return ret;
  1503 
  1504   gst_adapter_push (nuv->priv->adapter, buf);  
  1505   
  1506   while ((ret == GST_FLOW_OK) && (nuv->priv->more_data == FALSE)) {
  1507       ret = gst_nuv_demux_play (pad);
  1508   }
  1509 
  1510   nuv->priv->more_data = FALSE;
  1511   gst_object_unref (nuv);
  1512 
  1513   return ret;
  1514 }
  1515 
  1516 static void
  1517 gst_nuv_demux_loop (GstPad * pad)
  1518 {
  1519   gst_nuv_demux_play (pad);
  1520 }
  1521 
  1522 static void
  1523 gst_nuv_demux_index_free (gpointer data, gpointer user_data)
  1524 {
  1525   g_free (data);
  1526 }
  1527 
  1528 static void
  1529 gst_nuv_demux_reset (GstNuvDemux * nuv)
  1530 {
  1531   nuv->priv->eos = FALSE;
  1532   nuv->priv->more_data = FALSE;
  1533   nuv->priv->state = GST_NUV_DEMUX_START;
  1534   nuv->priv->mode = NUV_PUSH_MODE;
  1535   nuv->priv->offset = 0;
  1536   nuv->priv->time_start = 0;
  1537   nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
  1538   nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
  1539   nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
  1540   nuv->priv->last_video_return = GST_FLOW_OK;
  1541   nuv->priv->last_audio_return = GST_FLOW_OK;
  1542   nuv->priv->header_lengh = 0;
  1543   nuv->priv->segment_stop = GST_CLOCK_TIME_NONE;
  1544   nuv->priv->segment_start = GST_CLOCK_TIME_NONE;
  1545 
  1546   //clear index list
  1547   g_slist_foreach (nuv->priv->index, gst_nuv_demux_index_free, NULL);
  1548   g_slist_free (nuv->priv->index);
  1549   nuv->priv->index = NULL;
  1550 
  1551   gst_adapter_clear (nuv->priv->adapter);
  1552 
  1553   if (nuv->priv->mpeg_buffer != NULL) {
  1554     gst_buffer_unref (nuv->priv->mpeg_buffer);
  1555     nuv->priv->mpeg_buffer = NULL;
  1556   }
  1557 }
  1558 
  1559 static void
  1560 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
  1561 {
  1562   if (nuv->priv->src_video_pad) {
  1563     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
  1564     nuv->priv->src_video_pad = NULL;
  1565   }
  1566 
  1567   if (nuv->priv->src_audio_pad) {
  1568     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
  1569     nuv->priv->src_audio_pad = NULL;
  1570   }
  1571 }
  1572 
  1573 static GstStateChangeReturn
  1574 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
  1575 {
  1576   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  1577 
  1578   switch (transition) {
  1579     case GST_STATE_CHANGE_NULL_TO_READY:
  1580       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1581       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1582       break;
  1583     default:
  1584       break;
  1585   }
  1586 
  1587   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1588   if (ret == GST_STATE_CHANGE_FAILURE) {
  1589     goto done;
  1590   }
  1591 
  1592   switch (transition) {
  1593     case GST_STATE_CHANGE_READY_TO_NULL:
  1594       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1595       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1596       break;
  1597     default:
  1598       break;
  1599   }
  1600 
  1601 done:
  1602   return ret;
  1603 }
  1604 
  1605 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
  1606 GstBuffer *
  1607 gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
  1608 {
  1609   GstBuffer *buffer;
  1610   GstBuffer *cur;
  1611   guint8 *data;
  1612 
  1613   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
  1614   g_return_val_if_fail (nbytes > 0, NULL);
  1615 
  1616   GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);
  1617 
  1618   /* we don't have enough data, return NULL. This is unlikely
  1619    * as one usually does an _available() first instead of peeking a
  1620    * random size. */
  1621   if (G_UNLIKELY (nbytes > adapter->size))
  1622     return NULL;
  1623 
  1624   /* our head buffer has enough data left, return it */
  1625   cur = adapter->buflist->data;
  1626   if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) {
  1627     GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
  1628         nbytes);
  1629     buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes);
  1630 
  1631     gst_adapter_flush (adapter, nbytes);
  1632 
  1633     return buffer;
  1634   }
  1635 
  1636   data = gst_adapter_take (adapter, nbytes);
  1637   if (data == NULL)
  1638     return NULL;
  1639 
  1640   buffer = gst_buffer_new ();
  1641   GST_BUFFER_DATA (buffer) = data;
  1642   GST_BUFFER_MALLOCDATA (buffer) = data;
  1643   GST_BUFFER_SIZE (buffer) = nbytes;
  1644 
  1645   return buffer;
  1646 }
  1647 #endif
  1648 
  1649 
  1650 static gboolean
  1651 plugin_init (GstPlugin * plugin)
  1652 {
  1653 #ifdef ENABLE_NLS
  1654   setlocale (LC_ALL, "");
  1655   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  1656 #endif /* ENABLE_NLS */
  1657 
  1658   if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
  1659           GST_TYPE_NUV_DEMUX)) {
  1660     return FALSE;
  1661   }
  1662   return TRUE;
  1663 }
  1664 
  1665 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1666     GST_VERSION_MINOR,
  1667     "nuvdemux",
  1668     "Demuxes and muxes audio and video",
  1669      plugin_init, VERSION, "LGPL", "NuvDemux", "")
  1670