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