gst-plugins-nuvdemux/nuvdemux/gstnuvdemux.c
author melunko
Tue Jan 30 21:25:36 2007 +0000 (2007-01-30)
branchtrunk
changeset 314 15f6102ef37d
parent 251 9f0346db9a46
child 318 0944bea397f9
permissions -rw-r--r--
[svn r315] Removed unused comments
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
renatofilho@91
    59
#define GST_NUV_DEMUX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NUV_DEMUX, GstNuvDemuxPrivate))
renatofilho@91
    60
melunko@47
    61
GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug);
melunko@47
    62
#define GST_CAT_DEFAULT nuvdemux_debug
renatofilho@91
    63
#define GST_FLOW_ERROR_NO_DATA  -101
melunko@47
    64
renatofilho@82
    65
enum
renatofilho@82
    66
{
renatofilho@82
    67
   NUV_PUSH_MODE = 0,
renatofilho@82
    68
   NUV_PULL_MODE
renatofilho@82
    69
};
melunko@47
    70
melunko@47
    71
GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
melunko@47
    72
melunko@47
    73
static const GstElementDetails gst_nuv_demux_details =
melunko@47
    74
GST_ELEMENT_DETAILS ("Nuv demuxer",
melunko@47
    75
    "Codec/Demuxer",
melunko@47
    76
    "Demultiplex a .nuv file into audio and video",
melunko@47
    77
    "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
melunko@47
    78
    "Rosfran Borges <rosfran.borges@indt.org.br>");
melunko@47
    79
renatofilho@91
    80
renatofilho@91
    81
/* file header */
renatofilho@91
    82
typedef struct
renatofilho@91
    83
{
renatofilho@91
    84
    gchar id[12];       /* "NuppelVideo\0" or "MythTVVideo\0" */
renatofilho@91
    85
    gchar version[5];    /* "x.xx\0" */
renatofilho@91
    86
renatofilho@91
    87
    gint  i_width;
renatofilho@91
    88
    gint  i_height;
renatofilho@91
    89
    gint  i_width_desired;
renatofilho@91
    90
    gint  i_height_desired;
renatofilho@91
    91
renatofilho@91
    92
    gchar i_mode;            /* P progressive, I interlaced */
renatofilho@91
    93
renatofilho@91
    94
    gdouble  d_aspect;       /* 1.0 squared pixel */
renatofilho@91
    95
    gdouble  d_fps;
renatofilho@91
    96
    //fps num/denom
renatofilho@91
    97
    gint     i_fpsn;
renatofilho@91
    98
    gint     i_fpsd;
renatofilho@91
    99
renatofilho@91
   100
    gint     i_video_blocks; /* 0 no video, -1 unknown */
renatofilho@91
   101
    gint     i_audio_blocks;
renatofilho@91
   102
    gint     i_text_blocks;
renatofilho@91
   103
renatofilho@91
   104
    gint     i_keyframe_distance;
renatofilho@91
   105
renatofilho@91
   106
} nuv_header;
renatofilho@91
   107
renatofilho@91
   108
/* frame header */
renatofilho@91
   109
typedef struct
renatofilho@91
   110
{
renatofilho@91
   111
    gchar i_type;        /* A: audio, V: video, S: sync; T: test
renatofilho@91
   112
                           R: Seekpoint (string:RTjjjjjjjj)
renatofilho@91
   113
                           D: Extra data for codec */
renatofilho@91
   114
    gchar i_compression; /* V: 0 uncompressed
renatofilho@91
   115
                              1 RTJpeg
renatofilho@91
   116
                              2 RTJpeg+lzo
renatofilho@91
   117
                              N black frame
renatofilho@91
   118
                              L copy last
renatofilho@91
   119
                           A: 0 uncompressed (44100 1-bits, 2ch)
renatofilho@91
   120
                              1 lzo
renatofilho@91
   121
                              2 layer 2
renatofilho@91
   122
                              3 layer 3
renatofilho@91
   123
                              F flac
renatofilho@91
   124
                              S shorten
renatofilho@91
   125
                              N null frame loudless
renatofilho@91
   126
                              L copy last
renatofilho@91
   127
                            S: B audio and vdeo sync point
renatofilho@91
   128
                               A audio sync info (timecode == effective
renatofilho@91
   129
                                    dsp frequency*100)
renatofilho@91
   130
                               V next video sync (timecode == next video
renatofilho@91
   131
                                    frame num)
renatofilho@91
   132
                               S audio,video,text correlation */
renatofilho@91
   133
    gchar i_keyframe;    /* 0 keyframe, else no no key frame */
renatofilho@91
   134
    guint8 i_filters;  /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
renatofilho@91
   135
                           0x02: gauss 5 pixel (8,1,1,1,1)/12
renatofilho@91
   136
                           0x04: cartoon filter */
renatofilho@91
   137
renatofilho@91
   138
    gint32 i_timecode;     /* ms */
renatofilho@91
   139
renatofilho@91
   140
    gint i_length;       /* V,A,T: length of following data
renatofilho@91
   141
                           S: length of packet correl */
renatofilho@91
   142
} nuv_frame_header;
renatofilho@91
   143
renatofilho@91
   144
/* FIXME Not sure of this one */
renatofilho@91
   145
typedef struct
renatofilho@91
   146
{
renatofilho@91
   147
    gint             i_version;
renatofilho@91
   148
    guint32		     i_video_fcc;
renatofilho@91
   149
renatofilho@91
   150
    guint32		     i_audio_fcc;
renatofilho@91
   151
    gint             i_audio_sample_rate;
renatofilho@91
   152
    gint             i_audio_bits_per_sample;
renatofilho@91
   153
    gint             i_audio_channels;
renatofilho@91
   154
    gint             i_audio_compression_ratio;
renatofilho@91
   155
    gint             i_audio_quality;
renatofilho@91
   156
    gint             i_rtjpeg_quality;
renatofilho@91
   157
    gint             i_rtjpeg_luma_filter;
renatofilho@91
   158
    gint             i_rtjpeg_chroma_filter;
renatofilho@91
   159
    gint             i_lavc_bitrate;
renatofilho@91
   160
    gint             i_lavc_qmin;
renatofilho@91
   161
    gint             i_lavc_qmax;
renatofilho@91
   162
    gint             i_lavc_maxqdiff;
renatofilho@91
   163
    gint64         	 i_seekable_offset;
renatofilho@91
   164
    gint64           i_keyframe_adjust_offset;
renatofilho@91
   165
renatofilho@91
   166
} nuv_extended_header;
renatofilho@91
   167
renatofilho@91
   168
typedef enum {
renatofilho@91
   169
  GST_NUV_DEMUX_START,
renatofilho@91
   170
  GST_NUV_DEMUX_HEADER_DATA,
renatofilho@91
   171
  GST_NUV_DEMUX_EXTRA_DATA,
renatofilho@91
   172
  GST_NUV_DEMUX_MPEG_DATA,
renatofilho@91
   173
  GST_NUV_DEMUX_EXTEND_HEADER,
renatofilho@91
   174
  GST_NUV_DEMUX_EXTEND_HEADER_DATA,
renatofilho@91
   175
  GST_NUV_DEMUX_FRAME_HEADER,
renatofilho@91
   176
  GST_NUV_DEMUX_MOVI,
renatofilho@91
   177
  GST_NUV_DEMUX_INVALID_DATA
renatofilho@91
   178
} GstNuvDemuxState;
renatofilho@91
   179
renatofilho@91
   180
struct _GstNuvDemuxPrivate {
renatofilho@91
   181
  /* used for indicate the mode */
renatofilho@91
   182
  guint         mode;
renatofilho@91
   183
renatofilho@91
   184
  /* used on push mode */
renatofilho@91
   185
  GstAdapter    *adapter;
renatofilho@91
   186
renatofilho@91
   187
  /* pads */
renatofilho@91
   188
  GstPad        *sinkpad;
renatofilho@91
   189
  GstPad        *src_video_pad;
renatofilho@91
   190
  GstPad        *src_audio_pad;
renatofilho@91
   191
renatofilho@91
   192
  /* Flow control */
renatofilho@91
   193
  GstFlowReturn last_video_return;
renatofilho@91
   194
  GstFlowReturn last_audio_return;
renatofilho@194
   195
  gboolean      more_data;
renatofilho@91
   196
renatofilho@91
   197
  /* NUV decoding state */
renatofilho@91
   198
  GstNuvDemuxState  state;
renatofilho@91
   199
  GstSegment        segment;
renatofilho@91
   200
  guint64           last_update;
renatofilho@91
   201
  guint64           offset;
renatofilho@91
   202
  guint64           streamer_offset;
renatofilho@91
   203
renatofilho@91
   204
  /* duration information */
renatofilho@91
   205
  gint64            duration_bytes;
renatofilho@91
   206
  gint64            duration_time;
renatofilho@91
   207
  gint64            duration_average;
renatofilho@91
   208
renatofilho@91
   209
  /* segment control info */
renatofilho@91
   210
  gboolean          new_audio_segment;
renatofilho@91
   211
  gboolean          new_video_segment;
renatofilho@91
   212
renatofilho@91
   213
  /* Mpeg ExtraData */
renatofilho@91
   214
  guint64       mpeg_data_size;
renatofilho@91
   215
  GstBuffer     *mpeg_buffer;
renatofilho@91
   216
renatofilho@91
   217
  /* Headers */
renatofilho@170
   218
  nuv_header h;
renatofilho@170
   219
  nuv_extended_header eh;
renatofilho@170
   220
  nuv_frame_header fh;
renatofilho@91
   221
renatofilho@91
   222
  /* anothers info */
renatofilho@91
   223
  gint64            time_start;
renatofilho@91
   224
  gint64            time_diff;
renatofilho@91
   225
  gint64            time_qos;
renatofilho@91
   226
  guint64           last_frame_time;
renatofilho@91
   227
};
renatofilho@91
   228
