gst-plugins-nuvdemux/nuvdemux/gstnuvdemux.c
author rosfran
Wed Nov 15 18:07:23 2006 +0000 (2006-11-15)
branchtrunk
changeset 87 9ea342c364de
parent 84 8b867c828e89
child 88 2a70ed80ed1a
permissions -rw-r--r--
[svn r88] Fixes the autotools prefix directory, added do_seek callback to the source.
     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     GST_QUERY_DURATION,
   409     0
   410   };
   411 
   412   return src_types;
   413 }
   414 
   415 static gboolean
   416 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
   417 {
   418   gboolean res = FALSE;
   419   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   420 
   421   switch (GST_QUERY_TYPE (query)) {
   422     case GST_QUERY_POSITION:
   423       if (GST_CLOCK_TIME_IS_VALID (nuv->last_frame_time)) {
   424         gst_query_set_position (query, GST_FORMAT_TIME,
   425             nuv->last_frame_time);
   426         res = TRUE;
   427         GST_DEBUG_OBJECT (nuv, "POS %d", nuv->last_frame_time);
   428       }
   429       break;
   430     case GST_QUERY_DURATION:
   431       if (nuv->duration != 0) {
   432         gint64 total;
   433         gint64 fps;
   434         
   435         fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
   436         total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
   437         gst_query_set_duration (query, GST_FORMAT_TIME, total);
   438         GST_DEBUG_OBJECT (nuv, "DURATION %d", total);
   439         res = TRUE;
   440       }
   441       break;
   442     default:
   443       res = FALSE;
   444       break;
   445   }
   446 
   447   gst_object_unref (nuv);
   448 
   449   return res;
   450 }
   451 
   452 //TODO: create a function to control events and send to src pads
   453 static void
   454 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
   455 {
   456   if (nuv->h->i_video_blocks != 0) {
   457     GstCaps *video_caps = NULL;
   458 
   459     nuv->src_video_pad =
   460         gst_pad_new_from_static_template (&video_src_template, "video_src");
   461 
   462     video_caps = gst_caps_new_simple ("video/x-divx",
   463         "divxversion", G_TYPE_INT, 4,
   464         "width", G_TYPE_INT, nuv->h->i_width,
   465         "height", G_TYPE_INT, nuv->h->i_height,
   466         "framerate", GST_TYPE_FRACTION, nuv->h->i_fpsn, nuv->h->i_fpsd,
   467         "format", GST_TYPE_FOURCC, nuv->eh->i_video_fcc,
   468         "pixel-aspect-ratio", GST_TYPE_FRACTION,
   469         (gint) (nuv->h->d_aspect * 1000.0f), 1000, NULL);
   470 
   471     gst_pad_use_fixed_caps (nuv->src_video_pad);
   472     gst_pad_set_caps (nuv->src_video_pad, video_caps);
   473     gst_pad_set_active (nuv->src_video_pad, TRUE);
   474     gst_pad_set_query_type_function (nuv->src_video_pad, gst_nuv_demux_get_src_query_types);
   475     gst_pad_set_query_function (nuv->src_video_pad, gst_nuv_demux_handle_src_query);
   476     gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
   477     gst_caps_unref (video_caps);
   478   }
   479 
   480   if (nuv->h->i_audio_blocks != 0) {
   481     GstCaps *audio_caps = NULL;
   482 
   483     nuv->src_audio_pad =
   484         gst_pad_new_from_static_template (&audio_src_template, "audio_src");
   485 
   486     audio_caps = gst_caps_new_simple ("audio/mpeg",
   487         "rate", G_TYPE_INT, nuv->eh->i_audio_sample_rate,
   488         "format", GST_TYPE_FOURCC, nuv->eh->i_audio_fcc,
   489         "channels", G_TYPE_INT, nuv->eh->i_audio_channels,
   490         "mpegversion", G_TYPE_INT, nuv->eh->i_version, NULL);
   491 
   492     gst_pad_use_fixed_caps (nuv->src_audio_pad);
   493     gst_pad_set_caps (nuv->src_audio_pad, audio_caps);
   494     gst_pad_set_active (nuv->src_audio_pad, TRUE);
   495     gst_pad_set_query_type_function (nuv->src_video_pad, gst_nuv_demux_get_src_query_types);
   496     gst_pad_set_query_function (nuv->src_video_pad, gst_nuv_demux_handle_src_query);
   497     gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
   498     gst_caps_unref (audio_caps);
   499   }
   500 
   501   gst_element_no_more_pads (GST_ELEMENT (nuv));
   502 }
   503 
   504 static GstFlowReturn
   505 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
   506 {
   507   GstFlowReturn ret = GST_FLOW_OK;
   508 
   509   if (nuv->fh != NULL) 
   510   {
   511     g_free (nuv->fh);
   512     nuv->fh = NULL;
   513   }
   514   
   515   ret = gst_nuv_demux_frame_header_load (nuv, &nuv->fh);
   516   if (ret != GST_FLOW_OK)
   517     return ret;
   518 
   519   nuv->state = GST_NUV_DEMUX_MOVI;
   520   return ret;
   521 }
   522 
   523 static GstFlowReturn
   524 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
   525 {
   526   GstFlowReturn ret = GST_FLOW_OK;
   527   GstBuffer *buf = NULL;
   528   nuv_frame_header *h = nuv->fh;
   529 
   530   if (h->i_type == 'R')
   531     goto done;
   532     
   533   if (h->i_length > 0) {
   534 	  ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
   535 	  if ((ret != GST_FLOW_OK) || (buf == NULL))
   536 		  return ret;
   537 
   538       if (h->i_timecode < 0) {
   539           h->i_timecode = h->i_timecode * -1;
   540           nuv->time_offset = h->i_timecode;
   541       }
   542       else
   543           h->i_timecode += nuv->time_offset;
   544       
   545       GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND;
   546       nuv->last_frame_time = h->i_timecode * GST_MSECOND;
   547   }
   548 
   549   switch (h->i_type) {
   550     case 'V':
   551     {
   552       if (h->i_length == 0)
   553         break;
   554         
   555       if (!gst_pad_is_linked (nuv->src_video_pad)) {
   556         gst_buffer_unref (buf);
   557       	break;
   558       }
   559       
   560       if (nuv->new_video_segment) {
   561         /* send new segment event*/
   562         gst_pad_push_event (nuv->src_video_pad,
   563           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, 
   564                                      GST_CLOCK_TIME_NONE, 0));
   565         nuv->new_video_segment = FALSE;
   566       }
   567       
   568       GST_BUFFER_SIZE (buf) = h->i_length;
   569       gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_video_pad));
   570       ret = gst_pad_push (nuv->src_video_pad, buf);
   571       if (ret != GST_FLOW_OK) {
   572         GST_WARNING_OBJECT (nuv, "error pushing on srcpad %s, is linked? = %d",
   573             gst_pad_get_name (nuv->src_video_pad), gst_pad_is_linked (nuv->src_video_pad));
   574       }
   575       break;
   576     }
   577     case 'A':
   578     {
   579       if (h->i_length == 0)
   580         break;
   581         
   582       if (!gst_pad_is_linked (nuv->src_audio_pad)) {
   583         gst_buffer_unref (buf);
   584       	break;
   585       }
   586   
   587       if (nuv->new_audio_segment) {
   588         /* send new segment event*/
   589         gst_pad_push_event (nuv->src_audio_pad,
   590           gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, 
   591                                      GST_CLOCK_TIME_NONE, 0));
   592         nuv->new_audio_segment = FALSE;
   593       }
   594        
   595       GST_BUFFER_SIZE (buf) = h->i_length;
   596       gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_audio_pad));
   597       ret = gst_pad_push (nuv->src_audio_pad, buf);
   598       if (ret != GST_FLOW_OK) {
   599         GST_WARNING_OBJECT (nuv, "Error pushing on srcpad %s, is linked? = %d",
   600             gst_pad_get_name (nuv->src_audio_pad), gst_pad_is_linked (nuv->src_audio_pad));
   601       }
   602       break;
   603     }
   604     case 'S':
   605     {
   606       switch (h->i_compression) {
   607         case 'V':
   608 		  if ( !gst_pad_is_linked( nuv->src_video_pad ) )
   609 		    break;
   610 
   611           GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h->i_timecode);
   612           gst_pad_push_event (nuv->src_video_pad,
   613               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h->i_timecode * GST_MSECOND, 
   614               		GST_CLOCK_TIME_NONE, 0));
   615           break;
   616         case 'A':        
   617 		      if ( !gst_pad_is_linked( nuv->src_audio_pad ) )
   618 		      	break;
   619 
   620           GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h->i_timecode);
   621           gst_pad_push_event (nuv->src_audio_pad,
   622               gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, 
   623               		GST_CLOCK_TIME_NONE, 0));
   624           break;
   625         default:
   626           break;
   627       }
   628       if (buf != NULL)
   629         gst_buffer_unref (buf);
   630     }
   631     default:
   632       if (buf != NULL)
   633         gst_buffer_unref (buf);
   634 
   635       break;
   636   }
   637 
   638 done:
   639   nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
   640   g_free (nuv->fh);
   641   nuv->fh = NULL;
   642   return ret;
   643 }
   644 
   645 static GstFlowReturn
   646 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
   647 {
   648   GstFlowReturn ret = GST_FLOW_OK;
   649 
   650   /* ffmpeg extra data */
   651   ret =
   652       gst_nuv_demux_read_bytes (nuv, nuv->mpeg_data_size, TRUE,
   653       &nuv->mpeg_buffer);
   654   if ((ret != GST_FLOW_OK) || (nuv->mpeg_buffer == NULL)) {
   655     return ret; 
   656   }
   657   GST_BUFFER_SIZE (nuv->mpeg_buffer) = nuv->mpeg_data_size;
   658   nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   659   return ret;
   660 }
   661 
   662 static GstFlowReturn
   663 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
   664 {
   665   GstFlowReturn ret = GST_FLOW_OK;
   666 
   667   /* Load 'D' */
   668   nuv_frame_header *h;
   669 
   670   ret = gst_nuv_demux_frame_header_load (nuv, &h);
   671   if (ret != GST_FLOW_OK)
   672     return ret;
   673 
   674   if (h->i_type != 'D') {
   675     g_free (h);
   676     return GST_FLOW_ERROR;
   677   }
   678 
   679   if (h->i_length > 0) {
   680     if (h->i_compression == 'F') {
   681       nuv->state = GST_NUV_DEMUX_MPEG_DATA;
   682     } else {
   683       g_free (h);
   684       return GST_FLOW_ERROR;
   685     }
   686   } else {
   687     nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
   688   }
   689 
   690   g_free (h);
   691   h = NULL;
   692   return ret;
   693 }
   694 
   695 static GstFlowReturn
   696 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
   697 {
   698   GstFlowReturn ret = GST_FLOW_OK;
   699 
   700   ret = gst_nuv_demux_extended_header_load (nuv, &nuv->eh);
   701   if (ret != GST_FLOW_OK)
   702     return ret;
   703 
   704   gst_nuv_demux_create_pads (nuv);
   705   nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
   706   return ret;
   707 }
   708 
   709 static GstFlowReturn
   710 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
   711 {
   712   GstBuffer *buf = NULL;
   713   GstFlowReturn res = GST_FLOW_OK;
   714 
   715   res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
   716   if ((res != GST_FLOW_OK) || (buf == NULL)) {
   717     if (buf != NULL) {
   718       gst_buffer_unref (buf);
   719     }
   720     return res;
   721   }
   722 
   723   if (buf->data[0] == 'X') {
   724     gst_buffer_unref (buf);
   725     buf = NULL;
   726     nuv_frame_header *h = NULL;
   727 
   728     res = gst_nuv_demux_frame_header_load (nuv, &h);
   729     if (res != GST_FLOW_OK)
   730       return res;
   731 
   732     if (h->i_length != 512) {
   733       g_free (h);
   734       return GST_FLOW_ERROR;
   735     }
   736     g_free (h);
   737     h = NULL;
   738     nuv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
   739   } else {
   740     nuv->state = GST_NUV_DEMUX_INVALID_DATA;
   741     g_object_unref (buf);
   742     GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
   743         (_("incomplete NUV support")), ("incomplete NUV support"));
   744     return GST_FLOW_ERROR;
   745   }
   746   return res;
   747 }
   748 
   749 static GstFlowReturn
   750 gst_nuv_demux_play (GstPad * pad)
   751 {
   752   GstFlowReturn res = GST_FLOW_OK;
   753   GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
   754 
   755   switch (nuv->state) {
   756     case GST_NUV_DEMUX_START:
   757       res = gst_nuv_demux_stream_file_header (nuv);
   758       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   759         goto pause;
   760       }
   761       if (nuv->state != GST_NUV_DEMUX_HEADER_DATA)
   762         break;
   763 
   764     case GST_NUV_DEMUX_HEADER_DATA:
   765       res = gst_nuv_demux_stream_header_data (nuv);
   766       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   767         goto pause;
   768       }
   769       if (nuv->state != GST_NUV_DEMUX_EXTRA_DATA)
   770         break;
   771 
   772     case GST_NUV_DEMUX_EXTRA_DATA:
   773       res = gst_nuv_demux_stream_extra_data (nuv);
   774       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   775         goto pause;
   776       }
   777       if (nuv->state != GST_NUV_DEMUX_MPEG_DATA)
   778         break;
   779 
   780     case GST_NUV_DEMUX_MPEG_DATA:
   781       res = gst_nuv_demux_stream_mpeg_data (nuv);
   782       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   783         goto pause;
   784       }
   785 
   786       if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER)
   787         break;
   788 
   789     case GST_NUV_DEMUX_EXTEND_HEADER:
   790       res = gst_nuv_demux_stream_extend_header (nuv);
   791       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   792         goto pause;
   793       }
   794       if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER_DATA)
   795         break;
   796 
   797     case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
   798       res = gst_nuv_demux_stream_extend_header_data (nuv);
   799       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   800         goto pause;
   801       }
   802 
   803       if (nuv->state != GST_NUV_DEMUX_FRAME_HEADER)
   804         break;
   805 
   806     case GST_NUV_DEMUX_FRAME_HEADER:
   807       res = gst_nuv_demux_read_head_frame (nuv);
   808       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   809         goto pause;
   810       }
   811       if (nuv->state != GST_NUV_DEMUX_MOVI)
   812         break;
   813 
   814     case GST_NUV_DEMUX_MOVI:
   815       res = gst_nuv_demux_stream_data (nuv);
   816       if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
   817         goto pause;
   818       }
   819       break;
   820     case GST_NUV_DEMUX_INVALID_DATA:
   821       goto pause;
   822       break;
   823     default:
   824       g_assert_not_reached ();
   825   }
   826 
   827   GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->state,
   828       gst_flow_get_name (res));
   829 
   830   return GST_FLOW_OK;
   831 
   832 pause:
   833   GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
   834   gst_pad_pause_task (nuv->sinkpad);
   835   if (GST_FLOW_IS_FATAL (res)) {
   836     GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
   837         (_("Internal data stream error.")),
   838         ("streaming stopped, reason %s", gst_flow_get_name (res)));
   839 
   840     gst_nuv_demux_send_eos (nuv);
   841   }
   842   return res;
   843 }
   844 
   845 static void
   846 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
   847 {
   848   gst_element_post_message (GST_ELEMENT (nuv),
   849       gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
   850 
   851   if (nuv->src_video_pad)
   852     gst_pad_push_event (nuv->src_video_pad, gst_event_new_eos ());
   853   if (nuv->src_audio_pad)
   854     gst_pad_push_event (nuv->src_audio_pad, gst_event_new_eos ());
   855 }
   856     
   857 static GstFlowReturn
   858 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
   859     GstBuffer ** buffer)
   860 {
   861   GstFlowReturn ret = GST_FLOW_OK;
   862 
   863   if (size == 0) {
   864     return ret;
   865   }
   866   
   867   if (nuv->mode == NUV_PULL_MODE) {
   868     ret = gst_pad_pull_range (nuv->sinkpad, nuv->offset, size, buffer);
   869     if (ret == GST_FLOW_OK) {
   870         if (move) {
   871 		    nuv->offset += size;
   872 		}
   873       /* got eos */
   874     } else if (ret == GST_FLOW_UNEXPECTED) {
   875       if (buffer != NULL)
   876           gst_buffer_unref (buffer);
   877       
   878       gst_nuv_demux_send_eos (nuv);
   879       return GST_FLOW_WRONG_STATE;
   880     }
   881   } else {
   882     if (gst_adapter_available (nuv->adapter) < size)
   883       return GST_FLOW_ERROR_NO_DATA;
   884 
   885     if (move) {
   886       guint8 *data = NULL;
   887       data = (guint8 *) gst_adapter_take (nuv->adapter, size);
   888       *buffer = gst_buffer_new ();
   889       gst_buffer_set_data (*buffer, data, size);
   890     } else {
   891       guint8 *data = NULL;
   892       data = (guint8 *) gst_adapter_peek (nuv->adapter, size);
   893       *buffer = gst_buffer_new ();
   894       gst_buffer_set_data (*buffer, data, size);
   895     }
   896   }
   897   return ret;
   898 }
   899 
   900 static gboolean
   901 gst_nuv_demux_sink_activate (GstPad * sinkpad)
   902 {
   903   gboolean res = TRUE;
   904   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
   905 
   906   if (gst_pad_check_pull_range (sinkpad)) {
   907     res = gst_pad_activate_pull (sinkpad, TRUE);
   908   } else {
   909     res = gst_pad_activate_push (sinkpad, TRUE);
   910   }
   911   g_object_unref (nuv);
   912   return res;
   913 }
   914 
   915 static gboolean
   916 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
   917 {
   918   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
   919 
   920   if (active) {
   921     GST_DEBUG_OBJECT (nuv, "activating pull function");
   922     nuv->mode = NUV_PULL_MODE;
   923     if (nuv->adapter) {
   924       gst_adapter_clear (nuv->adapter);
   925       g_object_unref (nuv->adapter);
   926       nuv->adapter = NULL;
   927     }
   928     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
   929   } else {
   930     GST_DEBUG_OBJECT (nuv, "deactivating pull function");
   931     gst_pad_stop_task (sinkpad);
   932   }
   933   gst_object_unref (nuv);
   934 
   935   return TRUE;
   936 }
   937 
   938 static gboolean
   939 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
   940 {
   941   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   942 
   943   if (active) {
   944     nuv->mode = NUV_PUSH_MODE;
   945     if (nuv->adapter) {
   946       gst_adapter_clear (nuv->adapter);
   947     } else {
   948       nuv->adapter = gst_adapter_new ();
   949     }
   950     GST_DEBUG_OBJECT (nuv, "activating push/chain function");
   951   } else {
   952     GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
   953   }
   954 
   955   gst_object_unref (nuv);
   956 
   957   return TRUE;
   958 }
   959 
   960 static GstFlowReturn
   961 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
   962 {
   963   GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
   964 
   965   GST_DEBUG_OBJECT (nuv, " gst_nuv_demux_chain function");
   966   gst_adapter_push (nuv->adapter, buf);
   967 
   968   gst_object_unref (nuv);
   969 
   970   return gst_nuv_demux_play (pad);
   971 }
   972 
   973 static void
   974 gst_nuv_demux_loop (GstPad * pad)
   975 {
   976   gst_nuv_demux_play (pad);
   977 }
   978 
   979 static void
   980 gst_nuv_demux_reset (GstNuvDemux * nuv)
   981 {
   982   nuv->state = GST_NUV_DEMUX_START;
   983   nuv->mode = 0;
   984   nuv->offset = 0;
   985   nuv->time_offset = 0;
   986 
   987   if (nuv->adapter != NULL)
   988     gst_adapter_clear (nuv->adapter);
   989 
   990   if (nuv->mpeg_buffer != NULL) {
   991     gst_buffer_unref (nuv->mpeg_buffer);
   992     nuv->mpeg_buffer = NULL;
   993   }
   994 
   995   g_free (nuv->h);
   996   nuv->h = NULL;
   997 
   998   g_free (nuv->eh);
   999   nuv->eh = NULL;
  1000 
  1001   g_free (nuv->fh);
  1002   nuv->fh = NULL;
  1003 }
  1004 
  1005 static void
  1006 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
  1007 {
  1008   if (nuv->src_video_pad) {
  1009     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
  1010     nuv->src_video_pad = NULL;
  1011   }
  1012 
  1013   if (nuv->src_audio_pad) {
  1014     gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
  1015     nuv->src_audio_pad = NULL;
  1016   }
  1017 }
  1018 
  1019 static GstStateChangeReturn
  1020 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
  1021 {
  1022   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  1023 
  1024   switch (transition) {
  1025     case GST_STATE_CHANGE_READY_TO_PAUSED:
  1026       break;
  1027     default:
  1028       break;
  1029   }
  1030 
  1031   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  1032   if (ret == GST_STATE_CHANGE_FAILURE)
  1033     goto done;
  1034 
  1035   switch (transition) {
  1036     case GST_STATE_CHANGE_PAUSED_TO_READY:
  1037       GST_DEBUG_OBJECT (element, "GST_STATE_CHANGE_PAUSED_TO_READY");
  1038       gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
  1039       gst_nuv_demux_reset (GST_NUV_DEMUX (element));
  1040       break;
  1041     default:
  1042       break;
  1043   }
  1044 
  1045 done:
  1046   return ret;
  1047 }
  1048 
  1049 static gboolean
  1050 plugin_init (GstPlugin * plugin)
  1051 {
  1052 #ifdef ENABLE_NLS
  1053   setlocale (LC_ALL, "");
  1054   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  1055 #endif /* ENABLE_NLS */
  1056 
  1057   if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
  1058           GST_TYPE_NUV_DEMUX)) {
  1059     return FALSE;
  1060   }
  1061   return TRUE;
  1062 }
  1063 
  1064 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1065     GST_VERSION_MINOR,
  1066     "nuvdemux",
  1067     "Demuxes and muxes audio and video",
  1068     plugin_init, VERSION, "LGPL", "NuvDemux", "")