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