renatofilho@91
   229
melunko@47
   230
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
melunko@47
   231
    GST_PAD_SINK,
melunko@47
   232
    GST_PAD_ALWAYS,
melunko@47
   233
    GST_STATIC_CAPS ("video/x-nuv"));
melunko@47
   234
melunko@47
   235
static GstStaticPadTemplate audio_src_template =
melunko@47
   236
GST_STATIC_PAD_TEMPLATE ("audio_src",
melunko@47
   237
    GST_PAD_SRC,
melunko@47
   238
    GST_PAD_SOMETIMES,
melunko@47
   239
    GST_STATIC_CAPS_ANY);
melunko@47
   240
melunko@47
   241
static GstStaticPadTemplate video_src_template =
melunko@47
   242
GST_STATIC_PAD_TEMPLATE ("video_src",
melunko@47
   243
    GST_PAD_SRC,
melunko@47
   244
    GST_PAD_SOMETIMES,
melunko@47
   245
    GST_STATIC_CAPS_ANY);
melunko@47
   246
renatofilho@91
   247
static void gst_nuv_demux_dispose (GObject * object);
melunko@47
   248
static void gst_nuv_demux_finalize (GObject * object);
melunko@47
   249
static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
melunko@47
   250
    GstStateChange transition);
melunko@47
   251
static void gst_nuv_demux_loop (GstPad * pad);
melunko@47
   252
static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
melunko@47
   253
static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
melunko@47
   254
static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
melunko@47
   255
    gboolean active);
renatofilho@91
   256
static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
renatofilho@77
   257
    gboolean active);
melunko@47
   258
static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
renatofilho@91
   259
static gboolean gst_nuv_demux_sink_event    (GstPad *pad, GstEvent *event);
melunko@47
   260
static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
melunko@47
   261
    gboolean move, GstBuffer ** buffer);
melunko@47
   262
static void gst_nuv_demux_reset (GstNuvDemux * nuv);
melunko@47
   263
static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
melunko@47
   264
static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
renatofilho@91
   265
static void gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp);
renatofilho@91
   266
static gint64 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv);
renatofilho@91
   267
static gint64 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv);
renatofilho@181
   268
renatofilho@181
   269
#if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
renatofilho@196
   270
GstBuffer * gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes);
renatofilho@181
   271
#endif
melunko@47
   272
renatofilho@91
   273
melunko@47
   274
GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
melunko@47
   275
renatofilho@91
   276
/******************************************************************************
renatofilho@91
   277
 * Utils function
renatofilho@91
   278
 ******************************************************************************/
melunko@47
   279
#if G_BYTE_ORDER == G_BIG_ENDIAN
melunko@47
   280
static inline gdouble
melunko@47
   281
_gdouble_swap_le_be (gdouble * d)
melunko@47
   282
{
melunko@47
   283
  union
melunko@47
   284
  {
melunko@47
   285
    guint64 i;
melunko@47
   286
    gdouble d;
melunko@47
   287
  } u;
melunko@47
   288
melunko@47
   289
  u.d = *d;
melunko@47
   290
  u.i = GUINT64_SWAP_LE_BE (u.i);
melunko@47
   291
  return u.d;
melunko@47
   292
}
melunko@47
   293
melunko@47
   294
#define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
melunko@47
   295
#else /* G_BYTE_ORDER != G_BIG_ENDIAN */
melunko@47
   296
#define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
melunko@47
   297
#endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
melunko@47
   298
renatofilho@91
   299
static void
renatofilho@85
   300
double2fraction (double in, int *num, int *denom)
renatofilho@85
   301
{
renatofilho@85
   302
    if (in == 29.97) {
renatofilho@85
   303
        *num = 30000;
renatofilho@85
   304
        *denom = 1001;
renatofilho@85
   305
    } else if (in == 23.976) {
renatofilho@85
   306
        *num = 24000;
renatofilho@85
   307
        *denom = 1001;
renatofilho@85
   308
    } else {
renatofilho@85
   309
        *denom = 1;
renatofilho@85
   310
        while (in - floor(in) >= 0.1) {
renatofilho@85
   311
            *denom *= 10;
renatofilho@85
   312
            in *= 10.0;
renatofilho@85
   313
        }
renatofilho@85
   314
        *num = (int)floor(in);
renatofilho@85
   315
    }
renatofilho@85
   316
}
renatofilho@85
   317
renatofilho@91
   318
/* GObject Functions */
renatofilho@91
   319
melunko@47
   320
static void
melunko@47
   321
gst_nuv_demux_base_init (gpointer klass)
melunko@47
   322
{
melunko@47
   323
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
melunko@47
   324
melunko@47
   325
  gst_element_class_add_pad_template (element_class,
melunko@47
   326
      gst_static_pad_template_get (&audio_src_template));
melunko@47
   327
melunko@47
   328
  gst_element_class_add_pad_template (element_class,
melunko@47
   329
      gst_static_pad_template_get (&video_src_template));
melunko@47
   330
melunko@47
   331
  gst_element_class_add_pad_template (element_class,
melunko@47
   332
      gst_static_pad_template_get (&sink_template));
melunko@47
   333
  gst_element_class_set_details (element_class, &gst_nuv_demux_details);
melunko@47
   334
}
melunko@47
   335
melunko@47
   336
static void
melunko@47
   337
gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
melunko@47
   338
{
melunko@47
   339
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
melunko@47
   340
  GObjectClass *gobject_class = (GObjectClass *) klass;
melunko@47
   341
melunko@47
   342
  GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
melunko@47
   343
      0, "Demuxer for NUV streams");
melunko@47
   344
melunko@47
   345
  parent_class = g_type_class_peek_parent (klass);
melunko@47
   346
renatofilho@91
   347
  gobject_class->dispose = gst_nuv_demux_dispose;
melunko@47
   348
  gobject_class->finalize = gst_nuv_demux_finalize;
melunko@47
   349
  gstelement_class->change_state = gst_nuv_demux_change_state;
renatofilho@91
   350
renatofilho@91
   351
  g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate));
melunko@47
   352
}
melunko@47
   353
melunko@47
   354
static void
melunko@47
   355
gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
melunko@47
   356
{
renatofilho@91
   357
  nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv);
renatofilho@91
   358
  nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
renatofilho@91
   359
  gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate);
renatofilho@91
   360
  gst_pad_set_activatepull_function (nuv->priv->sinkpad,
renatofilho@91
   361
      gst_nuv_demux_sink_activate_pull);
renatofilho@91
   362
  gst_pad_set_activatepush_function (nuv->priv->sinkpad,
renatofilho@91
   363
      gst_nuv_demux_sink_activate_push);
renatofilho@91
   364
  gst_pad_set_chain_function (nuv->priv->sinkpad,
renatofilho@91
   365
      GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
renatofilho@91
   366
  gst_pad_set_event_function (nuv->priv->sinkpad,
renatofilho@91
   367
      gst_nuv_demux_sink_event);
renatofilho@91
   368
  gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad);
melunko@47
   369
renatofilho@91
   370
  nuv->priv->new_audio_segment = TRUE;
renatofilho@91
   371
  nuv->priv->new_video_segment = TRUE;
melunko@47
   372
renatofilho@133
   373
  /* creating adapter */
renatofilho@133
   374
  nuv->priv->mode = NUV_PUSH_MODE;
renatofilho@133
   375
  nuv->priv->adapter = gst_adapter_new ();
renatofilho@133
   376
renatofilho@91
   377
  gst_nuv_demux_reset (nuv);
renatofilho@91
   378
}
melunko@47
   379
renatofilho@91
   380
