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