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