static void
renatofilho@91
   381
gst_nuv_demux_dispose (GObject * object)
renatofilho@91
   382
{
renatofilho@91
   383
  GstNuvDemux *nuv = GST_NUV_DEMUX (object);
renatofilho@77
   384
renatofilho@91
   385
  if (nuv->priv->mpeg_buffer != NULL) {
renatofilho@91
   386
    gst_buffer_unref (nuv->priv->mpeg_buffer);
renatofilho@91
   387
  }
melunko@47
   388
renatofilho@91
   389
  gst_nuv_demux_reset (GST_NUV_DEMUX (object));
renatofilho@91
   390
  gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (object));
leo_sobral@80
   391
renatofilho@91
   392
  if (nuv->priv->adapter != NULL) {
renatofilho@91
   393
    gst_object_unref (nuv->priv->adapter);
renatofilho@91
   394
  }
melunko@47
   395
}
melunko@47
   396
melunko@47
   397
static void
melunko@47
   398
gst_nuv_demux_finalize (GObject * object)
melunko@47
   399
{
melunko@47
   400
  G_OBJECT_CLASS (parent_class)->finalize (object);
melunko@47
   401
}
melunko@47
   402
rosfran@56
   403
melunko@47
   404
/* HeaderLoad:
melunko@47
   405
 */
melunko@47
   406
static GstFlowReturn
renatofilho@170
   407
gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header *h)
melunko@47
   408
{
melunko@47
   409
  GstBuffer *buffer = NULL;
melunko@47
   410
  GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
melunko@47
   411
renatofilho@182
   412
  if ((res != GST_FLOW_OK) || (buffer == NULL)) {
renatofilho@182
   413
      goto done;
renatofilho@182
   414
  }
melunko@47
   415
melunko@47
   416
  memcpy (h->id, buffer->data, 12);
melunko@47
   417
  memcpy (h->version, buffer->data + 12, 5);
melunko@47
   418
  h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
melunko@47
   419
  h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
melunko@47
   420
  h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
melunko@47
   421
  h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
renatofilho@77
   422
  h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
melunko@47
   423
  h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
melunko@47
   424
  h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
renatofilho@85
   425
  /* get the num and denom values from fps */
renatofilho@85
   426
  double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
melunko@47
   427
  h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
melunko@47
   428
  h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
melunko@47
   429
  h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
melunko@47
   430
  h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
melunko@47
   431
melunko@47
   432
  GST_DEBUG_OBJECT (nuv,
melunko@47
   433
      "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
melunko@47
   434
      h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
melunko@47
   435
      h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
melunko@47
   436
      h->i_keyframe_distance);
melunko@47
   437
renatofilho@182
   438
done:
renatofilho@182
   439
  if (buffer != NULL) {
renatofilho@182
   440
     gst_buffer_unref (buffer);
renatofilho@182
   441
     buffer = NULL;
renatofilho@182
   442
  }
melunko@47
   443
  return res;
melunko@47
   444
}
melunko@47
   445
melunko@47
   446
static GstFlowReturn
melunko@47
   447
gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
melunko@47
   448
{
renatofilho@91
   449
  GstFlowReturn res;
melunko@47
   450
renatofilho@91
   451
  res = gst_nuv_demux_header_load (nuv, &nuv->priv->h);
melunko@47
   452
  if (res == GST_FLOW_OK)
renatofilho@91
   453
    nuv->priv->state = GST_NUV_DEMUX_EXTRA_DATA;
melunko@47
   454
  return res;
melunko@47
   455
}
melunko@47
   456
melunko@47
   457
/*
melunko@47
   458
 * Read NUV file tag
melunko@47
   459
 */
melunko@47
   460
static GstFlowReturn
melunko@47
   461
gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
melunko@47
   462
{
melunko@47
   463
  GstFlowReturn res = GST_FLOW_OK;
melunko@47
   464
  GstBuffer *file_header = NULL;
melunko@47
   465
melunko@47
   466
  res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
renatofilho@182
   467
  if (res == GST_FLOW_OK) {
melunko@47
   468
    if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
melunko@47
   469
        strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
renatofilho@91
   470
      nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
melunko@47
   471
    } else {
melunko@47
   472
      GST_DEBUG_OBJECT (nuv, "error parsing file header");
renatofilho@91
   473
      nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
melunko@47
   474
      res = GST_FLOW_ERROR;
melunko@47
   475
    }
melunko@47
   476
  }
renatofilho@163
   477
melunko@47
   478
  if (file_header != NULL) {
melunko@47
   479
    gst_buffer_unref (file_header);
renatofilho@182
   480
    file_header = NULL;
melunko@47
   481
  }
melunko@47
   482
  return res;
melunko@47
   483
}
melunko@47
   484
melunko@47
   485
/* FrameHeaderLoad:
melunko@47
   486
 */
melunko@47
   487
static GstFlowReturn
renatofilho@170
   488
gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header *h)
melunko@47
   489
{
melunko@47
   490
  unsigned char *data;
melunko@47
   491
  GstBuffer *buf = NULL;
melunko@47
   492
melunko@47
   493
  GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
melunko@47
   494
renatofilho@84
   495
  if ((res != GST_FLOW_OK) || (buf == NULL)) {
renatofilho@182
   496
      goto done;
melunko@47
   497
  }
renatofilho@194
   498
melunko@47
   499
  data = buf->data;
melunko@47
   500
renatofilho@77
   501
  h->i_type = GPOINTER_TO_INT (data[0]);
renatofilho@77
   502
  h->i_compression = GPOINTER_TO_INT (data[1]);
renatofilho@77
   503
  h->i_keyframe = GPOINTER_TO_INT (data[2]);
renatofilho@77
   504
  h->i_filters = GPOINTER_TO_INT (data[3]);
rosfran@57
   505
  h->i_timecode = GST_READ_UINT32_LE (&data[4]);
melunko@47
   506
  h->i_length = GST_READ_UINT32_LE (&data[8]);
renatofilho@91
   507
rosfran@56
   508
  GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
melunko@47
   509
      h->i_type,
melunko@47
   510
      h->i_compression ? h->i_compression : ' ',
melunko@47
   511
      h->i_keyframe ? h->i_keyframe : ' ',
melunko@47
   512
      h->i_filters, h->i_timecode, h->i_length);
melunko@47
   513
renatofilho@182
   514
done:  
renatofilho@182
   515
  if (buf != NULL) {
renatofilho@182
   516
      gst_buffer_unref (buf);
renatofilho@182
   517
      buf = NULL;
renatofilho@182
   518
  }
renatofilho@182
   519
  return res;
melunko@47
   520
}
melunko@47
   521
melunko@47
   522
static GstFlowReturn
melunko@47
   523
gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
renatofilho@170
   524
    nuv_extended_header * h)
melunko@47
   525
{
melunko@47
   526
  unsigned char *data;
melunko@47
   527
  GstBuffer *buff = NULL;
melunko@47
   528
melunko@47
   529
  GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
melunko@47
   530
renatofilho@84
   531
  if ((res != GST_FLOW_OK) || (buff == NULL)) {
renatofilho@182
   532
      goto done;
melunko@47
   533
  }
melunko@47
   534
melunko@47
   535
  data = buff->data;
melunko@47
   536
  h->i_version = GST_READ_UINT32_LE (&data[0]);
melunko@47
   537
  h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
melunko@47
   538
  h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
melunko@47
   539
  h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
melunko@47
   540
  h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
melunko@47
   541
  h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
melunko@47
   542
  h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
melunko@47
   543
  h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
melunko@47
   544
  h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
melunko@47
   545
  h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
melunko@47
   546
  h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
melunko@47
   547
  h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
melunko@47
   548
  h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
melunko@47
   549
  h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
melunko@47
   550
  h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
melunko@47
   551
  h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
melunko@47
   552
  h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
melunko@47
   553
melunko@47
   554
  GST_DEBUG_OBJECT (nuv,
melunko@47
   555
      "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
melunko@47
   556
      "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
melunko@47
   557
      h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
melunko@47
   558
      h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
melunko@47
   559
      h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
melunko@47
   560
      h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
melunko@47
   561
      h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
melunko@47
   562
      h->i_keyframe_adjust_offset);
melunko@47
   563
renatofilho@182
   564
done:
renatofilho@182
   565
  if (buff != NULL) {
renatofilho@182
   566
    gst_buffer_unref (buff);
renatofilho@182
   567
    buff = NULL;
renatofilho@182
   568
  }
melunko@47
   569
  return res;
melunko@47
   570
}
renatofilho@91
   571
renatofilho@91
   572
renatofilho@91
   573
/* Query Functions */
renatofilho@85
   574
static const GstQueryType *
renatofilho@85
   575
