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