gst-plugins-nuvdemux/nuvdemux/gstnuvdemux.c
author renatofilho
Wed Nov 15 18:11:45 2006 +0000 (2006-11-15)
branchtrunk
changeset 88 2a70ed80ed1a
parent 85 7f6ea4639cb9
child 91 6b1e210c250a
permissions -rw-r--r--
[svn r89]
     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 GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug);
    60 #define GST_CAT_DEFAULT nuvdemux_debug
    61 
    62 
    63 #define GST_FLOW_ERROR_NO_DATA  -101
    64 enum
    65 {
    66    NUV_PUSH_MODE = 0,
    67    NUV_PULL_MODE
    68 };
    69 
    70 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
    71 
    72 static const GstElementDetails gst_nuv_demux_details =
    73 GST_ELEMENT_DETAILS ("Nuv demuxer",
    74     "Codec/Demuxer",
    75     "Demultiplex a .nuv file into audio and video",
    76     "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
    77     "Rosfran Borges <rosfran.borges@indt.org.br>");
    78 
    79 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    80     GST_PAD_SINK,
    81     GST_PAD_ALWAYS,
    82     GST_STATIC_CAPS ("video/x-nuv"));
    83 
    84 static GstStaticPadTemplate audio_src_template =
    85 GST_STATIC_PAD_TEMPLATE ("audio_src",
    86     GST_PAD_SRC,
    87     GST_PAD_SOMETIMES,
    88     GST_STATIC_CAPS_ANY);
    89 
    90 static GstStaticPadTemplate video_src_template =
    91 GST_STATIC_PAD_TEMPLATE ("video_src",
    92     GST_PAD_SRC,
    93     GST_PAD_SOMETIMES,
    94     GST_STATIC_CAPS_ANY);
    95 
    96 static void gst_nuv_demux_finalize (GObject * object);
    97 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
    98     GstStateChange transition);
    99 static void gst_nuv_demux_loop (GstPad * pad);
   100 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
   101 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
   102 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
   103     gboolean active);
   104 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad, 
   105     gboolean active);
   106 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
   107 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
   108     gboolean move, GstBuffer ** buffer);
   109 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
   110 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
   111 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
   112 
   113 /* GObject methods */
   114 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
   115 
   116 #if G_BYTE_ORDER == G_BIG_ENDIAN
   117 static inline gdouble
   118 _gdouble_swap_le_be (gdouble * d)
   119 {
   120   union
   121   {
   122     guint64 i;
   123     gdouble d;
   124   } u;
   125 
   126   u.d = *d;
   127   u.i = GUINT64_SWAP_LE_BE (u.i);
   128   return u.d;
   129 }
   130 
   131 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
   132 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
   133 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
   134 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
   135 
   136 static void 
   137 double2fraction (double in, int *num, int *denom)
   138 {
   139     if (in == 29.97) {
   140         *num = 30000;
   141         *denom = 1001;
   142     } else if (in == 23.976) {
   143         *num = 24000;
   144         *denom = 1001;
   145     } else {
   146         *denom = 1;
   147         while (in - floor(in) >= 0.1) {
   148             *denom *= 10;
   149             in *= 10.0;
   150         }
   151         *num = (int)floor(in);
   152     }
   153 }
   154 
   155 static void
   156 gst_nuv_demux_base_init (gpointer klass)
   157 {
   158   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
   159 
   160   gst_element_class_add_pad_template (element_class,
   161       gst_static_pad_template_get (&audio_src_template));
   162 
   163   gst_element_class_add_pad_template (element_class,
   164       gst_static_pad_template_get (&video_src_template));
   165 
   166   gst_element_class_add_pad_template (element_class,
   167       gst_static_pad_template_get (&sink_template));
   168   gst_element_class_set_details (element_class, &gst_nuv_demux_details);
   169 }
   170 
   171 static void
   172 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
   173 {
   174   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   175   GObjectClass *gobject_class = (GObjectClass *) klass;
   176 
   177   GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
   178       0, "Demuxer for NUV streams");
   179 
   180   parent_class = g_type_class_peek_parent (klass);
   181 
   182   gobject_class->finalize = gst_nuv_demux_finalize;
   183   gstelement_class->change_state = gst_nuv_demux_change_state;
   184 }
   185 
   186 static void
   187 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
   188 {
   189   nuv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
   190 
   191   gst_pad_set_activate_function (nuv->sinkpad, gst_nuv_demux_sink_activate);
   192 
   193   gst_pad_set_activatepull_function (nuv->sinkpad,
   194       gst_nuv_demux_sink_activate_pull);
   195 
   196   gst_pad_set_activatepush_function (nuv->sinkpad, 
   197       gst_nuv_demux_sink_activate_push);
   198 
   199   gst_pad_set_chain_function (nuv->sinkpad,
   200       GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
   201 
   202   gst_element_add_pad (GST_ELEMENT (nuv), nuv->sinkpad);
   203   
   204   nuv->adapter = NULL;
   205   nuv->mpeg_buffer = NULL;
   206   nuv->h = NULL;
   207   nuv->eh = NULL;
   208   nuv->fh = NULL;
   209 
   210   nuv->new_audio_segment = TRUE;
   211   nuv->new_video_segment = TRUE;
   212   
   213   gst_nuv_demux_reset (nuv);
   214 }
   215 
   216 static void
   217 gst_nuv_demux_finalize (GObject * object)
   218 {
   219   GstNuvDemux *nuv = GST_NUV_DEMUX (object);
   220 
   221   if (nuv->mpeg_buffer != NULL) {
   222     gst_buffer_unref (nuv->mpeg_buffer);
   223   }
   224   
   225   gst_nuv_demux_destoy_src_pad (nuv);
   226   gst_nuv_demux_reset (nuv);
   227   if (nuv->adapter != NULL) {
   228     gst_object_unref (nuv->adapter);
   229   }
   230   G_OBJECT_CLASS (parent_class)->finalize (object);
   231 }
   232 
   233 
   234 /* HeaderLoad:
   235  */
   236 static GstFlowReturn
   237 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header ** h_ret)
   238 {
   239   GstBuffer *buffer = NULL;
   240   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
   241 
   242   if ((res != GST_FLOW_OK) || (buffer == NULL))
   243     return res;
   244 
   245   nuv_header *h = g_new0 (nuv_header, 1);
   246 
   247   memcpy (h->id, buffer->data, 12);
   248   memcpy (h->version, buffer->data + 12, 5);
   249   h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
   250   h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
   251   h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
   252   h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
   253   h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
   254   h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
   255   h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
   256   /* get the num and denom values from fps */
   257   double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
   258   h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
   259   h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
   260   h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
   261   h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
   262 
   263   GST_DEBUG_OBJECT (nuv,
   264       "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
   265       h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
   266       h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
   267       h->i_keyframe_distance);
   268 
   269   *h_ret = h;
   270   gst_buffer_unref (buffer);
   271   return res;
   272 }
   273 
   274 static GstFlowReturn
   275 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
   276 {
   277   GstFlowReturn res = gst_nuv_demux_header_load (nuv, &nuv->h);
   278 
   279   if (res == GST_FLOW_OK)
   280     nuv->state = GST_NUV_DEMUX_EXTRA_DATA;
   281   return res;
   282 }
   283 
   284 /*
   285  * Read NUV file tag
   286  */
   287 static GstFlowReturn
   288 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
   289 {
   290   GstFlowReturn res = GST_FLOW_OK;
   291   GstBuffer *file_header = NULL;
   292 
   293   res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
   294   if ((res != GST_FLOW_OK) || (file_header == NULL)) {
   295     return res;
   296   } else {
   297     if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
   298         strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
   299       nuv->state = GST_NUV_DEMUX_HEADER_DATA;
   300     } else {
   301       GST_DEBUG_OBJECT (nuv, "error parsing file header");
   302       nuv->state = GST_NUV_DEMUX_INVALID_DATA;
   303       res = GST_FLOW_ERROR;
   304     }
   305   }
   306   if (file_header != NULL) {
   307     gst_buffer_unref (file_header);
   308   }
   309   return res;
   310 }
   311 
   312 /* FrameHeaderLoad:
   313  */
   314 static GstFlowReturn
   315 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header ** h_ret)
   316 {
   317   unsigned char *data;
   318   nuv_frame_header *h;
   319   GstBuffer *buf = NULL;
   320 
   321   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
   322 
   323   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   324     if (buf != NULL) {
   325       gst_buffer_unref (buf);
   326     }
   327     return res;
   328   }
   329 
   330   h = g_new0 (nuv_frame_header, 1);
   331   data = buf->data;
   332 
   333   h->i_type = GPOINTER_TO_INT (data[0]);
   334   h->i_compression = GPOINTER_TO_INT (data[1]);
   335   h->i_keyframe = GPOINTER_TO_INT (data[2]);
   336   h->i_filters = GPOINTER_TO_INT (data[3]);
   337   h->i_timecode = GST_READ_UINT32_LE (&data[4]);
   338   h->i_length = GST_READ_UINT32_LE (&data[8]);
   339   
   340   GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
   341       h->i_type,
   342       h->i_compression ? h->i_compression : ' ',
   343       h->i_keyframe ? h->i_keyframe : ' ',
   344       h->i_filters, h->i_timecode, h->i_length);
   345 
   346   *h_ret = h;
   347   gst_buffer_unref (buf);
   348   return GST_FLOW_OK;
   349 }
   350 
   351 static GstFlowReturn
   352 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
   353     nuv_extended_header ** h_ret)
   354 {
   355   unsigned char *data;
   356   GstBuffer *buff = NULL;
   357   nuv_extended_header *h;
   358 
   359 
   360   GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
   361 
   362   if ((res != GST_FLOW_OK) || (buff == NULL)) {
   363     if (buff != NULL) {
   364       gst_buffer_unref (buff);
   365     }
   366     return res;
   367   }
   368 
   369   h = g_new0 (nuv_extended_header, 1);
   370   data = buff->data;
   371   h->i_version = GST_READ_UINT32_LE (&data[0]);
   372   h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
   373   h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
   374   h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
   375   h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
   376   h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
   377   h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
   378   h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
   379   h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
   380   h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
   381   h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
   382   h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
   383   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
   384   h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
   385   h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
   386   h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
   387   h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
   388 
   389   GST_DEBUG_OBJECT (nuv,
   390       "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
   391       "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
   392       h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
   393       h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
   394       h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
   395       h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
   396       h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
   397       h->i_keyframe_adjust_offset);
   398 
   399   *h_ret = h;
   400   gst_buffer_unref (buff);
   401   return res;
   402 }
   403 static const GstQueryType *
   404 gst_nuv_demux_get_src_query_types (GstPad * pad)
   405 {
   406   static const GstQueryType src_types[] = {
   407     GST_QUERY_POSITION,
   408     0
   409   };
   410 
   411   return src_types;
   412 }
   413 
   414 static gboolean
   415 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
   416 {
   417   gboolean res = FALSE;
   418   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   419 
   420   switch (GST_QUERY_TYPE (query)) {
   421     case GST_QUERY_POSITION:
   422       if (GST_CLOCK_TIME_IS_VALID (nuv->last_frame_time)) {
   423         gst_query_set_position (query, GST_FORMAT_TIME,
   424             nuv->last_frame_time);
   425         res = TRUE;
   426         GST_DEBUG_OBJECT (nuv, "POS %d", nuv->last_frame_time);
   427       }
   428       break;
   429     default:
   430       res = FALSE;
   431       break;
   432   }
   433 
   434   gst_object_unref (nuv);
   435 
   436   return res;
   437 }
   438 
   439 //TODO: create a function to control events and send to src pads
   440 static void
   441 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
   442 {
   443   if (nuv->h->i_video_blocks != 0) {
   444     GstCaps *video_caps = NULL;
   445 
   446     nuv->src_video_pad =
   447         gst_pad_new_from_static_template (&video_src_template, "video_src");
   448 
   449     video_caps = gst_caps_new_simple ("video/x-divx",
   450         "divxversion", G_TYPE_INT, 4,
   451         "width", G_TYPE_INT, nuv->h->i_width,
   452         "height", G_TYPE_INT, nuv->h->i_height,
   453         "framerate", GST_TYPE_FRACTION, nuv->h->i_fpsn, nuv->h->i_fpsd,
   454         "format", GST_TYPE_FOURCC, nuv->eh->i_video_fcc,
   455         "pixel-aspect-ratio", GST_TYPE_FRACTION,
   456         (gint) (nuv->h->d_aspect * 1000.0f), 1000, NULL);
   457 
   458     gst_pad_use_fixed_caps (nuv->src_video_pad);
   459     gst_pad_set_caps (nuv->src_video_pad, video_caps);
   460     gst_pad_set_active (nuv->src_video_pad, TRUE);
   461     gst_pad_set_query_type_function (nuv->src_video_pad, gst_nuv_demux_get_src_query_types);
   462     gst_pad_set_query_function (nuv->src_video_pad, gst_nuv_demux_handle_src_query);
   463     gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
   464     gst_caps_unref (video_caps);
   465   }
   466 
   467   if (nuv->h->i_audio_blocks != 0) {
   468     GstCaps *audio_caps = NULL;
   469 
   470     nuv->src_audio_pad =
   471         gst_pad_new_from_static_template (&audio_src_template, "audio_src");
   472 
   473     audio_caps = gst_caps_new_simple ("audio/mpeg",
   474         "rate", G_TYPE_INT, nuv->eh->i_audio_sample_rate,
   475         "format", GST_TYPE_FOURCC, nuv->eh->i_audio_fcc,
   476         "channels", G_TYPE_INT, nuv->eh->i_audio_channels,
   477         "mpegversion", G_TYPE_INT, nuv->eh->i_version, NULL);
   478 
   479     gst_pad_use_fixed_caps (nuv->src_audio_pad);
   480     gst_pad_set_caps (nuv->src_audio_pad, audio_caps);
   481     gst_pad_set_active (nuv->src_audio_pad, TRUE);
   482     gst_pad_set_query_type_function (nuv->src_video_pad, gst_nuv_demux_get_src_query_types);
   483     gst_pad_set_query_function (nuv->src_video_pad, gst_nuv_demux_handle_src_query);
   484     gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
   485     gst_caps_unref (audio_caps);
   486   }
   487 
   488   gst_element_no_more_pads (GST_ELEMENT (nuv));
   489 }
   490 
   491 static GstFlowReturn
   492 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
   493 {
   494   GstFlowReturn ret = GST_FLOW_OK;
   495 
   496   if (nuv->fh != NULL) 
   497   {
   498     g_free (nuv->fh);
   499     nuv->fh = NULL;
   500   }
   501   
   502   ret = gst_nuv_demux_frame_header_load (nuv, &nuv->fh);
   503   if (ret != GST_FLOW_OK)
   504     return ret;
   505 
   506   nuv->state = GST_NUV_DEMUX_MOVI;
   507   return ret;
   508 }
   509 
   510 static gboolean
   511 gst_nuv_combine_flow (GstNuvDemux *nuv)
   512 {
   513     GstFlowReturn ret_video = nuv->last_video_return;
   514     GstFlowReturn ret_audio = nuv->last_audio_return;
   515 
   516     if ((ret_video != GST_FLOW_OK) &&
   517         (ret_audio != GST_FLOW_OK))
   518         return FALSE;
   519 
   520     if (GST_FLOW_IS_FATAL (nuv->last_video_return))
   521         return FALSE;
   522 
   523     if (GST_FLOW_IS_FATAL (nuv->last_audio_return))
   524         return FALSE;
   525 
   526     return TRUE;    
   527 }
   528 
   529 static GstFlowReturn
   530 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
   531 {
   532   GstFlowReturn ret = GST_FLOW_OK;
   533   GstBuffer *buf = NULL;
   534   nuv_frame_header *h = nuv->fh;
   535 
   536   if (h->i_type == 'R')
   537     goto done;
   538     
   539   if (h->i_length > 0) {
   540 	  ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
   541 	  if ((ret != GST_FLOW_OK) || (buf == NULL))
   542 		  return ret;
   543 
   544       if (h->i_timecode < 0) {
   545           h->i_timecode = h->i_timecode * -1;
   546           nuv->time_offset = h->i_timecode;
   547       }
   548       else
   549           h->i_timecode += nuv->time_offset;
   550       
   551       GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND;
   552       nuv->last_frame_time = h->i_timecode * GST_MSECOND;
   553   }
   554 
   555   switch (h->i_type) {
   556     case 'V':
   557     {
   558       if (h->i_length == 0)
   559         break;
   560         
   561       if (nuv->new_video_segment) {
   562         /* send new segment event*/
   563         gst_pad_push_event (nuv->src_video_pad,
   564           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, 
   565                                      GST_CLOCK_TIME_NONE, 0));
   566         nuv->new_video_segment = FALSE;
   567       }
   568       
   569       GST_BUFFER_SIZE (buf) = h->i_length;
   570       gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_video_pad));
   571       nuv->last_video_return = gst_pad_push (nuv->src_video_pad, buf);
   572       if (!gst_nuv_combine_flow (nuv)) {
   573         ret = nuv->last_video_return;
   574         GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s, is linked? = %d",
   575             nuv->last_video_return, gst_pad_get_name (nuv->src_video_pad), gst_pad_is_linked (nuv->src_video_pad));
   576       }
   577       break;
   578     }
   579     case 'A':
   580     {
   581       if (h->i_length == 0)
   582         break;
   583   
   584       if (nuv->new_audio_segment) {
   585         /* send new segment event*/
   586         gst_pad_push_event (nuv->src_audio_pad,
   587           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, 
   588                                      GST_CLOCK_TIME_NONE, 0));
   589         nuv->new_audio_segment = FALSE;
   590       }
   591        
   592       GST_BUFFER_SIZE (buf) = h->i_length;
   593       gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_audio_pad));
   594       nuv->last_audio_return = gst_pad_push (nuv->src_audio_pad, buf);
   595       if (!gst_nuv_combine_flow (nuv)) {
   596         ret = nuv->last_audio_return;
   597         GST_WARNING_OBJECT (nuv, "Error %d pushing on srcpad %s, is linked? = %d",
   598             nuv->last_audio_return, gst_pad_get_name (nuv->src_audio_pad), gst_pad_is_linked (nuv->src_audio_pad));
   599       }
   600       break;
   601     }
   602     case 'S':
   603     {
   604       switch (h->i_compression) {
   605         case 'V':
   606 		  if ( !gst_pad_is_linked( nuv->src_video_pad ) )
   607 		    break;
   608 
   609           GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h->i_timecode);
   610           gst_pad_push_event (nuv->src_video_pad,
   611               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h->i_timecode * GST_MSECOND, 
   612               		GST_CLOCK_TIME_NONE, 0));
   613           break;
   614         case 'A':        
   615 		      if ( !gst_pad_is_linked( nuv->src_audio_pad ) )
   616 		      	break;
   617 
   618           GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h->i_timecode);
   619           gst_pad_push_event (nuv->src_audio_pad,
   620               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, 
   621               		GST_CLOCK_TIME_NONE, 0));
   622           break;
   623         default:
   624           break;
   625       }
   626       if (buf != NULL)
   627         gst_buffer_unref (buf);
   628     }
   629     default:
   630       if (buf != NULL)
   631         gst_buffer_unref (buf);
   632 
   633       break;
   634   }
   635 
   636 done:
   637   nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
   638   g_free (nuv->fh);
   639   nuv->fh = NULL;
   640   return ret;
   641 }
   642 
   643 static GstFlowReturn
   644 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
   645 {
   646   GstFlowReturn ret = GST_FLOW_OK;
   647 
   648   /* ffmpeg extra data */
   649   ret =
   650       gst_nuv_demux_read_bytes (nuv, nuv->mpeg_data_size, TRUE,
   651       &nuv->mpeg_buffer);
   652   if ((ret != GST_FLOW_OK) || (nuv->mpeg_buffer == NULL)) {
   653     return ret; 
   654   }
   655   GST_BUFFER_SIZE (nuv->mpeg_buffer) = nuv->mpeg_data_size;
   656   nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   657   return ret;
   658 }
   659 
   660 static GstFlowReturn
   661 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
   662 {
   663   GstFlowReturn ret = GST_FLOW_OK;
   664 
   665   /* Load 'D' */
   666   nuv_frame_header *h;
   667 
   668   ret = gst_nuv_demux_frame_header_load (nuv, &h);
   669   if (ret != GST_FLOW_OK)
   670     return ret;
   671 
   672   if (h->i_type != 'D') {
   673     g_free (h);
   674     return GST_FLOW_ERROR;
   675   }
   676 
   677   if (h->i_length > 0) {
   678     if (h->i_compression == 'F') {
   679       nuv->state = GST_NUV_DEMUX_MPEG_DATA;
   680     } else {
   681       g_free (h);
   682       return GST_FLOW_ERROR;
   683     }
   684   } else {
   685     nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   686   }
   687 
   688   g_free (h);
   689   h = NULL;
   690   return ret;
   691 }
   692 
   693 static GstFlowReturn
   694 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
   695 {
   696   GstFlowReturn ret = GST_FLOW_OK;
   697 
   698   ret = gst_nuv_demux_extended_header_load (nuv, &nuv->eh);
   699   if (ret != GST_FLOW_OK)
   700     return ret;
   701 
   702   gst_nuv_demux_create_pads (nuv);
   703   nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
   704   return ret;
   705 }
   706 
   707 static GstFlowReturn
   708 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
   709 {
   710   GstBuffer *buf = NULL;
   711   GstFlowReturn res = GST_FLOW_OK;
   712 
   713   res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
   714   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   715     if (buf != NULL) {
   716       gst_buffer_unref (buf);
   717     }
   718     return res;
   719   }
   720 
   721   if (buf->data[0] == 'X') {
   722     gst_buffer_unref (buf);
   723     buf = NULL;
   724     nuv_frame_header *h = NULL;
   725 
   726     res = gst_nuv_demux_frame_header_load (nuv, &h);
   727     if (res != GST_FLOW_OK)
   728       return res;
   729 
   730     if (h->i_length != 512) {
   731       g_free (h);
   732       return GST_FLOW_ERROR;
   733     }
   734     g_free (h);
   735     h = NULL;
   736     nuv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
   737   } else {
   738     nuv->state = GST_NUV_DEMUX_INVALID_DATA;
   739     g_object_unref (buf);
   740     GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
   741         (_("incomplete NUV support")), ("incomplete NUV support"));
   742     return GST_FLOW_ERROR;
   743   }
   744   return res;
   745 }
   746 
   747 static GstFlowReturn
   748 gst_nuv_demux_play (GstPad * pad)
   749 {
   750   GstFlowReturn res = GST_FLOW_OK;
   751   GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
   752 
   753   switch (nuv->state) {
   754     case GST_NUV_DEMUX_START:
   755       res = gst_nuv_demux_stream_file_header (nuv);
   756       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   757         goto pause;
   758       }
   759       if (nuv->state != GST_NUV_DEMUX_HEADER_DATA)
   760         break;
   761 
   762     case GST_NUV_DEMUX_HEADER_DATA:
   763       res = gst_nuv_demux_stream_header_data (nuv);
   764       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   765         goto pause;
   766       }
   767       if (nuv->state != GST_NUV_DEMUX_EXTRA_DATA)
   768         break;
   769 
   770     case GST_NUV_DEMUX_EXTRA_DATA:
   771       res = gst_nuv_demux_stream_extra_data (nuv);
   772       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   773         goto pause;
   774       }
   775       if (nuv->state != GST_NUV_DEMUX_MPEG_DATA)
   776         break;
   777 
   778     case GST_NUV_DEMUX_MPEG_DATA:
   779       res = gst_nuv_demux_stream_mpeg_data (nuv);
   780       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   781         goto pause;
   782       }
   783 
   784       if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER)
   785         break;
   786 
   787     case GST_NUV_DEMUX_EXTEND_HEADER:
   788       res = gst_nuv_demux_stream_extend_header (nuv);
   789       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   790         goto pause;
   791       }
   792       if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER_DATA)
   793         break;
   794 
   795     case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
   796       res = gst_nuv_demux_stream_extend_header_data (nuv);
   797       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   798         goto pause;
   799       }
   800 
   801       if (nuv->state != GST_NUV_DEMUX_FRAME_HEADER)
   802         break;
   803 
   804     case GST_NUV_DEMUX_FRAME_HEADER:
   805       res = gst_nuv_demux_read_head_frame (nuv);
   806       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   807         goto pause;
   808       }
   809       if (nuv->state != GST_NUV_DEMUX_MOVI)
   810         break;
   811 
   812     case GST_NUV_DEMUX_MOVI:
   813       res = gst_nuv_demux_stream_data (nuv);
   814       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   815         goto pause;
   816       }
   817       break;
   818     case GST_NUV_DEMUX_INVALID_DATA:
   819       goto pause;
   820       break;
   821     default:
   822       g_assert_not_reached ();
   823   }
   824 
   825   GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->state,
   826       gst_flow_get_name (res));
   827 
   828   return GST_FLOW_OK;
   829 
   830 pause:
   831   GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
   832   gst_pad_pause_task (nuv->sinkpad);
   833   if (GST_FLOW_IS_FATAL (res)) {
   834     GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
   835         (_("Internal data stream error.")),
   836         ("streaming stopped, reason %s", gst_flow_get_name (res)));
   837 
   838     gst_nuv_demux_send_eos (nuv);
   839   }
   840   return res;
   841 }
   842 
   843 static void
   844 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
   845 {
   846   gst_element_post_message (GST_ELEMENT (nuv),
   847       gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
   848 
   849   if (nuv->src_video_pad)
   850     gst_pad_push_event (nuv->src_video_pad, gst_event_new_eos ());
   851   if (nuv->src_audio_pad)
   852     gst_pad_push_event (nuv->src_audio_pad, gst_event_new_eos ());
   853 }
   854     
   855 static GstFlowReturn
   856 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
   857     GstBuffer ** buffer)
   858 {
   859   GstFlowReturn ret = GST_FLOW_OK;
   860 
   861   if (size == 0) {
   862     return ret;
   863   }
   864   
   865   if (nuv->mode == NUV_PULL_MODE) {
   866     ret = gst_pad_pull_range (nuv->sinkpad, nuv->offset, size, buffer);
   867     if (ret == GST_FLOW_OK) {
   868         if (move) {
   869 		    nuv->offset += size;
   870 		}
   871       /* got eos */
   872     } else if (ret == GST_FLOW_UNEXPECTED) {
   873       if (buffer != NULL)
   874           gst_buffer_unref (buffer);
   875       
   876       gst_nuv_demux_send_eos (nuv);
   877       return GST_FLOW_WRONG_STATE;
   878     }
   879   } else {
   880     if (gst_adapter_available (nuv->adapter) < size)
   881       return GST_FLOW_ERROR_NO_DATA;
   882 
   883     if (move) {
   884       guint8 *data = NULL;
   885       data = (guint8 *) gst_adapter_take (nuv->adapter, size);
   886       *buffer = gst_buffer_new ();
   887       gst_buffer_set_data (*buffer, data, size);
   888     } else {
   889       guint8 *data = NULL;
   890       data = (guint8 *) gst_adapter_peek (nuv->adapter, size);
   891       *buffer = gst_buffer_new ();
   892       gst_buffer_set_data (*buffer, data, size);
   893     }
   894   }
   895   return ret;
   896 }
   897 
   898 static gboolean
   899 gst_nuv_demux_sink_activate (GstPad * sinkpad)
   900 {
   901   gboolean res = TRUE;
   902   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
   903 
   904   if (gst_pad_check_pull_range (sinkpad)) {
   905     res = gst_pad_activate_pull (sinkpad, TRUE);
   906   } else {
   907     res = gst_pad_activate_push (sinkpad, TRUE);
   908   }
   909   g_object_unref (nuv);
   910   return res;
   911 }
   912 
   913 static gboolean
   914 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
   915 {
   916   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
   917 
   918   if (active) {
   919     GST_DEBUG_OBJECT (nuv, "activating pull function");
   920     nuv->mode = NUV_PULL_MODE;
   921     if (nuv->adapter) {
   922       gst_adapter_clear (nuv->adapter);
   923       g_object_unref (nuv->adapter);
   924       nuv->adapter = NULL;
   925     }
   926     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
   927   } else {
   928     GST_DEBUG_OBJECT (nuv, "deactivating pull function");
   929     gst_pad_stop_task (sinkpad);
   930   }
   931   gst_object_unref (nuv);
   932 
   933   return TRUE;
   934 }
   935 
   936 static gboolean
   937 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
   938 {
   939   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   940 
   941   if (active) {
   942     nuv->mode = NUV_PUSH_MODE;
   943     if (nuv->adapter) {
   944       gst_adapter_clear (nuv->adapter);
   945     } else {
   946       nuv->adapter = gst_adapter_new ();
   947     }
   948     GST_DEBUG_OBJECT (nuv, "activating push/chain function");
   949   } else {
   950     GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
   951   }
   952 
   953   gst_object_unref (nuv);
   954 
   955   return TRUE;
   956 }
   957 
   958 static GstFlowReturn
   959 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
   960 {
   961   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   962 
   963   GST_DEBUG_OBJECT (nuv, " gst_nuv_demux_chain function");
   964   gst_adapter_push (nuv->adapter, buf);
   965 
   966   gst_object_unref (nuv);
   967 
   968   return gst_nuv_demux_play (pad);
   969 }
   970 
   971 static void
   972 gst_nuv_demux_loop (GstPad * pad)
   973 {
   974   gst_nuv_demux_play (pad);
   975 }
   976 
   977 static void
   978 gst_nuv_demux_reset (GstNuvDemux * nuv)
   979 {
   980   nuv->state = GST_NUV_DEMUX_START;
   981   nuv->mode = 0;
   982   nuv->offset = 0;
   983   nuv->time_offset = 0;
   984 
   985   if (nuv->adapter != NULL)
   986     gst_adapter_clear (nuv->adapter);
   987 
   988   if (nuv->mpeg_buffer != NULL) {
   989     gst_buffer_unref (nuv->mpeg_buffer);
   990     nuv->mpeg_buffer = NULL;
   991   }
   992 
   993   g_free (nuv->h);
   994   nuv->h = NULL;
   995 
   996   g_free (nuv->eh);
   997   nuv->eh = NULL;
   998 
   999   g_free (nuv->fh);
  1000   nuv->fh = NULL;
  1001 }
  1002 
  1003 static void
  1004 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
  1005 {
  1006   if (nuv->src_video_pad) {
  1007     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
  1008     nuv->src_video_pad = NULL;
  1009   }
  1010 
  1011   if (nuv->src_audio_pad) {
  1012     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
  1013     nuv->src_audio_pad = NULL;
  1014   }
  1015 }
  1016 
  1017 static GstStateChangeReturn
  1018 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
  1019 {
  1020   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  1021 
  1022   switch (transition) {
  1023     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1024       break;
  1025     default:
  1026       break;
  1027   }
  1028 
  1029   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1030   if (ret == GST_STATE_CHANGE_FAILURE)
  1031     goto done;
  1032 
  1033   switch (transition) {
  1034     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1035       GST_DEBUG_OBJECT (element, "GST_STATE_CHANGE_PAUSED_TO_READY");
  1036       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1037       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1038       break;
  1039     default:
  1040       break;
  1041   }
  1042 
  1043 done:
  1044   return ret;
  1045 }
  1046 
  1047 static gboolean
  1048 plugin_init (GstPlugin * plugin)
  1049 {
  1050 #ifdef ENABLE_NLS
  1051   setlocale (LC_ALL, "");
  1052   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  1053 #endif /* ENABLE_NLS */
  1054 
  1055   if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
  1056           GST_TYPE_NUV_DEMUX)) {
  1057     return FALSE;
  1058   }
  1059   return TRUE;
  1060 }
  1061 
  1062 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1063     GST_VERSION_MINOR,
  1064     "nuvdemux",
  1065     "Demuxes and muxes audio and video",
  1066     plugin_init, VERSION, "LGPL", "NuvDemux", "")