gst_nuv_demux_get_src_query_types (GstPad * pad)
renatofilho@85
   576
{
renatofilho@85
   577
  static const GstQueryType src_types[] = {
renatofilho@85
   578
    GST_QUERY_POSITION,
renatofilho@91
   579
    GST_QUERY_DURATION,
renatofilho@85
   580
    0
renatofilho@85
   581
  };
renatofilho@85
   582
renatofilho@85
   583
  return src_types;
renatofilho@85
   584
}
renatofilho@85
   585
renatofilho@85
   586
static gboolean
renatofilho@85
   587
gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
renatofilho@85
   588
{
renatofilho@85
   589
  gboolean res = FALSE;
renatofilho@85
   590
  GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
renatofilho@85
   591
renatofilho@85
   592
  switch (GST_QUERY_TYPE (query)) {
renatofilho@85
   593
    case GST_QUERY_POSITION:
renatofilho@91
   594
      if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
renatofilho@91
   595
renatofilho@91
   596
        gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
renatofilho@85
   597
        res = TRUE;
renatofilho@91
   598
      }
renatofilho@91
   599
      break;
renatofilho@91
   600
    case GST_QUERY_DURATION:
renatofilho@91
   601
      {
renatofilho@91
   602
        gint64 duration = 0;
renatofilho@91
   603
        duration = gst_nuv_demux_get_time_duration (nuv);
renatofilho@91
   604
        if (duration == GST_CLOCK_TIME_NONE) {
renatofilho@91
   605
            duration = nuv->priv->duration_average;
renatofilho@91
   606
        }
renatofilho@91
   607
        if (duration != GST_CLOCK_TIME_NONE) {
renatofilho@91
   608
            gst_query_set_duration (query, GST_FORMAT_TIME, duration);
renatofilho@91
   609
            res = TRUE;
renatofilho@91
   610
        }
renatofilho@85
   611
      }
renatofilho@85
   612
      break;
renatofilho@85
   613
    default:
renatofilho@85
   614
      res = FALSE;
renatofilho@85
   615
      break;
renatofilho@85
   616
  }
renatofilho@85
   617
renatofilho@85
   618
  gst_object_unref (nuv);
renatofilho@85
   619
renatofilho@85
   620
  return res;
renatofilho@85
   621
}
melunko@47
   622
renatofilho@91
   623
static GstPad*
renatofilho@91
   624
gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
renatofilho@91
   625
{
renatofilho@91
   626
    GstPad *pad = NULL;
renatofilho@91
   627
    pad = gst_pad_new_from_static_template (template, name);
renatofilho@91
   628
    gst_pad_set_caps (pad, caps);
renatofilho@91
   629
    gst_pad_set_active (pad, TRUE);
renatofilho@133
   630
    gst_pad_use_fixed_caps (pad);
renatofilho@91
   631
    gst_element_add_pad (GST_ELEMENT (nuv), pad);
renatofilho@91
   632
renatofilho@91
   633
    return pad;
renatofilho@91
   634
}
renatofilho@91
   635
melunko@47
   636
static void
melunko@47
   637
gst_nuv_demux_create_pads (GstNuvDemux * nuv)
melunko@47
   638
{
renatofilho@170
   639
  if (nuv->priv->h.i_video_blocks != 0) {
melunko@47
   640
    GstCaps *video_caps = NULL;
melunko@47
   641
melunko@47
   642
    video_caps = gst_caps_new_simple ("video/x-divx",
melunko@47
   643
        "divxversion", G_TYPE_INT, 4,
renatofilho@170
   644
        "width", G_TYPE_INT, nuv->priv->h.i_width,
renatofilho@170
   645
        "height", G_TYPE_INT, nuv->priv->h.i_height,
renatofilho@170
   646
        "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
renatofilho@170
   647
        "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
melunko@47
   648
        "pixel-aspect-ratio", GST_TYPE_FRACTION,
renatofilho@170
   649
        (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
melunko@47
   650
renatofilho@91
   651
    nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
melunko@47
   652
    gst_caps_unref (video_caps);
melunko@47
   653
  }
melunko@47
   654
renatofilho@170
   655
  if (nuv->priv->h.i_audio_blocks != 0) {
melunko@47
   656
    GstCaps *audio_caps = NULL;
melunko@47
   657
renatofilho@91
   658
    audio_caps = gst_caps_new_simple ("audio/mpeg",
renatofilho@170
   659
        "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
renatofilho@170
   660
        "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
renatofilho@170
   661
        "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
melunko@273
   662
        "layer", G_TYPE_INT, 3, // fixme: magic number
renatofilho@170
   663
        "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
melunko@251
   664
    
melunko@251
   665
    char *tmp = gst_caps_to_string (audio_caps);
melunko@47
   666
renatofilho@91
   667
    nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
melunko@47
   668
    gst_caps_unref (audio_caps);
melunko@47
   669
  }
melunko@47
   670
melunko@47
   671
  gst_element_no_more_pads (GST_ELEMENT (nuv));
melunko@47
   672
}
melunko@47
   673
melunko@47
   674
static GstFlowReturn
melunko@47
   675
gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
melunko@47
   676
{
melunko@47
   677
  GstFlowReturn ret = GST_FLOW_OK;
melunko@47
   678
renatofilho@91
   679
  ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
melunko@47
   680
  if (ret != GST_FLOW_OK)
melunko@47
   681
    return ret;
melunko@47
   682
renatofilho@91
   683
  nuv->priv->state = GST_NUV_DEMUX_MOVI;
melunko@47
   684
  return ret;
melunko@47
   685
}
melunko@47
   686
renatofilho@88
   687
static gboolean
renatofilho@88
   688
gst_nuv_combine_flow (GstNuvDemux *nuv)
renatofilho@88
   689
{
renatofilho@91
   690
    GstFlowReturn ret_video = nuv->priv->last_video_return;
renatofilho@91
   691
    GstFlowReturn ret_audio = nuv->priv->last_audio_return;
renatofilho@88
   692
renatofilho@88
   693
    if ((ret_video != GST_FLOW_OK) &&
renatofilho@88
   694
        (ret_audio != GST_FLOW_OK))
renatofilho@88
   695
        return FALSE;
renatofilho@88
   696
renatofilho@91
   697
    if (GST_FLOW_IS_FATAL (ret_video))
renatofilho@88
   698
        return FALSE;
renatofilho@88
   699
renatofilho@91
   700
    if (GST_FLOW_IS_FATAL (ret_audio))
renatofilho@88
   701
        return FALSE;
renatofilho@88
   702
renatofilho@91
   703
    return TRUE;
renatofilho@88
   704
}
renatofilho@88
   705
melunko@47
   706
static GstFlowReturn
melunko@47
   707
gst_nuv_demux_stream_data (GstNuvDemux * nuv)
melunko@47
   708
{
melunko@47
   709
  GstFlowReturn ret = GST_FLOW_OK;
renatofilho@91
   710
  GstPad *pad = NULL;
renatofilho@91
   711
  guint64 timestamp;
melunko@47
   712
  GstBuffer *buf = NULL;
renatofilho@170
   713
  nuv_frame_header h;
renatofilho@91
   714
renatofilho@91
   715
  h = nuv->priv->fh;
melunko@47
   716
renatofilho@170
   717
  if (h.i_type == 'R')
melunko@47
   718
    goto done;
renatofilho@91
   719
renatofilho@170
   720
  if (h.i_length > 0) {
renatofilho@170
   721
	  ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
renatofilho@182
   722
	  if ((ret != GST_FLOW_OK) || (buf == NULL)) {
renatofilho@182
   723
          goto done;
renatofilho@182
   724
      }
rosfran@57
   725
melunko@218
   726
      if ((h.i_timecode < 0)) {
melunko@218
   727
	goto done;
leo_sobral@83
   728
      }
renatofilho@91
   729
renatofilho@170
   730
      timestamp = h.i_timecode * GST_MSECOND;
renatofilho@91
   731
      GST_BUFFER_TIMESTAMP (buf) = timestamp;
renatofilho@77
   732
  }
renatofilho@91
   733
  else {
renatofilho@91
   734
    goto done;
renatofilho@91
   735
  }
renatofilho@91
   736
renatofilho@60
   737
renatofilho@170
   738
  switch (h.i_type) {
melunko@47
   739
    case 'V':
melunko@47
   740
    {
renatofilho@91
   741
      pad = nuv->priv->src_video_pad;
renatofilho@91
   742
renatofilho@91
   743
      if (nuv->priv->new_video_segment) {
renatofilho@136
   744
leo_sobral@80
   745
        /* send new segment event*/
renatofilho@137
   746
        gst_pad_push_event (nuv->priv->src_video_pad,
renatofilho@137
   747
          gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
renatofilho@137
   748
                                     GST_CLOCK_TIME_NONE, 0));
renatofilho@91
   749
renatofilho@91
   750
        if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
renatofilho@91
   751
            nuv->priv->time_start = timestamp;
renatofilho@91
   752
        }
renatofilho@91
   753
        nuv->priv->new_video_segment = FALSE;
leo_sobral@80
   754
      }
renatofilho@91
   755
melunko@47
   756
      break;
melunko@47
   757
    }
melunko@47
   758
    case 'A':
melunko@47
   759
    {
renatofilho@91
   760
      pad = nuv->priv->src_audio_pad;
renatofilho@91
   761
renatofilho@91
   762
      if (nuv->priv->new_audio_segment) {
leo_sobral@80
   763
        /* send new segment event*/
renatofilho@137
   764
        gst_pad_push_event (nuv->priv->src_audio_pad,
renatofilho@137
   765
          gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
renatofilho@137
   766
                                     GST_CLOCK_TIME_NONE, 0));
renatofilho@91
   767
renatofilho@91
   768
        if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
renatofilho@91
   769
            nuv->priv->time_start = timestamp;
renatofilho@91
   770
        }
renatofilho@91
   771
        nuv->priv->new_audio_segment = FALSE;
leo_sobral@80
   772
      }
renatofilho@91
   773
melunko@47
   774
      break;
melunko@47
   775
    }
melunko@47
   776
    case 'S':
melunko@218
   777
   {
renatofilho@170
   778
      switch (h.i_compression) {
melunko@47
   779
        case 'V':
renatofilho@170
   780
          GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
renatofilho@91
   781
          gst_pad_push_event (nuv->priv->src_video_pad,
renatofilho@170
   782
              gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
rosfran@73
   783
              		GST_CLOCK_TIME_NONE, 0));
melunko@47
   784
          break;
renatofilho@91
   785
        case 'A':
renatofilho@170
   786
          GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
renatofilho@91
   787
          gst_pad_push_event (nuv->priv->src_audio_pad,
renatofilho@91
   788
              gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
rosfran@73
   789
              		GST_CLOCK_TIME_NONE, 0));
melunko@47
   790
          break;
melunko@47
   791
        default:
melunko@47
   792
          break;
melunko@47
   793
      }
