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