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