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