renatofilho@91
   794
      goto done;
rosfran@56
   795
    }
rosfran@56
   796
    default:
melunko@47
   797
      break;
melunko@47
   798
  }
melunko@47
   799
renatofilho@91
   800
  if ((buf != NULL) && (pad != NULL)) {
renatofilho@91
   801
      /* update average time */
renatofilho@170
   802
      nuv->priv->streamer_offset += h.i_length;
renatofilho@91
   803
      gst_segment_set_last_stop (&nuv->priv->segment, GST_FORMAT_TIME, timestamp);
renatofilho@91
   804
      gst_nuv_demux_update_duration (nuv, timestamp);
renatofilho@91
   805
renatofilho@91
   806
      /* pushing the buffer */
renatofilho@91
   807
      gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
renatofilho@91
   808
      ret = gst_pad_push (pad, buf);
renatofilho@168
   809
      buf = NULL;
renatofilho@91
   810
renatofilho@91
   811
      if (ret != GST_FLOW_OK) {
renatofilho@136
   812
        GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
renatofilho@136
   813
renatofilho@91
   814
        if (pad == nuv->priv->src_video_pad) {
renatofilho@91
   815
            nuv->priv->last_video_return = ret;
renatofilho@91
   816
        }
renatofilho@91
   817
        else if (pad == nuv->priv->src_audio_pad) {
renatofilho@91
   818
            nuv->priv->last_audio_return = ret;
renatofilho@91
   819
        }
renatofilho@91
   820
renatofilho@91
   821
        /* verify anothers flow if is necessary stop task */
renatofilho@91
   822
        if (gst_nuv_combine_flow (nuv) != FALSE) {
renatofilho@91
   823
            ret = GST_FLOW_OK;
renatofilho@91
   824
        }
renatofilho@91
   825
renatofilho@136
   826
        //GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
renatofilho@91
   827
      }
renatofilho@168
   828
  } 
renatofilho@168
   829
renatofilho@168
   830
done:
renatofilho@168
   831
  if (buf != NULL) {
renatofilho@163
   832
      gst_buffer_unref (buf);
renatofilho@163
   833
      buf = NULL;
renatofilho@91
   834
  }
renatofilho@194
   835
  if (ret == GST_FLOW_OK) {
renatofilho@194
   836
     nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
renatofilho@194
   837
     memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
renatofilho@194
   838
  }
melunko@47
   839
  return ret;
melunko@47
   840
}
melunko@47
   841
melunko@47
   842
static GstFlowReturn
melunko@47
   843
gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
melunko@47
   844
{
melunko@47
   845
  GstFlowReturn ret = GST_FLOW_OK;
melunko@47
   846
melunko@47
   847
  /* ffmpeg extra data */
renatofilho@91
   848
  ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
renatofilho@91
   849
      &nuv->priv->mpeg_buffer);
renatofilho@91
   850
  if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
renatofilho@91
   851
    return ret;
melunko@47
   852
  }
renatofilho@91
   853
renatofilho@91
   854
  GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
renatofilho@91
   855
  nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
melunko@47
   856
  return ret;
melunko@47
   857
}
melunko@47
   858
melunko@47
   859
static GstFlowReturn
melunko@47
   860
gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
melunko@47
   861
{
melunko@47
   862
  GstFlowReturn ret = GST_FLOW_OK;
melunko@47
   863
melunko@47
   864
  /* Load 'D' */
renatofilho@170
   865
  nuv_frame_header h;
melunko@47
   866
melunko@47
   867
  ret = gst_nuv_demux_frame_header_load (nuv, &h);
melunko@47
   868
  if (ret != GST_FLOW_OK)
melunko@47
   869
    return ret;
melunko@47
   870
renatofilho@170
   871
  if (h.i_type != 'D') {
renatofilho@92
   872
    GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
renatofilho@92
   873
    return GST_FLOW_NOT_SUPPORTED;
melunko@47
   874
  }
melunko@47
   875
renatofilho@170
   876
  if (h.i_length > 0) {
renatofilho@170
   877
    if (h.i_compression == 'F') {
renatofilho@91
   878
      nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
melunko@47
   879
    } else {
renatofilho@92
   880
      GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
renatofilho@92
   881
      return GST_FLOW_NOT_SUPPORTED;
melunko@47
   882
    }
melunko@47
   883
  } else {
renatofilho@91
   884
    nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
melunko@47
   885
  }
melunko@47
   886
melunko@47
   887
  return ret;
melunko@47
   888
}
melunko@47
   889
melunko@47
   890
static GstFlowReturn
melunko@47
   891
gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
melunko@47
   892
{
melunko@47
   893
  GstFlowReturn ret = GST_FLOW_OK;
melunko@47
   894
renatofilho@91
   895
  ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
melunko@47
   896
  if (ret != GST_FLOW_OK)
melunko@47
   897
    return ret;
melunko@47
   898
melunko@47
   899
  gst_nuv_demux_create_pads (nuv);
renatofilho@91
   900
  nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
melunko@47
   901
  return ret;
melunko@47
   902
}
melunko@47
   903
melunko@47
   904
static GstFlowReturn
melunko@47
   905
gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
melunko@47
   906
{
melunko@47
   907
  GstBuffer *buf = NULL;
melunko@47
   908
  GstFlowReturn res = GST_FLOW_OK;
melunko@47
   909
melunko@47
   910
  res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
renatofilho@84
   911
  if ((res != GST_FLOW_OK) || (buf == NULL)) {
melunko@47
   912
    if (buf != NULL) {
melunko@47
   913
      gst_buffer_unref (buf);
melunko@47
   914
    }
melunko@47
   915
    return res;
melunko@47
   916
  }
melunko@47
   917
melunko@47
   918
  if (buf->data[0] == 'X') {
melunko@47
   919
    gst_buffer_unref (buf);
melunko@47
   920
    buf = NULL;
renatofilho@170
   921
    nuv_frame_header h;
melunko@47
   922
melunko@47
   923
    res = gst_nuv_demux_frame_header_load (nuv, &h);
melunko@47
   924
    if (res != GST_FLOW_OK)
melunko@47
   925
      return res;
melunko@47
   926
renatofilho@170
   927
    if (h.i_length != 512) {
melunko@47
   928
      return GST_FLOW_ERROR;
melunko@47
   929
    }
renatofilho@91
   930
    nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
melunko@47
   931
  } else {
renatofilho@91
   932
    nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
melunko@47
   933
    g_object_unref (buf);
melunko@47
   934
    GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
melunko@47
   935
        (_("incomplete NUV support")), ("incomplete NUV support"));
melunko@47
   936
    return GST_FLOW_ERROR;
melunko@47
   937
  }
