2 * Copyright (C) <2006> Renato Araujo Oliveira Filho <renato.filho@indt.org.br>
3 * Rosfran Borges <rosfran.borges@indt.org.br>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 /* Element-Checklist-Version: 5 */
23 * SECTION:element-nuvdemux
27 * Demuxes an .nuv file into raw or compressed audio and/or video streams.
30 * This element currently only supports pull-based scheduling.
32 * <title>Example launch line</title>
35 * gst-launch filesrc test.nuv ! nuvdemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
37 * Play (parse and decode) an .nuv file and try to output it to
38 * an automatically detected soundcard and videosink. If the NUV file contains
39 * compressed audio or video data, this will only work if you have the
40 * right decoder elements/plugins installed.
51 #include <gst/gsterror.h>
52 #include <gst/gstplugin.h>
56 #include "glib/gi18n.h"
57 #include "gstnuvdemux.h"
59 #define GST_NUV_DEMUX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_NUV_DEMUX, GstNuvDemuxPrivate))
61 GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug);
62 #define GST_CAT_DEFAULT nuvdemux_debug
63 #define GST_FLOW_ERROR_NO_DATA -101
71 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
73 static const GstElementDetails gst_nuv_demux_details =
74 GST_ELEMENT_DETAILS ("Nuv demuxer",
76 "Demultiplex a .nuv file into audio and video",
77 "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
78 "Rosfran Borges <rosfran.borges@indt.org.br>");
84 gchar id[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */
85 gchar version[5]; /* "x.xx\0" */
90 gint i_height_desired;
92 gchar i_mode; /* P progressive, I interlaced */
94 gdouble d_aspect; /* 1.0 squared pixel */
100 gint i_video_blocks; /* 0 no video, -1 unknown */
104 gint i_keyframe_distance;
111 gchar i_type; /* A: audio, V: video, S: sync; T: test
112 R: Seekpoint (string:RTjjjjjjjj)
113 D: Extra data for codec */
114 gchar i_compression; /* V: 0 uncompressed
119 A: 0 uncompressed (44100 1-bits, 2ch)
125 N null frame loudless
127 S: B audio and vdeo sync point
128 A audio sync info (timecode == effective
130 V next video sync (timecode == next video
132 S audio,video,text correlation */
133 gchar i_keyframe; /* 0 keyframe, else no no key frame */
134 guint8 i_filters; /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
135 0x02: gauss 5 pixel (8,1,1,1,1)/12
136 0x04: cartoon filter */
138 gint32 i_timecode; /* ms */
140 gint i_length; /* V,A,T: length of following data
141 S: length of packet correl */
144 /* FIXME Not sure of this one */
151 gint i_audio_sample_rate;
152 gint i_audio_bits_per_sample;
153 gint i_audio_channels;
154 gint i_audio_compression_ratio;
155 gint i_audio_quality;
156 gint i_rtjpeg_quality;
157 gint i_rtjpeg_luma_filter;
158 gint i_rtjpeg_chroma_filter;
162 gint i_lavc_maxqdiff;
163 gint64 i_seekable_offset;
164 gint64 i_keyframe_adjust_offset;
166 } nuv_extended_header;
170 GST_NUV_DEMUX_HEADER_DATA,
171 GST_NUV_DEMUX_EXTRA_DATA,
172 GST_NUV_DEMUX_MPEG_DATA,
173 GST_NUV_DEMUX_EXTEND_HEADER,
174 GST_NUV_DEMUX_EXTEND_HEADER_DATA,
175 GST_NUV_DEMUX_FRAME_HEADER,
177 GST_NUV_DEMUX_INVALID_DATA
180 struct _GstNuvDemuxPrivate {
181 /* used for indicate the mode */
184 /* used on push mode */
189 GstPad *src_video_pad;
190 GstPad *src_audio_pad;
193 GstFlowReturn last_video_return;
194 GstFlowReturn last_audio_return;
196 /* NUV decoding state */
197 GstNuvDemuxState state;
201 guint64 streamer_offset;
203 /* duration information */
204 gint64 duration_bytes;
205 gint64 duration_time;
206 gint64 duration_average;
208 /* segment control info */
209 gboolean new_audio_segment;
210 gboolean new_video_segment;
213 guint64 mpeg_data_size;
214 GstBuffer *mpeg_buffer;
218 nuv_extended_header *eh;
219 nuv_frame_header *fh;
225 guint64 last_frame_time;
229 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
232 GST_STATIC_CAPS ("video/x-nuv"));
234 static GstStaticPadTemplate audio_src_template =
235 GST_STATIC_PAD_TEMPLATE ("audio_src",
238 GST_STATIC_CAPS_ANY);
240 static GstStaticPadTemplate video_src_template =
241 GST_STATIC_PAD_TEMPLATE ("video_src",
244 GST_STATIC_CAPS_ANY);
246 static void gst_nuv_demux_dispose (GObject * object);
247 static void gst_nuv_demux_finalize (GObject * object);
248 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
249 GstStateChange transition);
250 static void gst_nuv_demux_loop (GstPad * pad);
251 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
252 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
253 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
255 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
257 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
258 static gboolean gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event);
259 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
260 gboolean move, GstBuffer ** buffer);
261 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
262 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
263 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
264 static void gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp);
265 static gint64 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv);
266 static gint64 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv);
269 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
271 /******************************************************************************
273 ******************************************************************************/
274 #if G_BYTE_ORDER == G_BIG_ENDIAN
275 static inline gdouble
276 _gdouble_swap_le_be (gdouble * d)
285 u.i = GUINT64_SWAP_LE_BE (u.i);
289 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
290 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
291 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
292 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
295 double2fraction (double in, int *num, int *denom)
300 } else if (in == 23.976) {
305 while (in - floor(in) >= 0.1) {
309 *num = (int)floor(in);
313 /* GObject Functions */
316 gst_nuv_demux_base_init (gpointer klass)
318 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
320 gst_element_class_add_pad_template (element_class,
321 gst_static_pad_template_get (&audio_src_template));
323 gst_element_class_add_pad_template (element_class,
324 gst_static_pad_template_get (&video_src_template));
326 gst_element_class_add_pad_template (element_class,
327 gst_static_pad_template_get (&sink_template));
328 gst_element_class_set_details (element_class, &gst_nuv_demux_details);
332 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
334 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
335 GObjectClass *gobject_class = (GObjectClass *) klass;
337 GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
338 0, "Demuxer for NUV streams");
340 parent_class = g_type_class_peek_parent (klass);
342 gobject_class->dispose = gst_nuv_demux_dispose;
343 gobject_class->finalize = gst_nuv_demux_finalize;
344 gstelement_class->change_state = gst_nuv_demux_change_state;
346 g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate));
350 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
352 nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv);
353 nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
354 gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate);
355 gst_pad_set_activatepull_function (nuv->priv->sinkpad,
356 gst_nuv_demux_sink_activate_pull);
357 gst_pad_set_activatepush_function (nuv->priv->sinkpad,
358 gst_nuv_demux_sink_activate_push);
359 gst_pad_set_chain_function (nuv->priv->sinkpad,
360 GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
361 gst_pad_set_event_function (nuv->priv->sinkpad,
362 gst_nuv_demux_sink_event);
363 gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad);
365 nuv->priv->new_audio_segment = TRUE;
366 nuv->priv->new_video_segment = TRUE;
368 /* creating adapter */
369 nuv->priv->mode = NUV_PUSH_MODE;
370 nuv->priv->adapter = gst_adapter_new ();
372 gst_nuv_demux_reset (nuv);
376 gst_nuv_demux_dispose (GObject * object)
378 GstNuvDemux *nuv = GST_NUV_DEMUX (object);
380 if (nuv->priv->mpeg_buffer != NULL) {
381 gst_buffer_unref (nuv->priv->mpeg_buffer);
384 gst_nuv_demux_reset (GST_NUV_DEMUX (object));
385 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (object));
387 if (nuv->priv->adapter != NULL) {
388 gst_object_unref (nuv->priv->adapter);
393 gst_nuv_demux_finalize (GObject * object)
395 G_OBJECT_CLASS (parent_class)->finalize (object);
402 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header ** h_ret)
404 GstBuffer *buffer = NULL;
405 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
407 if ((res != GST_FLOW_OK) || (buffer == NULL))
410 nuv_header *h = g_new0 (nuv_header, 1);
412 memcpy (h->id, buffer->data, 12);
413 memcpy (h->version, buffer->data + 12, 5);
414 h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
415 h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
416 h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
417 h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
418 h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
419 h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
420 h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
421 /* get the num and denom values from fps */
422 double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
423 h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
424 h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
425 h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
426 h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
428 GST_DEBUG_OBJECT (nuv,
429 "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
430 h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
431 h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
432 h->i_keyframe_distance);
435 gst_buffer_unref (buffer);
440 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
444 res = gst_nuv_demux_header_load (nuv, &nuv->priv->h);
445 if (res == GST_FLOW_OK)
446 nuv->priv->state = GST_NUV_DEMUX_EXTRA_DATA;
454 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
456 GstFlowReturn res = GST_FLOW_OK;
457 GstBuffer *file_header = NULL;
459 res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
460 if ((res != GST_FLOW_OK) || (file_header == NULL)) {
463 if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
464 strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
465 nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
467 GST_DEBUG_OBJECT (nuv, "error parsing file header");
468 nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
469 res = GST_FLOW_ERROR;
472 if (file_header != NULL) {
473 gst_buffer_unref (file_header);
481 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header ** h_ret)
485 GstBuffer *buf = NULL;
487 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
489 if ((res != GST_FLOW_OK) || (buf == NULL)) {
491 gst_buffer_unref (buf);
496 h = g_new0 (nuv_frame_header, 1);
499 h->i_type = GPOINTER_TO_INT (data[0]);
500 h->i_compression = GPOINTER_TO_INT (data[1]);
501 h->i_keyframe = GPOINTER_TO_INT (data[2]);
502 h->i_filters = GPOINTER_TO_INT (data[3]);
503 h->i_timecode = GST_READ_UINT32_LE (&data[4]);
504 h->i_length = GST_READ_UINT32_LE (&data[8]);
506 GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
508 h->i_compression ? h->i_compression : ' ',
509 h->i_keyframe ? h->i_keyframe : ' ',
510 h->i_filters, h->i_timecode, h->i_length);
513 gst_buffer_unref (buf);
518 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
519 nuv_extended_header ** h_ret)
522 GstBuffer *buff = NULL;
523 nuv_extended_header *h;
526 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
528 if ((res != GST_FLOW_OK) || (buff == NULL)) {
530 gst_buffer_unref (buff);
535 h = g_new0 (nuv_extended_header, 1);
537 h->i_version = GST_READ_UINT32_LE (&data[0]);
538 h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
539 h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
540 h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
541 h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
542 h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
543 h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
544 h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
545 h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
546 h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
547 h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
548 h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
549 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
550 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
551 h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
552 h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
553 h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
555 GST_DEBUG_OBJECT (nuv,
556 "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
557 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
558 h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
559 h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
560 h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
561 h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
562 h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
563 h->i_keyframe_adjust_offset);
566 gst_buffer_unref (buff);
571 /* Query Functions */
572 static const GstQueryType *
573 gst_nuv_demux_get_src_query_types (GstPad * pad)
575 static const GstQueryType src_types[] = {
585 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
587 gboolean res = FALSE;
588 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
590 switch (GST_QUERY_TYPE (query)) {
591 case GST_QUERY_POSITION:
592 if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
594 gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
598 case GST_QUERY_DURATION:
601 duration = gst_nuv_demux_get_time_duration (nuv);
602 if (duration == GST_CLOCK_TIME_NONE) {
603 duration = nuv->priv->duration_average;
605 if (duration != GST_CLOCK_TIME_NONE) {
606 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
616 gst_object_unref (nuv);
622 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
625 pad = gst_pad_new_from_static_template (template, name);
626 gst_pad_set_caps (pad, caps);
627 gst_pad_set_active (pad, TRUE);
628 gst_pad_use_fixed_caps (pad);
629 gst_element_add_pad (GST_ELEMENT (nuv), pad);
635 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
637 if (nuv->priv->h->i_video_blocks != 0) {
638 GstCaps *video_caps = NULL;
640 video_caps = gst_caps_new_simple ("video/x-divx",
641 "divxversion", G_TYPE_INT, 4,
642 "width", G_TYPE_INT, nuv->priv->h->i_width,
643 "height", G_TYPE_INT, nuv->priv->h->i_height,
644 "framerate", GST_TYPE_FRACTION, nuv->priv->h->i_fpsn, nuv->priv->h->i_fpsd,
645 "format", GST_TYPE_FOURCC, nuv->priv->eh->i_video_fcc,
646 "pixel-aspect-ratio", GST_TYPE_FRACTION,
647 (gint) (nuv->priv->h->d_aspect * 1000.0f), 1000, NULL);
649 nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
650 gst_caps_unref (video_caps);
653 if (nuv->priv->h->i_audio_blocks != 0) {
654 GstCaps *audio_caps = NULL;
656 audio_caps = gst_caps_new_simple ("audio/mpeg",
657 "rate", G_TYPE_INT, nuv->priv->eh->i_audio_sample_rate,
658 "format", GST_TYPE_FOURCC, nuv->priv->eh->i_audio_fcc,
659 "channels", G_TYPE_INT, nuv->priv->eh->i_audio_channels,
660 "mpegversion", G_TYPE_INT, nuv->priv->eh->i_version, NULL);
662 nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
663 gst_caps_unref (audio_caps);
666 gst_element_no_more_pads (GST_ELEMENT (nuv));
670 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
672 GstFlowReturn ret = GST_FLOW_OK;
674 if (nuv->priv->fh != NULL)
676 g_free (nuv->priv->fh);
677 nuv->priv->fh = NULL;
680 ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
681 if (ret != GST_FLOW_OK)
684 nuv->priv->state = GST_NUV_DEMUX_MOVI;
689 gst_nuv_combine_flow (GstNuvDemux *nuv)
691 GstFlowReturn ret_video = nuv->priv->last_video_return;
692 GstFlowReturn ret_audio = nuv->priv->last_audio_return;
694 if ((ret_video != GST_FLOW_OK) &&
695 (ret_audio != GST_FLOW_OK))
698 if (GST_FLOW_IS_FATAL (ret_video))
701 if (GST_FLOW_IS_FATAL (ret_audio))
708 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
710 GstFlowReturn ret = GST_FLOW_OK;
713 GstBuffer *buf = NULL;
714 nuv_frame_header *h = NULL;
718 if (h->i_type == 'R')
721 if (h->i_length > 0) {
722 ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
723 if ((ret != GST_FLOW_OK) || (buf == NULL))
726 if (h->i_timecode < 0) {
727 h->i_timecode = h->i_timecode * -1;
728 nuv->priv->time_diff = h->i_timecode;
731 h->i_timecode += nuv->priv->time_diff;
733 timestamp = h->i_timecode * GST_MSECOND;
735 GST_BUFFER_SIZE (buf) = h->i_length;
736 GST_BUFFER_TIMESTAMP (buf) = timestamp;
746 pad = nuv->priv->src_video_pad;
748 if (nuv->priv->new_video_segment) {
750 /* send new segment event*/
751 gst_pad_push_event (nuv->priv->src_video_pad,
752 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
753 GST_CLOCK_TIME_NONE, 0));
755 if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
756 nuv->priv->time_start = timestamp;
758 nuv->priv->new_video_segment = FALSE;
765 pad = nuv->priv->src_audio_pad;
767 if (nuv->priv->new_audio_segment) {
768 /* send new segment event*/
769 gst_pad_push_event (nuv->priv->src_audio_pad,
770 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
771 GST_CLOCK_TIME_NONE, 0));
773 if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
774 nuv->priv->time_start = timestamp;
776 nuv->priv->new_audio_segment = FALSE;
783 switch (h->i_compression) {
785 GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h->i_timecode);
786 gst_pad_push_event (nuv->priv->src_video_pad,
787 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h->i_timecode * GST_MSECOND,
788 GST_CLOCK_TIME_NONE, 0));
791 GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h->i_timecode);
792 gst_pad_push_event (nuv->priv->src_audio_pad,
793 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
794 GST_CLOCK_TIME_NONE, 0));
800 gst_buffer_unref (buf);
806 gst_buffer_unref (buf);
811 if ((buf != NULL) && (pad != NULL)) {
812 /* update average time */
813 nuv->priv->streamer_offset += h->i_length;
814 gst_segment_set_last_stop (&nuv->priv->segment, GST_FORMAT_TIME, timestamp);
815 gst_nuv_demux_update_duration (nuv, timestamp);
817 /* pushing the buffer */
818 gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
819 ret = gst_pad_push (pad, buf);
821 if (ret != GST_FLOW_OK) {
822 GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
824 if (pad == nuv->priv->src_video_pad) {
825 nuv->priv->last_video_return = ret;
827 else if (pad == nuv->priv->src_audio_pad) {
828 nuv->priv->last_audio_return = ret;
831 /* verify anothers flow if is necessary stop task */
832 if (gst_nuv_combine_flow (nuv) != FALSE) {
836 //GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
841 nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
842 g_free (nuv->priv->fh);
843 nuv->priv->fh = NULL;
848 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
850 GstFlowReturn ret = GST_FLOW_OK;
852 /* ffmpeg extra data */
853 ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
854 &nuv->priv->mpeg_buffer);
855 if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
859 GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
860 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
865 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
867 GstFlowReturn ret = GST_FLOW_OK;
872 ret = gst_nuv_demux_frame_header_load (nuv, &h);
873 if (ret != GST_FLOW_OK)
876 if (h->i_type != 'D') {
878 GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
879 return GST_FLOW_NOT_SUPPORTED;
882 if (h->i_length > 0) {
883 if (h->i_compression == 'F') {
884 nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
887 GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
888 return GST_FLOW_NOT_SUPPORTED;
891 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
900 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
902 GstFlowReturn ret = GST_FLOW_OK;
904 ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
905 if (ret != GST_FLOW_OK)
908 gst_nuv_demux_create_pads (nuv);
909 nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
914 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
916 GstBuffer *buf = NULL;
917 GstFlowReturn res = GST_FLOW_OK;
919 res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
920 if ((res != GST_FLOW_OK) || (buf == NULL)) {
922 gst_buffer_unref (buf);
927 if (buf->data[0] == 'X') {
928 gst_buffer_unref (buf);
930 nuv_frame_header *h = NULL;
932 res = gst_nuv_demux_frame_header_load (nuv, &h);
933 if (res != GST_FLOW_OK)
936 if (h->i_length != 512) {
938 return GST_FLOW_ERROR;
942 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
944 nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
945 g_object_unref (buf);
946 GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
947 (_("incomplete NUV support")), ("incomplete NUV support"));
948 return GST_FLOW_ERROR;
954 gst_nuv_demux_play (GstPad * pad)
956 GstFlowReturn res = GST_FLOW_OK;
957 GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
959 switch (nuv->priv->state) {
960 case GST_NUV_DEMUX_START:
961 res = gst_nuv_demux_stream_file_header (nuv);
962 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
967 case GST_NUV_DEMUX_HEADER_DATA:
968 res = gst_nuv_demux_stream_header_data (nuv);
969 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
974 case GST_NUV_DEMUX_EXTRA_DATA:
975 res = gst_nuv_demux_stream_extra_data (nuv);
976 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
981 case GST_NUV_DEMUX_MPEG_DATA:
982 res = gst_nuv_demux_stream_mpeg_data (nuv);
983 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
988 case GST_NUV_DEMUX_EXTEND_HEADER:
989 res = gst_nuv_demux_stream_extend_header (nuv);
990 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
995 case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
996 res = gst_nuv_demux_stream_extend_header_data (nuv);
997 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1002 case GST_NUV_DEMUX_FRAME_HEADER:
1003 res = gst_nuv_demux_read_head_frame (nuv);
1004 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1009 case GST_NUV_DEMUX_MOVI:
1010 res = gst_nuv_demux_stream_data (nuv);
1011 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1016 case GST_NUV_DEMUX_INVALID_DATA:
1020 g_assert_not_reached ();
1023 GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->priv->state, gst_flow_get_name (res));
1028 GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
1029 gst_pad_pause_task (nuv->priv->sinkpad);
1030 if (GST_FLOW_IS_FATAL (res)) {
1031 GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
1032 (_("Internal data stream error.")),
1033 ("streaming stopped, reason %s", gst_flow_get_name (res)));
1035 gst_nuv_demux_send_eos (nuv);
1041 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
1043 gst_element_post_message (GST_ELEMENT (nuv),
1044 gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
1046 if (nuv->priv->src_video_pad)
1047 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
1048 if (nuv->priv->src_audio_pad)
1049 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
1052 static GstFlowReturn
1053 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
1054 GstBuffer ** buffer)
1056 GstFlowReturn ret = GST_FLOW_OK;
1062 if (nuv->priv->mode == NUV_PULL_MODE) {
1063 ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
1064 if (ret == GST_FLOW_OK) {
1066 nuv->priv->offset += size;
1069 } else if (ret == GST_FLOW_UNEXPECTED) {
1071 gst_buffer_unref (buffer);
1073 gst_nuv_demux_send_eos (nuv);
1074 return GST_FLOW_WRONG_STATE;
1077 if (gst_adapter_available (nuv->priv->adapter) < size)
1078 return GST_FLOW_ERROR_NO_DATA;
1081 guint8 *data = NULL;
1082 data = (guint8 *) gst_adapter_take (nuv->priv->adapter, size);
1083 *buffer = gst_buffer_new ();
1084 gst_buffer_set_data (*buffer, data, size);
1086 guint8 *data = NULL;
1087 data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
1088 *buffer = gst_buffer_new ();
1089 gst_buffer_set_data (*buffer, data, size);
1096 gst_nuv_demux_sink_activate (GstPad * sinkpad)
1098 gboolean res = TRUE;
1099 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1101 if (gst_pad_check_pull_range (sinkpad)) {
1102 res = gst_pad_activate_pull (sinkpad, TRUE);
1104 res = gst_pad_activate_push (sinkpad, TRUE);
1106 g_object_unref (nuv);
1111 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
1113 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1116 GST_DEBUG_OBJECT (nuv, "activating pull function");
1117 nuv->priv->mode = NUV_PULL_MODE;
1118 if (nuv->priv->adapter) {
1119 gst_adapter_clear (nuv->priv->adapter);
1120 g_object_unref (nuv->priv->adapter);
1121 nuv->priv->adapter = NULL;
1123 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
1125 GST_DEBUG_OBJECT (nuv, "deactivating pull function");
1126 gst_pad_stop_task (sinkpad);
1128 gst_object_unref (nuv);
1134 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
1136 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1140 GST_DEBUG_OBJECT (nuv, "activating push/chain function");
1142 GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
1145 gst_object_unref (nuv);
1152 gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event)
1157 nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1159 switch (GST_EVENT_TYPE (event)) {
1160 case GST_EVENT_SEEK:
1165 GstSeekType cur_type;
1167 GstSeekType stop_type;
1170 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur, &stop_type, &stop);
1172 GST_DEBUG_OBJECT (nuv, "got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop);
1176 case GST_EVENT_NEWSEGMENT:
1178 gint64 start, stop, time;
1179 gdouble rate, arate;
1183 GST_DEBUG_OBJECT (nuv, "got a new segment event");
1184 gst_event_parse_new_segment_full (event, &update, &rate, &format,
1185 &start, &stop, &time);
1187 GST_DEBUG_OBJECT (nuv, "got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop);
1188 nuv->priv->duration_bytes = stop - start;
1189 gst_event_unref (event);
1197 GstClockTimeDiff diff;
1198 GstClockTime timestamp;
1200 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1202 nuv->time_qos = timecode + diff;
1212 res = gst_pad_event_default (pad, event);
1216 gst_object_unref (nuv);
1220 static GstFlowReturn
1221 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
1223 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1225 GST_DEBUG_OBJECT (nuv, " gst_nuv_demux_chain function");
1226 gst_adapter_push (nuv->priv->adapter, buf);
1228 gst_object_unref (nuv);
1230 return gst_nuv_demux_play (pad);
1234 gst_nuv_demux_loop (GstPad * pad)
1236 gst_nuv_demux_play (pad);
1240 gst_nuv_demux_reset (GstNuvDemux * nuv)
1242 nuv->priv->state = GST_NUV_DEMUX_START;
1243 nuv->priv->mode = 0;
1244 nuv->priv->offset = 0;
1245 nuv->priv->streamer_offset = 0;
1246 nuv->priv->time_start = 0;
1247 nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
1248 nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
1249 nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
1250 nuv->priv->duration_average = GST_CLOCK_TIME_NONE;
1251 gst_segment_init (&nuv->priv->segment, GST_FORMAT_TIME);
1253 if (nuv->priv->adapter != NULL)
1254 gst_adapter_clear (nuv->priv->adapter);
1256 if (nuv->priv->mpeg_buffer != NULL) {
1257 gst_buffer_unref (nuv->priv->mpeg_buffer);
1258 nuv->priv->mpeg_buffer = NULL;
1261 g_free (nuv->priv->h);
1262 nuv->priv->h = NULL;
1264 g_free (nuv->priv->eh);
1265 nuv->priv->eh = NULL;
1267 g_free (nuv->priv->fh);
1268 nuv->priv->fh = NULL;
1272 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
1274 if (nuv->priv->src_video_pad) {
1275 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
1276 nuv->priv->src_video_pad = NULL;
1279 if (nuv->priv->src_audio_pad) {
1280 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
1281 nuv->priv->src_audio_pad = NULL;
1285 static GstStateChangeReturn
1286 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
1288 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1290 switch (transition) {
1291 case GST_STATE_CHANGE_NULL_TO_READY:
1292 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1293 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1299 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1300 if (ret == GST_STATE_CHANGE_FAILURE) {
1304 switch (transition) {
1305 case GST_STATE_CHANGE_READY_TO_NULL:
1306 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1307 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1318 gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp)
1320 guint64 interval = 0;
1322 if (gst_nuv_demux_get_time_duration (nuv) != GST_CLOCK_TIME_NONE)
1325 interval = current_timestamp - nuv->priv->last_update;
1327 if (interval > (10 * GST_SECOND)) {
1328 GstMessage* msg = NULL;
1330 gint64 duration_bytes = gst_nuv_demux_get_bytes_duration (nuv);
1332 if (duration_bytes == GST_CLOCK_TIME_NONE)
1335 if (current_timestamp > 0) {
1336 interval = gst_util_uint64_scale (1, current_timestamp - nuv->priv->time_start, GST_SECOND);
1338 average = gst_util_uint64_scale (1 , nuv->priv->streamer_offset, interval);
1341 nuv->priv->duration_average = gst_util_uint64_scale (GST_SECOND, duration_bytes, average);
1343 nuv->priv->last_update = current_timestamp;
1344 msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_average);
1345 gst_element_post_message (GST_ELEMENT (nuv), msg);
1346 GST_DEBUG_OBJECT (nuv, "New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
1352 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv)
1354 if (nuv->priv->duration_bytes == GST_CLOCK_TIME_NONE) {
1355 GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
1356 GstQuery *query = gst_query_new_duration (GST_FORMAT_BYTES);
1357 if (gst_pad_query (peer, query)) {
1360 gst_query_parse_duration (query, NULL, &duration);
1361 nuv->priv->duration_bytes = duration;
1363 gst_object_unref (peer);
1364 gst_query_unref (query);
1366 return nuv->priv->duration_bytes;
1370 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv)
1372 if (nuv->priv->duration_time == GST_CLOCK_TIME_NONE) {
1373 GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
1374 GstQuery *query = gst_query_new_duration (GST_FORMAT_TIME);
1375 if (gst_pad_query (peer, query)) {
1377 gst_query_parse_duration (query, NULL, &duration);
1378 nuv->priv->duration_time = duration;
1380 gst_object_unref (peer);
1381 gst_query_unref (query);
1383 return nuv->priv->duration_time;
1388 plugin_init (GstPlugin * plugin)
1391 setlocale (LC_ALL, "");
1392 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1393 #endif /* ENABLE_NLS */
1395 if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
1396 GST_TYPE_NUV_DEMUX)) {
1402 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1405 "Demuxes and muxes audio and video",
1406 plugin_init, VERSION, "LGPL", "NuvDemux", "")