1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gst-plugins-nuvdemux/src/gstnuvdemux.c Thu Oct 26 02:04:40 2006 +0100
1.3 @@ -0,0 +1,926 @@
1.4 +/* GStreamer
1.5 + * Copyright (C) <2006> Renato Araujo Oliveira Filho <renato.filho@indt.org.br>
1.6 + * Rosfran Borges <rosfran.borges@indt.org.br>
1.7 + *
1.8 + * This library is free software; you can redistribute it and/or
1.9 + * modify it under the terms of the GNU Library General Public
1.10 + * License as published by the Free Software Foundation; either
1.11 + * version 2 of the License, or (at your option) any later version.
1.12 + *
1.13 + * This library is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.16 + * Library General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU Library General Public
1.19 + * License along with this library; if not, write to the
1.20 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.21 + * Boston, MA 02111-1307, USA.
1.22 + */
1.23 +/* Element-Checklist-Version: 5 */
1.24 +
1.25 +/**
1.26 + * SECTION:element-nuvdemux
1.27 + *
1.28 + * <refsect2>
1.29 + * <para>
1.30 + * Demuxes an .nuv file into raw or compressed audio and/or video streams.
1.31 + * </para>
1.32 + * <para>
1.33 + * This element currently only supports pull-based scheduling.
1.34 + * </para>
1.35 + * <title>Example launch line</title>
1.36 + * <para>
1.37 + * <programlisting>
1.38 + * gst-launch filesrc test.nuv ! nuvdemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
1.39 + * </programlisting>
1.40 + * Play (parse and decode) an .nuv file and try to output it to
1.41 + * an automatically detected soundcard and videosink. If the NUV file contains
1.42 + * compressed audio or video data, this will only work if you have the
1.43 + * right decoder elements/plugins installed.
1.44 + * </para>
1.45 + * </refsect2>
1.46 + *
1.47 + */
1.48 +
1.49 +#ifdef HAVE_CONFIG_H
1.50 +#include "config.h"
1.51 +#endif
1.52 +
1.53 +#include <gst/gst.h>
1.54 +#include <gst/gsterror.h>
1.55 +#include <gst/gstplugin.h>
1.56 +#include <string.h>
1.57 +
1.58 +//#include "gst/gst-i18n-plugin.h"
1.59 +#include <glib/gi18n.h>
1.60 +#include "gstnuvdemux.h"
1.61 +
1.62 +GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug);
1.63 +#define GST_CAT_DEFAULT nuvdemux_debug
1.64 +
1.65 +
1.66 +#define GST_FLOW_ERROR_NO_DATA -101
1.67 +
1.68 +GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
1.69 +
1.70 +static const GstElementDetails gst_nuv_demux_details =
1.71 +GST_ELEMENT_DETAILS ("Nuv demuxer",
1.72 + "Codec/Demuxer",
1.73 + "Demultiplex a .nuv file into audio and video",
1.74 + "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
1.75 + "Rosfran Borges <rosfran.borges@indt.org.br>");
1.76 +
1.77 +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
1.78 + GST_PAD_SINK,
1.79 + GST_PAD_ALWAYS,
1.80 + GST_STATIC_CAPS ("video/x-nuv"));
1.81 +
1.82 +static GstStaticPadTemplate audio_src_template =
1.83 +GST_STATIC_PAD_TEMPLATE ("audio_src",
1.84 + GST_PAD_SRC,
1.85 + GST_PAD_SOMETIMES,
1.86 + GST_STATIC_CAPS_ANY);
1.87 +
1.88 +static GstStaticPadTemplate video_src_template =
1.89 +GST_STATIC_PAD_TEMPLATE ("video_src",
1.90 + GST_PAD_SRC,
1.91 + GST_PAD_SOMETIMES,
1.92 + GST_STATIC_CAPS_ANY);
1.93 +
1.94 +/* NUV Demux indexes init/dispose callers */
1.95 +static void gst_nuv_demux_finalize (GObject * object);
1.96 +static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
1.97 + GstStateChange transition);
1.98 +static void gst_nuv_demux_loop (GstPad * pad);
1.99 +static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
1.100 +static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
1.101 +static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
1.102 + gboolean active);
1.103 +static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
1.104 +static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
1.105 + gboolean move, GstBuffer ** buffer);
1.106 +static void gst_nuv_demux_reset (GstNuvDemux * nuv);
1.107 +static gboolean gst_nuv_demux_handle_sink_event (GstPad * sinkpad,
1.108 + GstEvent * event);
1.109 +static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
1.110 +static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
1.111 +
1.112 +/* GObject methods */
1.113 +GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
1.114 +
1.115 +#if G_BYTE_ORDER == G_BIG_ENDIAN
1.116 +static inline gdouble
1.117 +_gdouble_swap_le_be (gdouble * d)
1.118 +{
1.119 + union
1.120 + {
1.121 + guint64 i;
1.122 + gdouble d;
1.123 + } u;
1.124 +
1.125 + u.d = *d;
1.126 + u.i = GUINT64_SWAP_LE_BE (u.i);
1.127 + return u.d;
1.128 +}
1.129 +
1.130 +#define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
1.131 +#else /* G_BYTE_ORDER != G_BIG_ENDIAN */
1.132 +#define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
1.133 +#endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
1.134 +
1.135 +
1.136 +static void
1.137 +gst_nuv_demux_base_init (gpointer klass)
1.138 +{
1.139 + GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1.140 +
1.141 + gst_element_class_add_pad_template (element_class,
1.142 + gst_static_pad_template_get (&audio_src_template));
1.143 +
1.144 + gst_element_class_add_pad_template (element_class,
1.145 + gst_static_pad_template_get (&video_src_template));
1.146 +
1.147 + gst_element_class_add_pad_template (element_class,
1.148 + gst_static_pad_template_get (&sink_template));
1.149 + gst_element_class_set_details (element_class, &gst_nuv_demux_details);
1.150 +}
1.151 +
1.152 +static void
1.153 +gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
1.154 +{
1.155 + GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1.156 + GObjectClass *gobject_class = (GObjectClass *) klass;
1.157 +
1.158 + GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
1.159 + 0, "Demuxer for NUV streams");
1.160 +
1.161 + parent_class = g_type_class_peek_parent (klass);
1.162 +
1.163 + gobject_class->finalize = gst_nuv_demux_finalize;
1.164 + gstelement_class->change_state = gst_nuv_demux_change_state;
1.165 +}
1.166 +
1.167 +static void
1.168 +gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
1.169 +{
1.170 + nuv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
1.171 +
1.172 + gst_pad_set_activate_function (nuv->sinkpad, gst_nuv_demux_sink_activate);
1.173 +
1.174 + gst_pad_set_activatepull_function (nuv->sinkpad,
1.175 + gst_nuv_demux_sink_activate_pull);
1.176 +
1.177 + gst_pad_set_chain_function (nuv->sinkpad,
1.178 + GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
1.179 +
1.180 + gst_pad_set_event_function (nuv->sinkpad, gst_nuv_demux_handle_sink_event);
1.181 +
1.182 + gst_element_add_pad (GST_ELEMENT (nuv), nuv->sinkpad);
1.183 +
1.184 + nuv->adapter = NULL;
1.185 + nuv->mpeg_buffer = NULL;
1.186 + nuv->h = NULL;
1.187 + nuv->eh = NULL;
1.188 + nuv->fh = NULL;
1.189 + gst_nuv_demux_reset (nuv);
1.190 +}
1.191 +
1.192 +static void
1.193 +gst_nuv_demux_finalize (GObject * object)
1.194 +{
1.195 + GstNuvDemux *nuv = GST_NUV_DEMUX (object);
1.196 +
1.197 + if (nuv->mpeg_buffer != NULL) {
1.198 + gst_buffer_unref (nuv->mpeg_buffer);
1.199 + }
1.200 +
1.201 + gst_nuv_demux_destoy_src_pad (nuv);
1.202 + gst_nuv_demux_reset (nuv);
1.203 + if (nuv->adapter != NULL) {
1.204 + gst_object_unref (nuv->adapter);
1.205 + }
1.206 + G_OBJECT_CLASS (parent_class)->finalize (object);
1.207 +}
1.208 +
1.209 +/*****************************************************************************
1.210 + * Utils fucntions
1.211 + *****************************************************************************/
1.212 +
1.213 +static gboolean
1.214 +gst_nuv_demux_handle_sink_event (GstPad * sinkpad, GstEvent * event)
1.215 +{
1.216 + gboolean res = FALSE;
1.217 +
1.218 + switch (GST_EVENT_TYPE (event)) {
1.219 + case GST_EVENT_NEWSEGMENT:
1.220 + res = TRUE;
1.221 + break;
1.222 + default:
1.223 + return gst_pad_event_default (sinkpad, event);
1.224 + break;
1.225 + }
1.226 +
1.227 + gst_event_unref (event);
1.228 + return res;
1.229 +}
1.230 +
1.231 +
1.232 +/* HeaderLoad:
1.233 + */
1.234 +static GstFlowReturn
1.235 +gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header ** h_ret)
1.236 +{
1.237 + GstBuffer *buffer = NULL;
1.238 + GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
1.239 +
1.240 + if (res != GST_FLOW_OK)
1.241 + return res;
1.242 +
1.243 + nuv_header *h = g_new0 (nuv_header, 1);
1.244 +
1.245 + memcpy (h->id, buffer->data, 12);
1.246 + memcpy (h->version, buffer->data + 12, 5);
1.247 + h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
1.248 + h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
1.249 + h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
1.250 + h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
1.251 + h->i_mode = buffer->data[36];
1.252 + h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
1.253 + h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
1.254 + h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
1.255 + h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
1.256 + h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
1.257 + h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
1.258 +
1.259 + GST_DEBUG_OBJECT (nuv,
1.260 + "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
1.261 + h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
1.262 + h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
1.263 + h->i_keyframe_distance);
1.264 +
1.265 + *h_ret = h;
1.266 + gst_buffer_unref (buffer);
1.267 + return res;
1.268 +}
1.269 +
1.270 +static GstFlowReturn
1.271 +gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
1.272 +{
1.273 + GstFlowReturn res = gst_nuv_demux_header_load (nuv, &nuv->h);
1.274 +
1.275 + if (res == GST_FLOW_OK)
1.276 + nuv->state = GST_NUV_DEMUX_EXTRA_DATA;
1.277 + return res;
1.278 +}
1.279 +
1.280 +/*
1.281 + * Read NUV file tag
1.282 + */
1.283 +static GstFlowReturn
1.284 +gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
1.285 +{
1.286 + GstFlowReturn res = GST_FLOW_OK;
1.287 + GstBuffer *file_header = NULL;
1.288 +
1.289 + res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
1.290 + if (res != GST_FLOW_OK) {
1.291 + return res;
1.292 + } else {
1.293 + if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
1.294 + strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
1.295 + nuv->state = GST_NUV_DEMUX_HEADER_DATA;
1.296 + } else {
1.297 + GST_DEBUG_OBJECT (nuv, "error parsing file header");
1.298 + nuv->state = GST_NUV_DEMUX_INVALID_DATA;
1.299 + res = GST_FLOW_ERROR;
1.300 + }
1.301 + }
1.302 + if (file_header != NULL) {
1.303 + gst_buffer_unref (file_header);
1.304 + }
1.305 + return res;
1.306 +}
1.307 +
1.308 +/* FrameHeaderLoad:
1.309 + */
1.310 +static GstFlowReturn
1.311 +gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header ** h_ret)
1.312 +{
1.313 + unsigned char *data;
1.314 + nuv_frame_header *h;
1.315 + GstBuffer *buf = NULL;
1.316 +
1.317 + GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
1.318 +
1.319 + if (res != GST_FLOW_OK) {
1.320 + if (buf != NULL) {
1.321 + gst_buffer_unref (buf);
1.322 + }
1.323 + return res;
1.324 + }
1.325 +
1.326 + h = g_new0 (nuv_frame_header, 1);
1.327 + data = buf->data;
1.328 +
1.329 + h->i_type = data[0];
1.330 + h->i_compression = data[1];
1.331 + h->i_keyframe = data[2];
1.332 + h->i_filters = data[3];
1.333 +
1.334 + h->i_timecode = GST_READ_UINT32_LE (&data[4]);
1.335 + h->i_length = GST_READ_UINT32_LE (&data[8]);
1.336 + GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%u l=%u",
1.337 + h->i_type,
1.338 + h->i_compression ? h->i_compression : ' ',
1.339 + h->i_keyframe ? h->i_keyframe : ' ',
1.340 + h->i_filters, h->i_timecode, h->i_length);
1.341 +
1.342 + *h_ret = h;
1.343 + gst_buffer_unref (buf);
1.344 + return GST_FLOW_OK;
1.345 +}
1.346 +
1.347 +static GstFlowReturn
1.348 +gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
1.349 + nuv_extended_header ** h_ret)
1.350 +{
1.351 + unsigned char *data;
1.352 + GstBuffer *buff = NULL;
1.353 + nuv_extended_header *h;
1.354 +
1.355 +
1.356 + GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
1.357 +
1.358 + if (res != GST_FLOW_OK) {
1.359 + if (buff != NULL) {
1.360 + gst_buffer_unref (buff);
1.361 + }
1.362 + return res;
1.363 + }
1.364 +
1.365 + h = g_new0 (nuv_extended_header, 1);
1.366 + data = buff->data;
1.367 + h->i_version = GST_READ_UINT32_LE (&data[0]);
1.368 + h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
1.369 + h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
1.370 + h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
1.371 + h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
1.372 + h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
1.373 + h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
1.374 + h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
1.375 + h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
1.376 + h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
1.377 + h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
1.378 + h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
1.379 + h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
1.380 + h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
1.381 + h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
1.382 + h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
1.383 + h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
1.384 +
1.385 + GST_DEBUG_OBJECT (nuv,
1.386 + "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
1.387 + "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
1.388 + h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
1.389 + h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
1.390 + h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
1.391 + h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
1.392 + h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
1.393 + h->i_keyframe_adjust_offset);
1.394 +
1.395 + *h_ret = h;
1.396 + gst_buffer_unref (buff);
1.397 + return res;
1.398 +}
1.399 +
1.400 +static gboolean
1.401 +gst_nuv_demux_handle_src_event (GstPad * pad, GstEvent * event)
1.402 +{
1.403 + gst_event_unref (event);
1.404 + return FALSE;
1.405 +}
1.406 +
1.407 +
1.408 +static void
1.409 +gst_nuv_demux_create_pads (GstNuvDemux * nuv)
1.410 +{
1.411 + if (nuv->h->i_video_blocks != 0) {
1.412 + GstCaps *video_caps = NULL;
1.413 +
1.414 + nuv->src_video_pad =
1.415 + gst_pad_new_from_static_template (&video_src_template, "video_src");
1.416 +
1.417 + video_caps = gst_caps_new_simple ("video/x-divx",
1.418 + "divxversion", G_TYPE_INT, 4,
1.419 + "width", G_TYPE_INT, nuv->h->i_width,
1.420 + "height", G_TYPE_INT, nuv->h->i_height,
1.421 + "framerate", GST_TYPE_FRACTION, (gint) (nuv->h->d_fps * 1000.0f), 1000,
1.422 + "pixel-aspect-ratio", GST_TYPE_FRACTION,
1.423 + (gint) (nuv->h->d_aspect * 1000.0f), 1000, NULL);
1.424 +
1.425 + gst_pad_use_fixed_caps (nuv->src_video_pad);
1.426 + gst_pad_set_active (nuv->src_video_pad, TRUE);
1.427 + gst_pad_set_caps (nuv->src_video_pad, video_caps);
1.428 +
1.429 + gst_pad_set_event_function (nuv->src_video_pad,
1.430 + gst_nuv_demux_handle_src_event);
1.431 + gst_pad_set_active (nuv->src_video_pad, TRUE);
1.432 + gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
1.433 + gst_caps_unref (video_caps);
1.434 + }
1.435 +
1.436 + if (nuv->h->i_audio_blocks != 0) {
1.437 + GstCaps *audio_caps = NULL;
1.438 +
1.439 + nuv->src_audio_pad =
1.440 + gst_pad_new_from_static_template (&audio_src_template, "audio_src");
1.441 +
1.442 + audio_caps = gst_caps_new_simple ("audio/mpeg",
1.443 + "rate", G_TYPE_INT, nuv->eh->i_audio_sample_rate,
1.444 + "format", GST_TYPE_FOURCC, nuv->eh->i_audio_fcc,
1.445 + "channels", G_TYPE_INT, nuv->eh->i_audio_channels,
1.446 + "mpegversion", G_TYPE_INT, nuv->eh->i_version, NULL);
1.447 +
1.448 + gst_pad_use_fixed_caps (nuv->src_audio_pad);
1.449 + gst_pad_set_active (nuv->src_audio_pad, TRUE);
1.450 + gst_pad_set_caps (nuv->src_audio_pad, audio_caps);
1.451 + gst_pad_set_active (nuv->src_audio_pad, TRUE);
1.452 + gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
1.453 +
1.454 + gst_pad_set_event_function (nuv->src_audio_pad,
1.455 + gst_nuv_demux_handle_src_event);
1.456 + gst_caps_unref (audio_caps);
1.457 + }
1.458 +
1.459 + gst_element_no_more_pads (GST_ELEMENT (nuv));
1.460 +}
1.461 +
1.462 +static GstFlowReturn
1.463 +gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
1.464 +{
1.465 + GstFlowReturn ret = GST_FLOW_OK;
1.466 +
1.467 + ret = gst_nuv_demux_frame_header_load (nuv, &nuv->fh);
1.468 + if (ret != GST_FLOW_OK)
1.469 + return ret;
1.470 +
1.471 + nuv->state = GST_NUV_DEMUX_MOVI;
1.472 + return ret;
1.473 +}
1.474 +
1.475 +static GstFlowReturn
1.476 +gst_nuv_demux_stream_data (GstNuvDemux * nuv)
1.477 +{
1.478 + GstFlowReturn ret = GST_FLOW_OK;
1.479 + GstBuffer *buf = NULL;
1.480 + nuv_frame_header *h = nuv->fh;
1.481 +
1.482 + if (h->i_type == 'R')
1.483 + goto done;
1.484 +
1.485 + ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
1.486 + if (ret != GST_FLOW_OK) {
1.487 + return ret;
1.488 + }
1.489 +
1.490 + GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND;
1.491 +
1.492 + switch (h->i_type) {
1.493 + case 'V':
1.494 + {
1.495 + GST_BUFFER_OFFSET (buf) = nuv->video_offset;
1.496 + gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_video_pad));
1.497 + ret = gst_pad_push (nuv->src_video_pad, buf);
1.498 + nuv->video_offset++;
1.499 + break;
1.500 + }
1.501 + case 'A':
1.502 + {
1.503 + GST_BUFFER_OFFSET (buf) = nuv->audio_offset;
1.504 + gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_audio_pad));
1.505 + ret = gst_pad_push (nuv->src_audio_pad, buf);
1.506 + nuv->audio_offset++;
1.507 + break;
1.508 + }
1.509 + case 'S':
1.510 + {
1.511 + switch (h->i_compression) {
1.512 + case 'V':
1.513 + gst_pad_push_event (nuv->src_video_pad,
1.514 + gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
1.515 + break;
1.516 + case 'A':
1.517 + gst_pad_push_event (nuv->src_audio_pad,
1.518 + gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
1.519 +
1.520 + break;
1.521 + default:
1.522 + break;
1.523 + }
1.524 + break;
1.525 + }
1.526 + }
1.527 +
1.528 +done:
1.529 + nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
1.530 + g_free (nuv->fh);
1.531 + nuv->fh = NULL;
1.532 + return ret;
1.533 +}
1.534 +
1.535 +static GstFlowReturn
1.536 +gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
1.537 +{
1.538 + GstFlowReturn ret = GST_FLOW_OK;
1.539 +
1.540 + /* ffmpeg extra data */
1.541 + ret =
1.542 + gst_nuv_demux_read_bytes (nuv, nuv->mpeg_data_size, TRUE,
1.543 + &nuv->mpeg_buffer);
1.544 + if (ret != GST_FLOW_OK) {
1.545 + return GST_FLOW_ERROR;
1.546 + }
1.547 + GST_BUFFER_SIZE (nuv->mpeg_buffer) = nuv->mpeg_data_size;
1.548 + nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
1.549 + return ret;
1.550 +}
1.551 +
1.552 +static GstFlowReturn
1.553 +gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
1.554 +{
1.555 + GstFlowReturn ret = GST_FLOW_OK;
1.556 +
1.557 + /* Load 'D' */
1.558 + nuv_frame_header *h;
1.559 +
1.560 + ret = gst_nuv_demux_frame_header_load (nuv, &h);
1.561 + if (ret != GST_FLOW_OK)
1.562 + return ret;
1.563 +
1.564 + if (h->i_type != 'D') {
1.565 + g_free (h);
1.566 + return GST_FLOW_ERROR;
1.567 + }
1.568 +
1.569 + if (h->i_length > 0) {
1.570 + if (h->i_compression == 'F') {
1.571 + nuv->state = GST_NUV_DEMUX_MPEG_DATA;
1.572 + } else {
1.573 + g_free (h);
1.574 + return GST_FLOW_ERROR;
1.575 + }
1.576 + } else {
1.577 + nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
1.578 + }
1.579 +
1.580 + g_free (h);
1.581 + h = NULL;
1.582 + return ret;
1.583 +}
1.584 +
1.585 +static GstFlowReturn
1.586 +gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
1.587 +{
1.588 + GstFlowReturn ret = GST_FLOW_OK;
1.589 +
1.590 + ret = gst_nuv_demux_extended_header_load (nuv, &nuv->eh);
1.591 + if (ret != GST_FLOW_OK)
1.592 + return ret;
1.593 +
1.594 + gst_nuv_demux_create_pads (nuv);
1.595 + nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
1.596 + return ret;
1.597 +}
1.598 +
1.599 +static GstFlowReturn
1.600 +gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
1.601 +{
1.602 + GstBuffer *buf = NULL;
1.603 + GstFlowReturn res = GST_FLOW_OK;
1.604 +
1.605 + res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
1.606 + if (res != GST_FLOW_OK) {
1.607 + if (buf != NULL) {
1.608 + gst_buffer_unref (buf);
1.609 + }
1.610 + return res;
1.611 + }
1.612 +
1.613 + if (buf->data[0] == 'X') {
1.614 + gst_buffer_unref (buf);
1.615 + buf = NULL;
1.616 + nuv_frame_header *h = NULL;
1.617 +
1.618 + res = gst_nuv_demux_frame_header_load (nuv, &h);
1.619 + if (res != GST_FLOW_OK)
1.620 + return res;
1.621 +
1.622 + if (h->i_length != 512) {
1.623 + g_free (h);
1.624 + return GST_FLOW_ERROR;
1.625 + }
1.626 + g_free (h);
1.627 + h = NULL;
1.628 + nuv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
1.629 + } else {
1.630 + nuv->state = GST_NUV_DEMUX_INVALID_DATA;
1.631 + g_object_unref (buf);
1.632 + GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
1.633 + (_("incomplete NUV support")), ("incomplete NUV support"));
1.634 + return GST_FLOW_ERROR;
1.635 + }
1.636 + return res;
1.637 +}
1.638 +
1.639 +static GstFlowReturn
1.640 +gst_nuv_demux_play (GstPad * pad)
1.641 +{
1.642 + GstFlowReturn res = GST_FLOW_OK;
1.643 + GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
1.644 +
1.645 + switch (nuv->state) {
1.646 + case GST_NUV_DEMUX_START:
1.647 + res = gst_nuv_demux_stream_file_header (nuv);
1.648 + if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1.649 + goto pause;
1.650 + }
1.651 + if (nuv->state != GST_NUV_DEMUX_HEADER_DATA)
1.652 + break;
1.653 +
1.654 + case GST_NUV_DEMUX_HEADER_DATA:
1.655 + res = gst_nuv_demux_stream_header_data (nuv);
1.656 + if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1.657 + goto pause;
1.658 + }
1.659 + if (nuv->state != GST_NUV_DEMUX_EXTRA_DATA)
1.660 + break;
1.661 +
1.662 + case GST_NUV_DEMUX_EXTRA_DATA:
1.663 + res = gst_nuv_demux_stream_extra_data (nuv);
1.664 + if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1.665 + goto pause;
1.666 + }
1.667 + if (nuv->state != GST_NUV_DEMUX_MPEG_DATA)
1.668 + break;
1.669 +
1.670 + case GST_NUV_DEMUX_MPEG_DATA:
1.671 + res = gst_nuv_demux_stream_mpeg_data (nuv);
1.672 + if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1.673 + goto pause;
1.674 + }
1.675 +
1.676 + if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER)
1.677 + break;
1.678 +
1.679 + case GST_NUV_DEMUX_EXTEND_HEADER:
1.680 + res = gst_nuv_demux_stream_extend_header (nuv);
1.681 + if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1.682 + goto pause;
1.683 + }
1.684 + if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER_DATA)
1.685 + break;
1.686 +
1.687 + case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
1.688 + res = gst_nuv_demux_stream_extend_header_data (nuv);
1.689 + if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1.690 + goto pause;
1.691 + }
1.692 +
1.693 + if (nuv->state != GST_NUV_DEMUX_FRAME_HEADER)
1.694 + break;
1.695 +
1.696 + case GST_NUV_DEMUX_FRAME_HEADER:
1.697 + res = gst_nuv_demux_read_head_frame (nuv);
1.698 + if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1.699 + goto pause;
1.700 + }
1.701 + if (nuv->state != GST_NUV_DEMUX_MOVI)
1.702 + break;
1.703 +
1.704 + case GST_NUV_DEMUX_MOVI:
1.705 + res = gst_nuv_demux_stream_data (nuv);
1.706 + if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1.707 + goto pause;
1.708 + }
1.709 + break;
1.710 + case GST_NUV_DEMUX_INVALID_DATA:
1.711 + goto pause;
1.712 + break;
1.713 + default:
1.714 + g_assert_not_reached ();
1.715 + }
1.716 +
1.717 + GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->state,
1.718 + gst_flow_get_name (res));
1.719 +
1.720 + return GST_FLOW_OK;
1.721 +
1.722 +pause:
1.723 + GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
1.724 + gst_pad_pause_task (nuv->sinkpad);
1.725 + if (GST_FLOW_IS_FATAL (res)) {
1.726 + GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
1.727 + (_("Internal data stream error.")),
1.728 + ("streaming stopped, reason %s", gst_flow_get_name (res)));
1.729 +
1.730 + gst_nuv_demux_send_eos (nuv);
1.731 + }
1.732 + return res;
1.733 +}
1.734 +
1.735 +static void
1.736 +gst_nuv_demux_send_eos (GstNuvDemux * nuv)
1.737 +{
1.738 + gst_element_post_message (GST_ELEMENT (nuv),
1.739 + gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
1.740 +
1.741 + if (nuv->src_video_pad)
1.742 + gst_pad_push_event (nuv->src_video_pad, gst_event_new_eos ());
1.743 + if (nuv->src_audio_pad)
1.744 + gst_pad_push_event (nuv->src_audio_pad, gst_event_new_eos ());
1.745 +}
1.746 +
1.747 +static GstFlowReturn
1.748 +gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
1.749 + GstBuffer ** buffer)
1.750 +{
1.751 + GstFlowReturn ret = GST_FLOW_OK;
1.752 +
1.753 + if (size == 0) {
1.754 + *buffer = gst_buffer_new ();
1.755 + return ret;
1.756 + }
1.757 +
1.758 + if (nuv->mode == 0) {
1.759 + ret = gst_pad_pull_range (nuv->sinkpad, nuv->offset, size, buffer);
1.760 + if (ret == GST_FLOW_OK) {
1.761 + if (move) {
1.762 + nuv->offset += size;
1.763 + }
1.764 + /* got eos */
1.765 + } else if (ret == GST_FLOW_UNEXPECTED) {
1.766 + gst_nuv_demux_send_eos (nuv);
1.767 + return GST_FLOW_WRONG_STATE;
1.768 + }
1.769 + } else {
1.770 + if (gst_adapter_available (nuv->adapter) < size)
1.771 + return GST_FLOW_ERROR_NO_DATA;
1.772 +
1.773 + if (move) {
1.774 + guint8 *data = NULL;
1.775 + data = (guint8 *) gst_adapter_take (nuv->adapter, size);
1.776 + *buffer = gst_buffer_new ();
1.777 + gst_buffer_set_data (*buffer, data, size);
1.778 + } else {
1.779 + guint8 *data = NULL;
1.780 +
1.781 + data = (guint8 *) gst_adapter_peek (nuv->adapter, size);
1.782 + *buffer = gst_buffer_new ();
1.783 + gst_buffer_set_data (*buffer, data, size);
1.784 + }
1.785 + }
1.786 + return ret;
1.787 +}
1.788 +
1.789 +static gboolean
1.790 +gst_nuv_demux_sink_activate (GstPad * sinkpad)
1.791 +{
1.792 + gboolean res = TRUE;
1.793 + GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1.794 +
1.795 + if (gst_pad_check_pull_range (sinkpad)) {
1.796 + nuv->mode = 0;
1.797 + nuv->adapter = NULL;
1.798 + res = gst_pad_activate_pull (sinkpad, TRUE);
1.799 + } else {
1.800 + nuv->mode = 1;
1.801 + nuv->adapter = gst_adapter_new ();
1.802 + res = gst_pad_activate_push (sinkpad, TRUE);
1.803 + }
1.804 +
1.805 + g_object_unref (nuv);
1.806 + return res;
1.807 +}
1.808 +
1.809 +static gboolean
1.810 +gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
1.811 +{
1.812 + GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1.813 +
1.814 + if (active) {
1.815 + gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
1.816 + } else {
1.817 + gst_pad_stop_task (sinkpad);
1.818 + }
1.819 + gst_object_unref (nuv);
1.820 +
1.821 + return TRUE;
1.822 +}
1.823 +
1.824 +static GstFlowReturn
1.825 +gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
1.826 +{
1.827 + GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1.828 +
1.829 + gst_adapter_push (nuv->adapter, buf);
1.830 +
1.831 + return gst_nuv_demux_play (pad);
1.832 +}
1.833 +
1.834 +static void
1.835 +gst_nuv_demux_loop (GstPad * pad)
1.836 +{
1.837 + gst_nuv_demux_play (pad);
1.838 +}
1.839 +
1.840 +static void
1.841 +gst_nuv_demux_reset (GstNuvDemux * nuv)
1.842 +{
1.843 + nuv->state = GST_NUV_DEMUX_START;
1.844 + nuv->mode = 0;
1.845 + nuv->offset = 0;
1.846 + nuv->video_offset = 0;
1.847 + nuv->audio_offset = 0;
1.848 +
1.849 + if (nuv->adapter != NULL)
1.850 + gst_adapter_clear (nuv->adapter);
1.851 +
1.852 + if (nuv->mpeg_buffer != NULL) {
1.853 + gst_buffer_unref (nuv->mpeg_buffer);
1.854 + nuv->mpeg_buffer = NULL;
1.855 + }
1.856 +
1.857 + g_free (nuv->h);
1.858 + nuv->h = NULL;
1.859 +
1.860 + g_free (nuv->eh);
1.861 + nuv->eh = NULL;
1.862 +
1.863 + g_free (nuv->fh);
1.864 + nuv->fh = NULL;
1.865 +}
1.866 +
1.867 +static void
1.868 +gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
1.869 +{
1.870 + if (nuv->src_video_pad) {
1.871 + gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
1.872 + nuv->src_video_pad = NULL;
1.873 + }
1.874 +
1.875 + if (nuv->src_audio_pad) {
1.876 + gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
1.877 + nuv->src_audio_pad = NULL;
1.878 + }
1.879 +}
1.880 +
1.881 +static GstStateChangeReturn
1.882 +gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
1.883 +{
1.884 + GstStateChangeReturn ret;
1.885 +
1.886 + switch (transition) {
1.887 + case GST_STATE_CHANGE_READY_TO_PAUSED:
1.888 + break;
1.889 + default:
1.890 + break;
1.891 + }
1.892 +
1.893 + ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1.894 + if (ret == GST_STATE_CHANGE_FAILURE)
1.895 + goto done;
1.896 +
1.897 + switch (transition) {
1.898 + case GST_STATE_CHANGE_PAUSED_TO_READY:
1.899 + gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1.900 + gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1.901 + break;
1.902 + default:
1.903 + break;
1.904 + }
1.905 +
1.906 +done:
1.907 + return ret;
1.908 +}
1.909 +
1.910 +static gboolean
1.911 +plugin_init (GstPlugin * plugin)
1.912 +{
1.913 +#ifdef ENABLE_NLS
1.914 + setlocale (LC_ALL, "");
1.915 + bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1.916 +#endif /* ENABLE_NLS */
1.917 +
1.918 + if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
1.919 + GST_TYPE_NUV_DEMUX)) {
1.920 + return FALSE;
1.921 + }
1.922 + return TRUE;
1.923 +}
1.924 +
1.925 +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1.926 + GST_VERSION_MINOR,
1.927 + "nuvdemux",
1.928 + "Demuxes and muxes audio and video",
1.929 + plugin_init, VERSION, "LGPL", "NuvDemux", "")