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