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