[svn r293] Some fixes to the program info.
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;
197 /* NUV decoding state */
198 GstNuvDemuxState state;
202 guint64 streamer_offset;
204 /* duration information */
205 gint64 duration_bytes;
206 gint64 duration_time;
207 gint64 duration_average;
209 /* segment control info */
210 gboolean new_audio_segment;
211 gboolean new_video_segment;
214 guint64 mpeg_data_size;
215 GstBuffer *mpeg_buffer;
219 nuv_extended_header eh;
226 guint64 last_frame_time;
230 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
233 GST_STATIC_CAPS ("video/x-nuv"));
235 static GstStaticPadTemplate audio_src_template =
236 GST_STATIC_PAD_TEMPLATE ("audio_src",
239 GST_STATIC_CAPS_ANY);
241 static GstStaticPadTemplate video_src_template =
242 GST_STATIC_PAD_TEMPLATE ("video_src",
245 GST_STATIC_CAPS_ANY);
247 static void gst_nuv_demux_dispose (GObject * object);
248 static void gst_nuv_demux_finalize (GObject * object);
249 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
250 GstStateChange transition);
251 static void gst_nuv_demux_loop (GstPad * pad);
252 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
253 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
254 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
256 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
258 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
259 static gboolean gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event);
260 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
261 gboolean move, GstBuffer ** buffer);
262 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
263 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
264 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
265 static void gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp);
266 static gint64 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv);
267 static gint64 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv);
269 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6)
270 GstBuffer * gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes);
274 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
276 /******************************************************************************
278 ******************************************************************************/
279 #if G_BYTE_ORDER == G_BIG_ENDIAN
280 static inline gdouble
281 _gdouble_swap_le_be (gdouble * d)
290 u.i = GUINT64_SWAP_LE_BE (u.i);
294 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
295 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
296 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
297 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
300 double2fraction (double in, int *num, int *denom)
305 } else if (in == 23.976) {
310 while (in - floor(in) >= 0.1) {
314 *num = (int)floor(in);
318 /* GObject Functions */
321 gst_nuv_demux_base_init (gpointer klass)
323 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
325 gst_element_class_add_pad_template (element_class,
326 gst_static_pad_template_get (&audio_src_template));
328 gst_element_class_add_pad_template (element_class,
329 gst_static_pad_template_get (&video_src_template));
331 gst_element_class_add_pad_template (element_class,
332 gst_static_pad_template_get (&sink_template));
333 gst_element_class_set_details (element_class, &gst_nuv_demux_details);
337 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
339 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
340 GObjectClass *gobject_class = (GObjectClass *) klass;
342 GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
343 0, "Demuxer for NUV streams");
345 parent_class = g_type_class_peek_parent (klass);
347 gobject_class->dispose = gst_nuv_demux_dispose;
348 gobject_class->finalize = gst_nuv_demux_finalize;
349 gstelement_class->change_state = gst_nuv_demux_change_state;
351 g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate));
355 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
357 nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv);
358 nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
359 gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate);
360 gst_pad_set_activatepull_function (nuv->priv->sinkpad,
361 gst_nuv_demux_sink_activate_pull);
362 gst_pad_set_activatepush_function (nuv->priv->sinkpad,
363 gst_nuv_demux_sink_activate_push);
364 gst_pad_set_chain_function (nuv->priv->sinkpad,
365 GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
366 gst_pad_set_event_function (nuv->priv->sinkpad,
367 gst_nuv_demux_sink_event);
368 gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad);
370 nuv->priv->new_audio_segment = TRUE;
371 nuv->priv->new_video_segment = TRUE;
373 /* creating adapter */
374 nuv->priv->mode = NUV_PUSH_MODE;
375 nuv->priv->adapter = gst_adapter_new ();
377 gst_nuv_demux_reset (nuv);
381 gst_nuv_demux_dispose (GObject * object)
383 GstNuvDemux *nuv = GST_NUV_DEMUX (object);
385 if (nuv->priv->mpeg_buffer != NULL) {
386 gst_buffer_unref (nuv->priv->mpeg_buffer);
389 gst_nuv_demux_reset (GST_NUV_DEMUX (object));
390 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (object));
392 if (nuv->priv->adapter != NULL) {
393 gst_object_unref (nuv->priv->adapter);
398 gst_nuv_demux_finalize (GObject * object)
400 G_OBJECT_CLASS (parent_class)->finalize (object);
407 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header *h)
409 GstBuffer *buffer = NULL;
410 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
412 if ((res != GST_FLOW_OK) || (buffer == NULL)) {
416 memcpy (h->id, buffer->data, 12);
417 memcpy (h->version, buffer->data + 12, 5);
418 h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
419 h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
420 h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
421 h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
422 h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
423 h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
424 h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
425 /* get the num and denom values from fps */
426 double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
427 h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
428 h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
429 h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
430 h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
432 GST_DEBUG_OBJECT (nuv,
433 "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
434 h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
435 h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
436 h->i_keyframe_distance);
439 if (buffer != NULL) {
440 gst_buffer_unref (buffer);
447 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
451 res = gst_nuv_demux_header_load (nuv, &nuv->priv->h);
452 if (res == GST_FLOW_OK)
453 nuv->priv->state = GST_NUV_DEMUX_EXTRA_DATA;
461 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
463 GstFlowReturn res = GST_FLOW_OK;
464 GstBuffer *file_header = NULL;
466 res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
467 if (res == GST_FLOW_OK) {
468 if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
469 strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
470 nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
472 GST_DEBUG_OBJECT (nuv, "error parsing file header");
473 nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
474 res = GST_FLOW_ERROR;
478 if (file_header != NULL) {
479 gst_buffer_unref (file_header);
488 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header *h)
491 GstBuffer *buf = NULL;
493 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
495 if ((res != GST_FLOW_OK) || (buf == NULL)) {
501 h->i_type = GPOINTER_TO_INT (data[0]);
502 h->i_compression = GPOINTER_TO_INT (data[1]);
503 h->i_keyframe = GPOINTER_TO_INT (data[2]);
504 h->i_filters = GPOINTER_TO_INT (data[3]);
505 h->i_timecode = GST_READ_UINT32_LE (&data[4]);
506 h->i_length = GST_READ_UINT32_LE (&data[8]);
508 GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
510 h->i_compression ? h->i_compression : ' ',
511 h->i_keyframe ? h->i_keyframe : ' ',
512 h->i_filters, h->i_timecode, h->i_length);
516 gst_buffer_unref (buf);
523 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
524 nuv_extended_header * h)
527 GstBuffer *buff = NULL;
529 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
531 if ((res != GST_FLOW_OK) || (buff == NULL)) {
536 h->i_version = GST_READ_UINT32_LE (&data[0]);
537 h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
538 h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
539 h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
540 h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
541 h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
542 h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
543 h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
544 h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
545 h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
546 h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
547 h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
548 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
549 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
550 h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
551 h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
552 h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
554 GST_DEBUG_OBJECT (nuv,
555 "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
556 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
557 h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
558 h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
559 h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
560 h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
561 h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
562 h->i_keyframe_adjust_offset);
566 gst_buffer_unref (buff);
573 /* Query Functions */
574 static const GstQueryType *
575 gst_nuv_demux_get_src_query_types (GstPad * pad)
577 static const GstQueryType src_types[] = {
587 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
589 gboolean res = FALSE;
590 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
592 switch (GST_QUERY_TYPE (query)) {
593 case GST_QUERY_POSITION:
594 if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
596 gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
600 case GST_QUERY_DURATION:
603 duration = gst_nuv_demux_get_time_duration (nuv);
604 if (duration == GST_CLOCK_TIME_NONE) {
605 duration = nuv->priv->duration_average;
607 if (duration != GST_CLOCK_TIME_NONE) {
608 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
618 gst_object_unref (nuv);
624 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
627 pad = gst_pad_new_from_static_template (template, name);
628 gst_pad_set_caps (pad, caps);
629 gst_pad_set_active (pad, TRUE);
630 gst_pad_use_fixed_caps (pad);
631 gst_element_add_pad (GST_ELEMENT (nuv), pad);
637 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
639 if (nuv->priv->h.i_video_blocks != 0) {
640 GstCaps *video_caps = NULL;
642 video_caps = gst_caps_new_simple ("video/x-divx",
643 "divxversion", G_TYPE_INT, 4,
644 "width", G_TYPE_INT, nuv->priv->h.i_width,
645 "height", G_TYPE_INT, nuv->priv->h.i_height,
646 "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
647 "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
648 "pixel-aspect-ratio", GST_TYPE_FRACTION,
649 (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
651 nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
652 gst_caps_unref (video_caps);
655 if (nuv->priv->h.i_audio_blocks != 0) {
656 GstCaps *audio_caps = NULL;
658 audio_caps = gst_caps_new_simple ("audio/mpeg",
659 "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
660 "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
661 "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
662 "layer", G_TYPE_INT, 3, // fixme: magic number
663 "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
665 char *tmp = gst_caps_to_string (audio_caps);
667 nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
668 gst_caps_unref (audio_caps);
671 gst_element_no_more_pads (GST_ELEMENT (nuv));
675 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
677 GstFlowReturn ret = GST_FLOW_OK;
679 ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
680 if (ret != GST_FLOW_OK)
683 nuv->priv->state = GST_NUV_DEMUX_MOVI;
688 gst_nuv_combine_flow (GstNuvDemux *nuv)
690 GstFlowReturn ret_video = nuv->priv->last_video_return;
691 GstFlowReturn ret_audio = nuv->priv->last_audio_return;
693 if ((ret_video != GST_FLOW_OK) &&
694 (ret_audio != GST_FLOW_OK))
697 if (GST_FLOW_IS_FATAL (ret_video))
700 if (GST_FLOW_IS_FATAL (ret_audio))
707 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
709 GstFlowReturn ret = GST_FLOW_OK;
712 GstBuffer *buf = NULL;
720 if (h.i_length > 0) {
721 ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
722 if ((ret != GST_FLOW_OK) || (buf == NULL)) {
726 if ((h.i_timecode < 0)) {
730 timestamp = h.i_timecode * GST_MSECOND;
731 GST_BUFFER_TIMESTAMP (buf) = timestamp;
741 pad = nuv->priv->src_video_pad;
743 if (nuv->priv->new_video_segment) {
745 /* send new segment event*/
746 gst_pad_push_event (nuv->priv->src_video_pad,
747 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
748 GST_CLOCK_TIME_NONE, 0));
750 if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
751 nuv->priv->time_start = timestamp;
753 nuv->priv->new_video_segment = FALSE;
760 pad = nuv->priv->src_audio_pad;
762 if (nuv->priv->new_audio_segment) {
763 /* send new segment event*/
764 gst_pad_push_event (nuv->priv->src_audio_pad,
765 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
766 GST_CLOCK_TIME_NONE, 0));
768 if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
769 nuv->priv->time_start = timestamp;
771 nuv->priv->new_audio_segment = FALSE;
778 switch (h.i_compression) {
780 GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
781 gst_pad_push_event (nuv->priv->src_video_pad,
782 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
783 GST_CLOCK_TIME_NONE, 0));
786 GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
787 gst_pad_push_event (nuv->priv->src_audio_pad,
788 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
789 GST_CLOCK_TIME_NONE, 0));
800 if ((buf != NULL) && (pad != NULL)) {
801 /* update average time */
802 nuv->priv->streamer_offset += h.i_length;
803 gst_segment_set_last_stop (&nuv->priv->segment, GST_FORMAT_TIME, timestamp);
804 gst_nuv_demux_update_duration (nuv, timestamp);
806 /* pushing the buffer */
807 gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
808 ret = gst_pad_push (pad, buf);
811 if (ret != GST_FLOW_OK) {
812 GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
814 if (pad == nuv->priv->src_video_pad) {
815 nuv->priv->last_video_return = ret;
817 else if (pad == nuv->priv->src_audio_pad) {
818 nuv->priv->last_audio_return = ret;
821 /* verify anothers flow if is necessary stop task */
822 if (gst_nuv_combine_flow (nuv) != FALSE) {
826 //GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
832 gst_buffer_unref (buf);
835 if (ret == GST_FLOW_OK) {
836 nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
837 memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
843 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
845 GstFlowReturn ret = GST_FLOW_OK;
847 /* ffmpeg extra data */
848 ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
849 &nuv->priv->mpeg_buffer);
850 if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
854 GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
855 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
860 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
862 GstFlowReturn ret = GST_FLOW_OK;
867 ret = gst_nuv_demux_frame_header_load (nuv, &h);
868 if (ret != GST_FLOW_OK)
871 if (h.i_type != 'D') {
872 GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
873 return GST_FLOW_NOT_SUPPORTED;
876 if (h.i_length > 0) {
877 if (h.i_compression == 'F') {
878 nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
880 GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
881 return GST_FLOW_NOT_SUPPORTED;
884 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
891 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
893 GstFlowReturn ret = GST_FLOW_OK;
895 ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
896 if (ret != GST_FLOW_OK)
899 gst_nuv_demux_create_pads (nuv);
900 nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
905 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
907 GstBuffer *buf = NULL;
908 GstFlowReturn res = GST_FLOW_OK;
910 res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
911 if ((res != GST_FLOW_OK) || (buf == NULL)) {
913 gst_buffer_unref (buf);
918 if (buf->data[0] == 'X') {
919 gst_buffer_unref (buf);
923 res = gst_nuv_demux_frame_header_load (nuv, &h);
924 if (res != GST_FLOW_OK)
927 if (h.i_length != 512) {
928 return GST_FLOW_ERROR;
930 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
932 nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
933 g_object_unref (buf);
934 GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
935 (_("incomplete NUV support")), ("incomplete NUV support"));
936 return GST_FLOW_ERROR;
942 gst_nuv_demux_play (GstPad * pad)
944 GstFlowReturn res = GST_FLOW_OK;
945 GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
947 switch (nuv->priv->state) {
948 case GST_NUV_DEMUX_START:
949 res = gst_nuv_demux_stream_file_header (nuv);
950 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
955 case GST_NUV_DEMUX_HEADER_DATA:
956 res = gst_nuv_demux_stream_header_data (nuv);
957 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
962 case GST_NUV_DEMUX_EXTRA_DATA:
963 res = gst_nuv_demux_stream_extra_data (nuv);
964 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
969 case GST_NUV_DEMUX_MPEG_DATA:
970 res = gst_nuv_demux_stream_mpeg_data (nuv);
971 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
976 case GST_NUV_DEMUX_EXTEND_HEADER:
977 res = gst_nuv_demux_stream_extend_header (nuv);
978 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
983 case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
984 res = gst_nuv_demux_stream_extend_header_data (nuv);
985 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
990 case GST_NUV_DEMUX_FRAME_HEADER:
991 res = gst_nuv_demux_read_head_frame (nuv);
992 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
997 case GST_NUV_DEMUX_MOVI:
998 res = gst_nuv_demux_stream_data (nuv);
999 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1004 case GST_NUV_DEMUX_INVALID_DATA:
1008 g_assert_not_reached ();
1014 GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
1015 gst_pad_pause_task (nuv->priv->sinkpad);
1016 if (GST_FLOW_IS_FATAL (res)) {
1017 GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
1018 (_("Internal data stream error.")),
1019 ("streaming stopped, reason %s", gst_flow_get_name (res)));
1021 gst_nuv_demux_send_eos (nuv);
1027 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
1029 gst_element_post_message (GST_ELEMENT (nuv),
1030 gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
1032 if (nuv->priv->src_video_pad)
1033 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
1034 if (nuv->priv->src_audio_pad)
1035 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
1038 static GstFlowReturn
1039 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
1040 GstBuffer ** buffer)
1042 GstFlowReturn ret = GST_FLOW_OK;
1048 if (nuv->priv->mode == NUV_PULL_MODE) {
1049 ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
1050 if (ret == GST_FLOW_OK) {
1052 nuv->priv->offset += size;
1055 } else if (ret == GST_FLOW_UNEXPECTED) {
1057 gst_buffer_unref (buffer);
1059 gst_nuv_demux_send_eos (nuv);
1060 return GST_FLOW_WRONG_STATE;
1063 if (gst_adapter_available (nuv->priv->adapter) < size) {
1064 nuv->priv->more_data = TRUE;
1065 return GST_FLOW_ERROR_NO_DATA;
1068 *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
1070 guint8 *data = NULL;
1071 data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
1072 *buffer = gst_buffer_new ();
1073 gst_buffer_set_data (*buffer, data, size);
1080 gst_nuv_demux_sink_activate (GstPad * sinkpad)
1082 gboolean res = TRUE;
1083 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1085 if (gst_pad_check_pull_range (sinkpad)) {
1086 if (nuv->priv->adapter) {
1087 gst_adapter_clear (nuv->priv->adapter);
1088 g_object_unref (nuv->priv->adapter);
1089 nuv->priv->adapter = NULL;
1091 res = gst_pad_activate_pull (sinkpad, TRUE);
1093 if (nuv->priv->adapter) {
1094 gst_adapter_clear (nuv->priv->adapter);
1096 nuv->priv->adapter = gst_adapter_new ();
1098 res = gst_pad_activate_push (sinkpad, TRUE);
1101 g_object_unref (nuv);
1106 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
1108 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1111 GST_DEBUG_OBJECT (nuv, "activating pull function");
1112 nuv->priv->mode = NUV_PULL_MODE;
1113 if (nuv->priv->adapter) {
1114 gst_adapter_clear (nuv->priv->adapter);
1115 g_object_unref (nuv->priv->adapter);
1116 nuv->priv->adapter = NULL;
1118 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
1120 GST_DEBUG_OBJECT (nuv, "deactivating pull function");
1121 gst_pad_stop_task (sinkpad);
1123 gst_object_unref (nuv);
1129 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
1131 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1135 GST_DEBUG_OBJECT (nuv, "activating push/chain function");
1137 GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
1140 gst_object_unref (nuv);
1147 gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event)
1152 nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1154 switch (GST_EVENT_TYPE (event)) {
1155 case GST_EVENT_SEEK:
1160 GstSeekType cur_type;
1162 GstSeekType stop_type;
1165 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur, &stop_type, &stop);
1166 GST_DEBUG_OBJECT (nuv, "got seek, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, cur, stop);
1169 case GST_EVENT_NEWSEGMENT:
1171 gint64 start, stop, time;
1172 gdouble rate, arate;
1176 GST_DEBUG_OBJECT (nuv, "got a new segment event");
1177 gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &time);
1179 GST_DEBUG_OBJECT (nuv, "got newsegment, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, start, stop);
1180 nuv->priv->duration_bytes = stop - start;
1181 gst_event_unref (event);
1189 GstClockTimeDiff diff;
1190 GstClockTime timestamp;
1192 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1194 nuv->time_qos = timecode + diff;
1203 res = gst_pad_event_default (pad, event);
1207 gst_object_unref (nuv);
1211 static GstFlowReturn
1212 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
1214 GstFlowReturn ret = GST_FLOW_OK;
1216 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1218 gst_adapter_push (nuv->priv->adapter, buf);
1220 while ((ret == GST_FLOW_OK) && (nuv->priv->more_data == FALSE)) {
1221 ret = gst_nuv_demux_play (pad);
1224 nuv->priv->more_data = FALSE;
1225 gst_object_unref (nuv);
1231 gst_nuv_demux_loop (GstPad * pad)
1233 gst_nuv_demux_play (pad);
1237 gst_nuv_demux_reset (GstNuvDemux * nuv)
1239 nuv->priv->more_data = FALSE;
1240 nuv->priv->state = GST_NUV_DEMUX_START;
1241 nuv->priv->mode = 0;
1242 nuv->priv->offset = 0;
1243 nuv->priv->streamer_offset = 0;
1244 nuv->priv->time_start = 0;
1245 nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
1246 nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
1247 nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
1248 nuv->priv->duration_average = GST_CLOCK_TIME_NONE;
1249 gst_segment_init (&nuv->priv->segment, GST_FORMAT_TIME);
1251 if (nuv->priv->adapter != NULL) {
1252 gst_adapter_clear (nuv->priv->adapter);
1255 if (nuv->priv->mpeg_buffer != NULL) {
1256 gst_buffer_unref (nuv->priv->mpeg_buffer);
1257 nuv->priv->mpeg_buffer = NULL;
1262 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
1264 if (nuv->priv->src_video_pad) {
1265 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
1266 nuv->priv->src_video_pad = NULL;
1269 if (nuv->priv->src_audio_pad) {
1270 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
1271 nuv->priv->src_audio_pad = NULL;
1275 static GstStateChangeReturn
1276 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
1278 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1280 switch (transition) {
1281 case GST_STATE_CHANGE_NULL_TO_READY:
1282 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1283 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1289 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1290 if (ret == GST_STATE_CHANGE_FAILURE) {
1294 switch (transition) {
1295 case GST_STATE_CHANGE_READY_TO_NULL:
1296 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1297 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1308 gst_nuv_demux_update_duration (GstNuvDemux *nuv, guint64 current_timestamp)
1310 guint64 interval = 0;
1312 if (gst_nuv_demux_get_time_duration (nuv) != GST_CLOCK_TIME_NONE)
1315 interval = current_timestamp - nuv->priv->last_update;
1317 if (interval > (10 * GST_SECOND)) {
1318 GstMessage* msg = NULL;
1320 gint64 duration_bytes = gst_nuv_demux_get_bytes_duration (nuv);
1322 if (duration_bytes == GST_CLOCK_TIME_NONE) {
1326 interval = gst_util_uint64_scale (1, current_timestamp - nuv->priv->time_start, GST_SECOND);
1328 average = gst_util_uint64_scale (1 , nuv->priv->streamer_offset, interval);
1331 nuv->priv->duration_average = gst_util_uint64_scale (GST_SECOND, duration_bytes, average);
1334 nuv->priv->last_update = current_timestamp;
1335 msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_average);
1336 gst_element_post_message (GST_ELEMENT (nuv), msg);
1337 GST_DEBUG_OBJECT (nuv, "New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
1338 g_debug ("New Duration Average %"G_GUINT64_FORMAT, nuv->priv->duration_average);
1343 gst_nuv_demux_get_bytes_duration (GstNuvDemux *nuv)
1345 if (nuv->priv->duration_bytes == GST_CLOCK_TIME_NONE) {
1346 GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
1347 GstQuery *query = gst_query_new_duration (GST_FORMAT_BYTES);
1348 if (gst_pad_query (peer, query)) {
1351 gst_query_parse_duration (query, NULL, &duration);
1352 nuv->priv->duration_bytes = duration;
1354 gst_object_unref (peer);
1355 gst_query_unref (query);
1357 return nuv->priv->duration_bytes;
1361 gst_nuv_demux_get_time_duration (GstNuvDemux *nuv)
1363 if (nuv->priv->duration_time == GST_CLOCK_TIME_NONE) {
1364 GstPad *peer = gst_pad_get_peer (nuv->priv->sinkpad);
1365 GstQuery *query = gst_query_new_duration (GST_FORMAT_TIME);
1366 if (gst_pad_query (peer, query)) {
1368 gst_query_parse_duration (query, NULL, &duration);
1369 nuv->priv->duration_time = duration;
1371 gst_object_unref (peer);
1372 gst_query_unref (query);
1374 return nuv->priv->duration_time;
1377 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6)
1379 gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
1385 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1386 g_return_val_if_fail (nbytes > 0, NULL);
1388 GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);
1390 /* we don't have enough data, return NULL. This is unlikely
1391 * as one usually does an _available() first instead of peeking a
1393 if (G_UNLIKELY (nbytes > adapter->size))
1396 /* our head buffer has enough data left, return it */
1397 cur = adapter->buflist->data;
1398 if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) {
1399 GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
1401 buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes);
1403 gst_adapter_flush (adapter, nbytes);
1408 data = gst_adapter_take (adapter, nbytes);
1412 buffer = gst_buffer_new ();
1413 GST_BUFFER_DATA (buffer) = data;
1414 GST_BUFFER_MALLOCDATA (buffer) = data;
1415 GST_BUFFER_SIZE (buffer) = nbytes;
1423 plugin_init (GstPlugin * plugin)
1426 setlocale (LC_ALL, "");
1427 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1428 #endif /* ENABLE_NLS */
1430 if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
1431 GST_TYPE_NUV_DEMUX)) {
1437 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1440 "Demuxes and muxes audio and video",
1441 plugin_init, VERSION, "LGPL", "NuvDemux", "")