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