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