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