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