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