melunko@47
   938
  return res;
melunko@47
   939
}
melunko@47
   940
melunko@47
   941
static GstFlowReturn
melunko@47
   942
gst_nuv_demux_play (GstPad * pad)
melunko@47
   943
{
melunko@47
   944
  GstFlowReturn res = GST_FLOW_OK;
melunko@47
   945
  GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
melunko@47
   946
renatofilho@91
   947
  switch (nuv->priv->state) {
melunko@47
   948
    case GST_NUV_DEMUX_START:
melunko@47
   949
      res = gst_nuv_demux_stream_file_header (nuv);
melunko@47
   950
      if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
melunko@47
   951
        goto pause;
melunko@47
   952
      }
renatofilho@136
   953
      break;
melunko@47
   954
melunko@47
   955
    case GST_NUV_DEMUX_HEADER_DATA:
melunko@47
   956
      res = gst_nuv_demux_stream_header_data (nuv);
melunko@47
   957
      if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
melunko@47
   958
        goto pause;
melunko@47
   959
      }
renatofilho@136
   960
      break;
melunko@47
   961
melunko@47
   962
    case GST_NUV_DEMUX_EXTRA_DATA:
melunko@47
   963
      res = gst_nuv_demux_stream_extra_data (nuv);
melunko@47
   964
      if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
melunko@47
   965
        goto pause;
melunko@47
   966
      }
renatofilho@136
   967
      break;
melunko@47
   968
melunko@47
   969
    case GST_NUV_DEMUX_MPEG_DATA:
melunko@47
   970
      res = gst_nuv_demux_stream_mpeg_data (nuv);
melunko@47
   971
      if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
melunko@47
   972
        goto pause;
melunko@47
   973
      }
renatofilho@136
   974
      break;
melunko@47
   975
melunko@47
   976
    case GST_NUV_DEMUX_EXTEND_HEADER:
melunko@47
   977
      res = gst_nuv_demux_stream_extend_header (nuv);
melunko@47
   978
      if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
melunko@47
   979
        goto pause;
melunko@47
   980
      }
renatofilho@136
   981
      break;
melunko@47
   982
melunko@47
   983
    case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
melunko@47
   984
      res = gst_nuv_demux_stream_extend_header_data (nuv);
melunko@47
   985
      if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
melunko@47
   986
        goto pause;
melunko@47
   987
      }
renatofilho@136
   988
      break;
melunko@47
   989
melunko@47
   990
    case GST_NUV_DEMUX_FRAME_HEADER:
melunko@47
   991
      res = gst_nuv_demux_read_head_frame (nuv);
melunko@47
   992
      if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
melunko@47
   993
        goto pause;
melunko@47
   994
      }
renatofilho@136
   995
      break;
melunko@47
   996
melunko@47
   997
    case GST_NUV_DEMUX_MOVI:
melunko@47
   998
      res = gst_nuv_demux_stream_data (nuv);
renatofilho@77
   999
      if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
melunko@47
  1000
        goto pause;
melunko@47
  1001
      }
melunko@47
  1002
      break;
renatofilho@136
  1003
melunko@47
  1004
    case GST_NUV_DEMUX_INVALID_DATA:
melunko@47
  1005
      goto pause;
melunko@47
  1006
      break;
melunko@47
  1007
    default:
melunko@47
  1008
      g_assert_not_reached ();
melunko@47
  1009
  }
melunko@47
  1010
melunko@47
  1011
  return GST_FLOW_OK;
melunko@47
  1012
melunko@47
  1013
pause:
melunko@47
  1014
  GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
renatofilho@91
  1015
  gst_pad_pause_task (nuv->priv->sinkpad);
melunko@47
  1016
  if (GST_FLOW_IS_FATAL (res)) {
melunko@47
  1017
    GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
melunko@47
  1018
        (_("Internal data stream error.")),
melunko@47
  1019
        ("streaming stopped, reason %s", gst_flow_get_name (res)));
melunko@47
  1020
melunko@47
  1021
    gst_nuv_demux_send_eos (nuv);
melunko@47
  1022
  }
melunko@47
  1023
  return res;
melunko@47
  1024
}
melunko@47
  1025
melunko@47
  1026
static void
melunko@47
  1027
gst_nuv_demux_send_eos (GstNuvDemux * nuv)
melunko@47
  1028
{
melunko@47
  1029
  gst_element_post_message (GST_ELEMENT (nuv),
melunko@47
  1030
      gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
melunko@47
  1031
renatofilho@91
  1032
  if (nuv->priv->src_video_pad)
renatofilho@91
  1033
    gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
renatofilho@91
  1034
  if (nuv->priv->src_audio_pad)
renatofilho@91
  1035
    gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
melunko@47
  1036
}
renatofilho@91
  1037
melunko@47
  1038
static GstFlowReturn
melunko@47
  1039
gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
melunko@47
  1040
    GstBuffer ** buffer)
melunko@47
  1041
{
melunko@47
  1042
  GstFlowReturn ret = GST_FLOW_OK;
melunko@47
  1043
melunko@47
  1044
  if (size == 0) {
melunko@47
  1045
    return ret;
melunko@47
  1046
  }
renatofilho@91
  1047
renatofilho@91
  1048
  if (nuv->priv->mode == NUV_PULL_MODE) {
renatofilho@91
  1049
    ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
melunko@47
  1050
    if (ret == GST_FLOW_OK) {
renatofilho@77
  1051
        if (move) {
renatofilho@91
  1052
		    nuv->priv->offset += size;
renatofilho@77
  1053
		}
melunko@47
  1054
      /* got eos */
melunko@47
  1055
    } else if (ret == GST_FLOW_UNEXPECTED) {
renatofilho@77
  1056
      if (buffer != NULL)
renatofilho@77
  1057
          gst_buffer_unref (buffer);
renatofilho@91
  1058
melunko@47
  1059
      gst_nuv_demux_send_eos (nuv);
melunko@47
  1060
      return GST_FLOW_WRONG_STATE;
melunko@47
  1061
    }
melunko@47
  1062
  } else {
renatofilho@194
  1063
    if (gst_adapter_available (nuv->priv->adapter) < size) {
renatofilho@194
  1064
      nuv->priv->more_data = TRUE;
melunko@47
  1065
      return GST_FLOW_ERROR_NO_DATA;
renatofilho@194
  1066
    }
melunko@47
  1067
    if (move) {
renatofilho@181
  1068
      *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
melunko@47
  1069
    } else {
renatofilho@156
  1070
      guint8 *data = NULL;
renatofilho@156
  1071
      data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
renatofilho@156
  1072
      *buffer = gst_buffer_new ();
renatofilho@165
  1073
      gst_buffer_set_data (*buffer, data, size);
melunko@47
  1074
    }
melunko@47
  1075
  }
melunko@47
  1076
  return ret;
melunko@47
  1077
}
melunko@47
  1078
melunko@47
  1079
static gboolean
melunko@47
  1080
gst_nuv_demux_sink_activate (GstPad * sinkpad)
melunko@47
  1081
{
melunko@47
  1082
  gboolean res = TRUE;
melunko@47
  1083
  GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
melunko@47
  1084
melunko@47
  1085
  if (gst_pad_check_pull_range (sinkpad)) {
rosfran@193
  1086
    if (nuv->priv->adapter) {
rosfran@193
  1087
      gst_adapter_clear (nuv->priv->adapter);
rosfran@193
  1088
      g_object_unref (nuv->priv->adapter);
rosfran@193
  1089
      nuv->priv->adapter = NULL;
rosfran@193
  1090
    }
melunko@47
  1091
    res = gst_pad_activate_pull (sinkpad, TRUE);
melunko@47
  1092
  } else {
rosfran@193
  1093
    if (nuv->priv->adapter) {
rosfran@193
  1094
      gst_adapter_clear (nuv->priv->adapter);
rosfran@193
  1095
    } else {
rosfran@193
  1096
      nuv->priv->adapter = gst_adapter_new ();
rosfran@193
  1097
    }
melunko@47
  1098
    res = gst_pad_activate_push (sinkpad, TRUE);
melunko@47
  1099
  }
renatofilho@173
  1100
  
melunko@47
  1101
  g_object_unref (nuv);
melunko@47
  1102
  return res;
melunko@47
  1103
}
melunko@47
  1104
melunko@47
  1105
static gboolean
melunko@47
  1106
gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
melunko@47
  1107
{
melunko@47
  1108
  GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
melunko@47
  1109
melunko@47
  1110
  if (active) {
renatofilho@85
  1111
    GST_DEBUG_OBJECT (nuv, "activating pull function");
renatofilho@91
  1112
    nuv->priv->mode = NUV_PULL_MODE;
renatofilho@91
  1113
    if (nuv->priv->adapter) {
renatofilho@91
  1114
      gst_adapter_clear (nuv->priv->adapter);
renatofilho@91
  1115
      g_object_unref (nuv->priv->adapter);
renatofilho@91
  1116
      nuv->priv->adapter = NULL;
renatofilho@79
  1117
    }
melunko@47
  1118
    gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
melunko@47
  1119
  } else {
renatofilho@85
  1120
    GST_DEBUG_OBJECT (nuv, "deactivating pull function");
melunko@47
  1121
    gst_pad_stop_task (sinkpad);
melunko@47
  1122
  }
melunko@47
  1123
  gst_object_unref (nuv);
melunko@47
  1124
melunko@47
  1125
  return TRUE;
melunko@47
  1126
}
melunko@47
  1127
renatofilho@77
  1128
static gboolean
renatofilho@77
  1129
gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
renatofilho@77
  1130
{
renatofilho@77
  1131
  GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
renatofilho@77
  1132
renatofilho@133
  1133
renatofilho@77
  1134
  if (active) {
renatofilho@77
  1135
    GST_DEBUG_OBJECT (nuv, "activating push/chain function");
renatofilho@77
  1136
  } else {
renatofilho@77
  1137
    GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
renatofilho@77
  1138
  }
renatofilho@77
  1139
renatofilho@77
  1140
  gst_object_unref (nuv);
renatofilho@77
  1141
renatofilho@77
  1142
  return TRUE;
renatofilho@77
  1143
}
renatofilho@77
  1144
renatofilho@91
  1145
renatofilho@91
  1146
static gboolean
renatofilho@91
  1147
gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event)
renatofilho@91
  1148
{
renatofilho@91
  1149
  gboolean res;
renatofilho@91
  1150
  GstNuvDemux *nuv;
renatofilho@91
  1151
renatofilho@91
  1152
  nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
renatofilho@91
  1153
renatofilho@91
  1154
  switch (GST_EVENT_TYPE (event)) {
renatofilho@91
  1155
    case GST_EVENT_SEEK:
renatofilho@91
  1156
    {
renatofilho@91
  1157
      gdouble rate;
renatofilho@91
  1158
      GstFormat format;
renatofilho@91
  1159
      GstSeekFlags flags;
renatofilho@91
  1160
      GstSeekType cur_type;
renatofilho@91
  1161
      gint64 cur;
renatofilho@91
  1162
      GstSeekType stop_type;
renatofilho@91
  1163
      gint64 stop;
renatofilho@91
  1164
renatofilho@91
  1165
      gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur, &stop_type, &stop);
renatofilho@91
  1166
      GST_DEBUG_OBJECT (nuv, "got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop);
renatofilho@91
  1167
      break;
renatofilho@91
  1168
    }
renatofilho@91
  1169
    case GST_EVENT_NEWSEGMENT:
renatofilho@91
  1170
    {
renatofilho@91
  1171
      gint64 start, stop, time;
renatofilho@91
  1172
      gdouble rate, arate;
renatofilho@91
  1173
      GstFormat format;
renatofilho@91
  1174
      gboolean update;
renatofilho@91
  1175
renatofilho@91
  1176
      GST_DEBUG_OBJECT (nuv, "got a new segment event");
renatofilho@167
  1177
      gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &time);
renatofilho@91
  1178
renatofilho@91
  1179
      GST_DEBUG_OBJECT (nuv, "got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop);
renatofilho@91
  1180
      nuv->priv->duration_bytes = stop - start;
renatofilho@91
  1181
      gst_event_unref (event);
renatofilho@91
  1182
      res = TRUE;
renatofilho@91
  1183
      break;
renatofilho@91
  1184
    }
renatofilho@91
  1185
    case GST_EVENT_QOS:
renatofilho@91
  1186
    {
renatofilho@91
  1187
/*
renatofilho@91
  1188
      gdouble proportion;
renatofilho@91
  1189
      GstClockTimeDiff diff;
renatofilho@91
  1190
      GstClockTime timestamp;
renatofilho@91
  1191
renatofilho@91
  1192
      gst_event_parse_qos (event, &proportion, &diff, &timestamp);
renatofilho@91
  1193
      if (diff > 0)
renatofilho@91
  1194
        nuv->time_qos = timecode + diff;
renatofilho@91
  1195
      else
renatofilho@91
  1196
        nuv->time_qos = -1;
renatofilho@91
  1197
*/
renatofilho@91
  1198
      break;
renatofilho@91
  1199
    }
renatofilho@91
  1200
renatofilho@91
  1201
    case GST_EVENT_EOS:
renatofilho@91
  1202
    default:
renatofilho@91
  1203
      res = gst_pad_event_default (pad, event);
renatofilho@91
  1204
      break;
renatofilho@91
  1205
  }
renatofilho@91
  1206
renatofilho@91
  1207
  gst_object_unref (nuv);
renatofilho@91
  1208
  return res;
renatofilho@91
  1209
}
renatofilho@91
  1210
melunko@47
  1211
static GstFlowReturn
melunko@47
  1212
gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
melunko@47
  1213
{
renatofilho@194
  1214
  GstFlowReturn ret = GST_FLOW_OK;
renatofilho@194
  1215
melunko@47
  1216
  GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
melunko@47
  1217
renatofilho@194
  1218
  gst_adapter_push (nuv->priv->adapter, buf);  
rosfran@193
  1219
  
renatofilho@194
  1220
  while ((ret == GST_FLOW_OK) && (nuv->priv->more_data == FALSE)) {
renatofilho@194
  1221
      ret = gst_nuv_demux_play (pad);
rosfran@193
  1222
  }
renatofilho@194
  1223
renatofilho@194
  1224
  nuv->priv->more_data = FALSE;
renatofilho@194
  1225
  gst_object_unref (nuv);
rosfran@193
  1226
  
renatofilho@194
  1227
  return ret;
melunko@47
  1228
}
melunko@47
  1229
melunko@47
  1230
static void
melunko@47
  1231
gst_nuv_demux_loop (GstPad * pad)
melunko@47
  1232
{
melunko@47
  1233
  gst_nuv_demux_play (pad);
melunko@47
  1234
}
melunko@47
  1235
melunko@47
  1236
static void
melunko@47
  1237
gst_nuv_demux_reset (GstNuvDemux * nuv)
melunko@47
  1238
{
renatofilho@194
  1239
  nuv->priv->more_data = FALSE;
renatofilho@91
  1240
  nuv->priv->state = GST_NUV_DEMUX_START;
renatofilho@91
  1241
  nuv->priv->mode = 0;
renatofilho@91
  1242
  nuv->priv->offset = 0;
renatofilho@91
  1243
  nuv->priv->streamer_offset = 0;
renatofilho@91
  1244
  nuv->priv->time_start = 0;
renatofilho@91
  1245
  nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
renatofilho@91
  1246
  nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
renatofilho@91
  1247
  nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
renatofilho@91
  1248
  nuv->priv->duration_average = GST_CLOCK_TIME_NONE;
renatofilho@91
  1249
  gst_segment_init (&nuv->priv->segment, GST_FORMAT_TIME);
melunko@47
  1250
renatofilho@182
  1251
  if (nuv->priv->adapter != NULL) {
renatofilho@91
  1252
    gst_adapter_clear (nuv->priv->adapter);
renatofilho@182
  1253
  }
melunko@47
  1254
renatofilho@91
  1255
  if (nuv->priv->mpeg_buffer != NULL) {
renatofilho@91
  1256
    gst_buffer_unref (nuv->priv->mpeg_buffer);
renatofilho@91
  1257
    nuv->priv->mpeg_buffer = NULL;
melunko@47
  1258
  }
melunko@47
  1259
}
melunko@47
  1260
melunko@47
  1261
static void
melunko@47
  1262
gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
melunko@47
  1263
{
renatofilho@91
  1264
  if (nuv->priv->src_video_pad) {
renatofilho@91
  1265
    gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
renatofilho@91
  1266
    nuv->priv->src_video_pad = NULL;
melunko@47
  1267
  }
melunko@47
  1268
renatofilho@91
  1269
  if (nuv->priv->src_audio_pad) {
renatofilho@91
  1270
    gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
renatofilho@91
  1271
    nuv->priv->src_audio_pad = NULL;
melunko@47
  1272
  }
melunko@47
  1273
}
melunko@47
  1274
melunko@47
  1275
static GstStateChangeReturn
melunko@47
  1276
gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
melunko@47
  1277
{
rosfran@62
  1278
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
rosfran@73
  1279
melunko@47
  1280
  switch (transition) {
renatofilho@136
  1281
    case GST_STATE_CHANGE_NULL_TO_READY:
renatofilho@91
  1282
      gst_nuv_demux_reset (GST_NUV_DEMUX (element));
renatofilho@133
  1283
      gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
melunko@47
  1284
      break;
melunko@47
  1285
    default:
melunko@47
  1286
      break;
melunko@47
  1287
  }
melunko@47
  1288
melunko@47
  1289
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
renatofilho@136
  1290
  if (ret == GST_STATE_CHANGE_FAILURE) {
melunko@47
  1291
    goto done;
renatofilho@136
  1292
  }
melunko@47
  1293
melunko@47
  1294
  switch (transition) {
renatofilho@91
  1295
    case GST_STATE_CHANGE_READY_TO_NULL:
renatofilho@91
  1296
      gst_nuv_demux_reset (GST_NUV_DEMUX (element));
renatofilho@66
  1297
      gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
melunko@47
  1298
      break;
melunko@47
  1299
    default:
melunko@47
  1300
      break;
melunko@47
  1301
  }
melunko@47
  1302
melunko@47
  1303
done:
melunko@47
  1304
  return ret;
melunko@47
  1305
}
melunko@47
  1306
renatofilho@91
  1307
static void
renatofilho@91
  1308
gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp)
renatofilho@91
  1309
{
renatofilho@91
  1310
    guint64 interval = 0;
renatofilho@91
  1311
renatofilho@91
  1312
    if (gst_nuv_demux_get_time_duration (nuv) != GST_CLOCK_TIME_NONE)
renatofilho@91
  1313
        return;
renatofilho@91
  1314
renatofilho@91
  1315
    interval  = current_timestamp - nuv->priv->last_update;
renatofilho@91
  1316
renatofilho@91
  1317
    if (interval > (10 * GST_SECOND)) {
renatofilho@156
  1318
        GstMessage* msg = NULL;
renatofilho@91
  1319
        gint64 average = 0;
renatofilho@91
  1320
        gint64 duration_bytes = gst_nuv_demux_get_bytes_duration (nuv);
renatofilho@91
  1321
renatofilho@194
  1322
        if (duration_bytes == GST_CLOCK_TIME_NONE) {
renatofilho@91
  1323
            return;
renatofilho@194
  1324
        }
renatofilho@91
  1325
renatofilho@168
  1326
        interval = gst_util_uint64_scale (1, current_timestamp - nuv->priv->time_start, GST_SECOND);
renatofilho@168
  1327
        if (interval > 0) {
renatofilho@168
  1328
            average = gst_util_uint64_scale (1 , nuv->priv->streamer_offset, interval);
renatofilho@136
  1329
        }
renatofilho@168
  1330
        if (average > 0) {
renatofilho@168
  1331
            nuv->priv->duration_average = gst_util_uint64_scale (GST_SECOND, duration_bytes, average);
renatofilho@168
  1332
        }
renatofilho@168
  1333
        
renatofilho@168
  1334
        nuv->priv->last_update = current_timestamp;
renatofilho@168
  1335
        msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_average);
renatofilho@168
  1336
        gst_element_post_message (GST_ELEMENT (nuv), msg);
renatofilho@168
  1337
        GST_DEBUG_OBJECT (nuv, "New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
renatofilho@209
  1338
        g_debug ("New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
renatofilho@91
  1339
    }
renatofilho@91
  1340
}
renatofilho@91
  1341
renatofilho@91
  1342
static gint64
renatofilho@91
  1343
gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv)
renatofilho@91
  1344
{
renatofilho@91
  1345
    if (nuv->priv->duration_bytes == GST_CLOCK_TIME_NONE) {
renatofilho@156
  1346
        GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
renatofilho@156
  1347
        GstQuery *query = gst_query_new_duration (GST_FORMAT_BYTES);
renatofilho@156
  1348
        if (gst_pad_query (peer, query)) {
renatofilho@156
  1349
            gint64 duration;
renatofilho@156
  1350
renatofilho@156
  1351
            gst_query_parse_duration (query, NULL, &duration);
renatofilho@91
  1352
            nuv->priv->duration_bytes = duration;
renatofilho@91
  1353
        }
renatofilho@156
  1354
        gst_object_unref (peer);
renatofilho@156
  1355
        gst_query_unref (query);
renatofilho@91
  1356
    }
renatofilho@91
  1357
    return nuv->priv->duration_bytes;
renatofilho@91
  1358
}
renatofilho@91
  1359
renatofilho@91
  1360
static gint64
renatofilho@91
  1361
gst_nuv_demux_get_time_duration (GstNuvDemux *nuv)
renatofilho@91
  1362
{
renatofilho@91
  1363
    if (nuv->priv->duration_time == GST_CLOCK_TIME_NONE) {
renatofilho@156
  1364
        GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
renatofilho@156
  1365
        GstQuery *query = gst_query_new_duration (GST_FORMAT_TIME);
renatofilho@156
  1366
        if (gst_pad_query (peer, query)) {
renatofilho@156
  1367
            gint64 duration;
renatofilho@156
  1368
            gst_query_parse_duration (query, NULL, &duration);
renatofilho@91
  1369
            nuv->priv->duration_time = duration;
renatofilho@91
  1370
        }
renatofilho@156
  1371
        gst_object_unref (peer);
renatofilho@156
  1372
        gst_query_unref (query);
renatofilho@91
  1373
    }
renatofilho@91
  1374
    return nuv->priv->duration_time;
renatofilho@91
  1375
}
renatofilho@91
  1376
renatofilho@181
  1377
#if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6) 
renatofilho@173
  1378
GstBuffer *
renatofilho@181
  1379
gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
renatofilho@173
  1380
{
renatofilho@173
  1381
  GstBuffer *buffer;
renatofilho@173
  1382
  GstBuffer *cur;
renatofilho@173
  1383
  guint8 *data;
renatofilho@173
  1384
renatofilho@173
  1385
  g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
renatofilho@173
  1386
  g_return_val_if_fail (nbytes > 0, NULL);
renatofilho@173
  1387
renatofilho@173
  1388
  GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);
renatofilho@173
  1389
renatofilho@173
  1390
  /* we don't have enough data, return NULL. This is unlikely
renatofilho@173
  1391
   * as one usually does an _available() first instead of peeking a
renatofilho@173
  1392
   * random size. */
renatofilho@173
  1393
  if (G_UNLIKELY (nbytes > adapter->size))
renatofilho@173
  1394
    return NULL;
renatofilho@173
  1395
renatofilho@173
  1396
  /* our head buffer has enough data left, return it */
renatofilho@173
  1397
  cur = adapter->buflist->data;
renatofilho@173
  1398
  if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) {
renatofilho@173
  1399
    GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
renatofilho@173
  1400
        nbytes);
renatofilho@173
  1401
    buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes);
renatofilho@173
  1402
renatofilho@173
  1403
    gst_adapter_flush (adapter, nbytes);
renatofilho@173
  1404
renatofilho@173
  1405
    return buffer;
renatofilho@173
  1406
  }
renatofilho@173
  1407
renatofilho@173
  1408
  data = gst_adapter_take (adapter, nbytes);
renatofilho@173
  1409
  if (data == NULL)
renatofilho@173
  1410
    return NULL;
renatofilho@173
  1411
renatofilho@173
  1412
  buffer = gst_buffer_new ();
renatofilho@173
  1413
  GST_BUFFER_DATA (buffer) = data;
renatofilho@173
  1414
  GST_BUFFER_MALLOCDATA (buffer) = data;
renatofilho@173
  1415
  GST_BUFFER_SIZE (buffer) = nbytes;
renatofilho@173
  1416
renatofilho@173
  1417
  return buffer;
renatofilho@173
  1418
}
renatofilho@181
  1419
#endif
renatofilho@173
  1420
renatofilho@91
  1421
melunko@47
  1422
static gboolean
melunko@47
  1423
plugin_init (GstPlugin * plugin)
melunko@47
  1424
{
melunko@47
  1425
#ifdef ENABLE_NLS
melunko@47
  1426
  setlocale (LC_ALL, "");
rosfran@73
  1427
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
melunko@47
  1428
#endif /* ENABLE_NLS */
melunko@47
  1429
melunko@47
  1430
  if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
melunko@47
  1431
          GST_TYPE_NUV_DEMUX)) {
melunko@47
  1432
    return FALSE;
melunko@47
  1433
  }
melunko@47
  1434
  return TRUE;
melunko@47
  1435
}
melunko@47
  1436
melunko@47
  1437
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
melunko@47
  1438
    GST_VERSION_MINOR,
melunko@47
  1439
    "nuvdemux",
melunko@47
  1440
    "Demuxes and muxes audio and video",
renatofilho@163
  1441
     plugin_init, VERSION, "LGPL", "NuvDemux", "")
renatofilho@163
  1442