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
64 #define GST_FLOW_ERROR_EOS -102
72 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
74 static const GstElementDetails gst_nuv_demux_details =
75 GST_ELEMENT_DETAILS ("Nuv demuxer",
77 "Demultiplex a .nuv file into audio and video",
78 "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
79 "Rosfran Borges <rosfran.borges@indt.org.br>");
85 gchar id[12]; /* "NuppelVideo\0" or "MythTVVideo\0" */
86 gchar version[5]; /* "x.xx\0" */
91 gint i_height_desired;
93 gchar i_mode; /* P progressive, I interlaced */
95 gdouble d_aspect; /* 1.0 squared pixel */
101 gint i_video_blocks; /* 0 no video, -1 unknown */
105 gint i_keyframe_distance;
112 gchar i_type; /* A: audio, V: video, S: sync; T: test
113 R: Seekpoint (string:RTjjjjjjjj)
114 D: Extra data for codec */
115 gchar i_compression; /* V: 0 uncompressed
120 A: 0 uncompressed (44100 1-bits, 2ch)
126 N null frame loudless
128 S: B audio and vdeo sync point
129 A audio sync info (timecode == effective
131 V next video sync (timecode == next video
133 S audio,video,text correlation */
134 gchar i_keyframe; /* 0 keyframe, else no no key frame */
135 guint8 i_filters; /* 0x01: gauss 5 pixel (8,2,2,2,2)/16
136 0x02: gauss 5 pixel (8,1,1,1,1)/12
137 0x04: cartoon filter */
139 gint32 i_timecode; /* ms */
141 gint i_length; /* V,A,T: length of following data
142 S: length of packet correl */
146 /* FIXME Not sure of this one */
153 gint i_audio_sample_rate;
154 gint i_audio_bits_per_sample;
155 gint i_audio_channels;
156 gint i_audio_compression_ratio;
157 gint i_audio_quality;
158 gint i_rtjpeg_quality;
159 gint i_rtjpeg_luma_filter;
160 gint i_rtjpeg_chroma_filter;
164 gint i_lavc_maxqdiff;
165 gint64 i_seekable_offset;
166 gint64 i_keyframe_adjust_offset;
168 } nuv_extended_header;
179 GST_NUV_DEMUX_HEADER_DATA,
180 GST_NUV_DEMUX_EXTRA_DATA,
181 GST_NUV_DEMUX_MPEG_DATA,
182 GST_NUV_DEMUX_EXTEND_HEADER,
183 GST_NUV_DEMUX_EXTEND_HEADER_DATA,
184 GST_NUV_DEMUX_INDEX_CREATE,
185 GST_NUV_DEMUX_FRAME_HEADER,
187 GST_NUV_DEMUX_INVALID_DATA
190 struct _GstNuvDemuxPrivate {
191 /* used for indicate the mode */
194 /* used on push mode */
199 GstPad *src_video_pad;
200 GstPad *src_audio_pad;
203 GstFlowReturn last_video_return;
204 GstFlowReturn last_audio_return;
210 /* NUV decoding state */
211 GstNuvDemuxState state;
214 /* duration information */
215 guint64 duration_bytes;
216 guint64 duration_time;
217 guint64 segment_stop;
218 guint64 segment_start;
220 /* segment control info */
221 gboolean new_audio_segment;
222 gboolean new_video_segment;
225 guint64 mpeg_data_size;
226 GstBuffer *mpeg_buffer;
230 nuv_extended_header eh;
234 guint64 header_lengh;
238 guint64 last_frame_time;
243 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
246 GST_STATIC_CAPS ("video/x-nuv"));
248 static GstStaticPadTemplate audio_src_template =
249 GST_STATIC_PAD_TEMPLATE ("audio_src",
252 GST_STATIC_CAPS_ANY);
254 static GstStaticPadTemplate video_src_template =
255 GST_STATIC_PAD_TEMPLATE ("video_src",
258 GST_STATIC_CAPS_ANY);
260 static void gst_nuv_demux_dispose (GObject * object);
261 static void gst_nuv_demux_finalize (GObject * object);
262 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
263 GstStateChange transition);
264 static void gst_nuv_demux_loop (GstPad * pad);
265 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
266 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
267 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
269 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
271 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
272 static gboolean gst_nuv_demux_sink_event (GstPad *pad, GstEvent *event);
273 static gboolean gst_nuv_demux_srcpad_event (GstPad * pad, GstEvent * event);
274 static frame_index_data * gst_nuv_demux_do_seek_index (GstNuvDemux *nuv, gint64 seek_pos,
275 gint64 segment_stop, GstFormat format);
278 static GstFlowReturn gst_nuv_demux_move_bytes (GstNuvDemux * nuv, guint64 size);
279 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
280 gboolean move, GstBuffer ** buffer);
281 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
282 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
283 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
284 static void gst_nuv_demux_create_seek_index (GstNuvDemux * nuv);
287 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6)
288 GstBuffer * gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes);
292 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
294 /******************************************************************************
296 ******************************************************************************/
297 #if G_BYTE_ORDER == G_BIG_ENDIAN
298 static inline gdouble
299 _gdouble_swap_le_be (gdouble * d)
308 u.i = GUINT64_SWAP_LE_BE (u.i);
312 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
313 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
314 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
315 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
318 double2fraction (double in, int *num, int *denom)
323 } else if (in == 23.976) {
328 while (in - floor(in) >= 0.1) {
332 *num = (int)floor(in);
336 /* GObject Functions */
339 gst_nuv_demux_base_init (gpointer klass)
341 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
343 gst_element_class_add_pad_template (element_class,
344 gst_static_pad_template_get (&audio_src_template));
346 gst_element_class_add_pad_template (element_class,
347 gst_static_pad_template_get (&video_src_template));
349 gst_element_class_add_pad_template (element_class,
350 gst_static_pad_template_get (&sink_template));
351 gst_element_class_set_details (element_class, &gst_nuv_demux_details);
355 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
357 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
358 GObjectClass *gobject_class = (GObjectClass *) klass;
360 GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
361 0, "Demuxer for NUV streams");
363 parent_class = g_type_class_peek_parent (klass);
365 gobject_class->dispose = gst_nuv_demux_dispose;
366 gobject_class->finalize = gst_nuv_demux_finalize;
367 gstelement_class->change_state = gst_nuv_demux_change_state;
369 g_type_class_add_private (gobject_class, sizeof (GstNuvDemuxPrivate));
373 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
375 nuv->priv = GST_NUV_DEMUX_GET_PRIVATE (nuv);
376 nuv->priv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
378 /* creating adapter */
379 nuv->priv->mode = NUV_PUSH_MODE;
380 nuv->priv->adapter = gst_adapter_new ();
382 nuv->priv->new_audio_segment = TRUE;
383 nuv->priv->new_video_segment = TRUE;
385 gst_pad_set_activate_function (nuv->priv->sinkpad, gst_nuv_demux_sink_activate);
386 gst_pad_set_activatepull_function (nuv->priv->sinkpad,
387 gst_nuv_demux_sink_activate_pull);
388 gst_pad_set_activatepush_function (nuv->priv->sinkpad,
389 gst_nuv_demux_sink_activate_push);
390 gst_pad_set_chain_function (nuv->priv->sinkpad,
391 GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
392 gst_pad_set_event_function (nuv->priv->sinkpad,
393 GST_DEBUG_FUNCPTR (gst_nuv_demux_sink_event));
396 gst_element_add_pad (GST_ELEMENT (nuv), nuv->priv->sinkpad);
401 gst_nuv_demux_dispose (GObject * object)
403 GstNuvDemux *nuv = GST_NUV_DEMUX (object);
406 if (nuv->priv->mpeg_buffer != NULL) {
407 gst_buffer_unref (nuv->priv->mpeg_buffer);
410 gst_nuv_demux_reset (GST_NUV_DEMUX (object));
411 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (object));
413 if (nuv->priv->adapter != NULL) {
414 gst_object_unref (nuv->priv->adapter);
419 gst_nuv_demux_finalize (GObject * object)
421 G_OBJECT_CLASS (parent_class)->finalize (object);
428 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header *h)
430 GstBuffer *buffer = NULL;
431 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
433 if ((res != GST_FLOW_OK) || (buffer == NULL)) {
438 memcpy (h->id, buffer->data, 12);
439 memcpy (h->version, buffer->data + 12, 5);
440 h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
441 h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
442 h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
443 h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
444 h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
445 h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
446 h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
447 /* get the num and denom values from fps */
448 double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
449 h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
450 h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
451 h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
452 h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
454 GST_DEBUG_OBJECT (nuv,
455 "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
456 h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
457 h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
458 h->i_keyframe_distance);
462 if (buffer != NULL) {
463 gst_buffer_unref (buffer);
470 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
474 if (nuv->priv->new_file)
475 res = gst_nuv_demux_header_load (nuv, NULL);
477 res = gst_nuv_demux_header_load (nuv, &nuv->priv->h);
479 if (res == GST_FLOW_OK)
480 nuv->priv->state = GST_NUV_DEMUX_EXTRA_DATA;
488 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
490 GstFlowReturn res = GST_FLOW_OK;
491 GstBuffer *file_header = NULL;
493 res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
494 if (res == GST_FLOW_OK) {
495 if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
496 strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
497 nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
499 GST_DEBUG_OBJECT (nuv, "error parsing file header");
500 nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
501 res = GST_FLOW_ERROR;
505 if (file_header != NULL) {
506 gst_buffer_unref (file_header);
515 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header *h)
518 GstBuffer *buf = NULL;
520 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
522 if ((res != GST_FLOW_OK) || (buf == NULL)) {
531 h->i_type = GPOINTER_TO_INT (data[0]);
532 h->i_compression = GPOINTER_TO_INT (data[1]);
533 h->i_keyframe = GPOINTER_TO_INT (data[2]);
534 h->i_filters = GPOINTER_TO_INT (data[3]);
535 h->i_timecode = GST_READ_UINT32_LE (&data[4]);
536 h->i_length = GST_READ_UINT32_LE (&data[8]);
538 GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
540 h->i_compression ? h->i_compression : ' ',
541 h->i_keyframe ? h->i_keyframe : ' ',
542 h->i_filters, h->i_timecode, h->i_length);
546 gst_buffer_unref (buf);
554 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
555 nuv_extended_header * h)
558 GstBuffer *buff = NULL;
560 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
562 if ((res != GST_FLOW_OK) || (buff == NULL)) {
570 h->i_version = GST_READ_UINT32_LE (&data[0]);
571 h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
572 h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
573 h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
574 h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
575 h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
576 h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
577 h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
578 h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
579 h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
580 h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
581 h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
582 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
583 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
584 h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
585 h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
586 h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
588 GST_DEBUG_OBJECT (nuv,
589 "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
590 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
591 h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
592 h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
593 h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
594 h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
595 h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
596 h->i_keyframe_adjust_offset);
600 gst_buffer_unref (buff);
607 /* Query Functions */
608 static const GstQueryType *
609 gst_nuv_demux_get_src_query_types (GstPad * pad)
611 static const GstQueryType src_types[] = {
621 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
623 gboolean res = FALSE;
624 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
627 switch (GST_QUERY_TYPE (query))
629 case GST_QUERY_POSITION:
632 gst_query_parse_position (query, &format, NULL);
635 case GST_FORMAT_TIME:
636 if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
637 gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
646 case GST_QUERY_DURATION:
649 gst_query_parse_duration (query, &format, NULL);
652 case GST_FORMAT_TIME:
653 if (nuv->priv->duration_time != GST_CLOCK_TIME_NONE) {
654 gst_query_set_duration (query, GST_FORMAT_TIME, nuv->priv->duration_time);
668 res = gst_pad_query_default (pad, query);
671 gst_object_unref (nuv);
676 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
679 pad = gst_pad_new_from_static_template (template, name);
680 gst_pad_set_caps (pad, caps);
681 gst_pad_set_active (pad, TRUE);
682 gst_pad_use_fixed_caps (pad);
683 gst_element_add_pad (GST_ELEMENT (nuv), pad);
685 gst_pad_set_event_function (pad,
686 GST_DEBUG_FUNCPTR (gst_nuv_demux_srcpad_event));
688 gst_pad_set_query_type_function (pad,
689 GST_DEBUG_FUNCPTR (gst_nuv_demux_get_src_query_types));
691 gst_pad_set_query_function (pad,
692 GST_DEBUG_FUNCPTR (gst_nuv_demux_handle_src_query));
699 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
701 if (nuv->priv->h.i_video_blocks != 0) {
702 GstCaps *video_caps = NULL;
704 video_caps = gst_caps_new_simple ("video/x-divx",
705 "divxversion", G_TYPE_INT, 4,
706 "width", G_TYPE_INT, nuv->priv->h.i_width,
707 "height", G_TYPE_INT, nuv->priv->h.i_height,
708 "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
709 "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
710 "pixel-aspect-ratio", GST_TYPE_FRACTION,
711 (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
713 nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
714 gst_caps_unref (video_caps);
717 if (nuv->priv->h.i_audio_blocks != 0) {
718 GstCaps *audio_caps = NULL;
720 audio_caps = gst_caps_new_simple ("audio/mpeg",
721 "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
722 "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
723 "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
724 "layer", G_TYPE_INT, 3, // fixme: magic number
725 "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
727 nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
728 gst_caps_unref (audio_caps);
731 gst_element_no_more_pads (GST_ELEMENT (nuv));
735 gst_nuv_demux_validate_header (nuv_frame_header *h)
737 gboolean valid = FALSE;
738 //g_usleep (1 * G_USEC_PER_SEC );
742 if (h->i_compression == 0 ||
743 h->i_compression == 1 ||
744 h->i_compression == 2 ||
745 h->i_compression == 'N' ||
746 h->i_compression == 'L') {
751 if (h->i_compression == 0 ||
752 h->i_compression == 1 ||
753 h->i_compression == 2 ||
754 h->i_compression == 3 ||
755 h->i_compression == 'F' ||
756 h->i_compression == 'S' ||
757 h->i_compression == 'N' ||
758 h->i_compression == 'L') {
763 if (h->i_compression == 'B' ||
764 h->i_compression == 'A' ||
765 h->i_compression == 'V' ||
766 h->i_compression == 'S') {
787 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
789 GstFlowReturn ret = GST_FLOW_OK;
790 gboolean valid = FALSE;
793 ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
794 if (ret != GST_FLOW_OK) {
798 if (gst_nuv_demux_validate_header (&nuv->priv->fh) == TRUE)
801 } while (valid == FALSE);
803 nuv->priv->state = GST_NUV_DEMUX_MOVI;
808 gst_nuv_combine_flow (GstNuvDemux *nuv)
810 GstFlowReturn ret_video = nuv->priv->last_video_return;
811 GstFlowReturn ret_audio = nuv->priv->last_audio_return;
813 if ((ret_video != GST_FLOW_OK) &&
814 (ret_audio != GST_FLOW_OK))
817 if (GST_FLOW_IS_FATAL (ret_video))
820 if (GST_FLOW_IS_FATAL (ret_audio))
827 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
829 GstFlowReturn ret = GST_FLOW_OK;
832 GstBuffer *buf = NULL;
837 if (h.i_type == 'R') {
841 if (h.i_length > 0) {
842 ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
843 if ((ret != GST_FLOW_OK) || (buf == NULL)) {
847 if ((h.i_timecode < 0)) {
852 timestamp = h.i_timecode * GST_MSECOND;
853 GST_BUFFER_TIMESTAMP (buf) = timestamp;
863 pad = nuv->priv->src_video_pad;
865 if (nuv->priv->new_video_segment) {
867 /* send new segment event*/
868 gst_pad_push_event (nuv->priv->src_video_pad,
869 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
870 GST_CLOCK_TIME_NONE, 0));
872 if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
873 nuv->priv->time_start = timestamp;
875 nuv->priv->new_video_segment = FALSE;
882 pad = nuv->priv->src_audio_pad;
884 if (nuv->priv->new_audio_segment) {
885 /* send new segment event*/
886 gst_pad_push_event (nuv->priv->src_audio_pad,
887 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
888 GST_CLOCK_TIME_NONE, 0));
890 if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
891 nuv->priv->time_start = timestamp;
893 nuv->priv->new_audio_segment = FALSE;
900 switch (h.i_compression) {
902 GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
903 gst_pad_push_event (nuv->priv->src_video_pad,
904 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
905 GST_CLOCK_TIME_NONE, 0));
908 GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
909 gst_pad_push_event (nuv->priv->src_audio_pad,
910 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
911 GST_CLOCK_TIME_NONE, 0));
922 if ((buf != NULL) && (pad != NULL)) {
923 /* pushing the buffer */
924 gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
925 ret = gst_pad_push (pad, buf);
928 if (ret != GST_FLOW_OK) {
929 GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
931 if (pad == nuv->priv->src_video_pad) {
932 nuv->priv->last_video_return = ret;
934 else if (pad == nuv->priv->src_audio_pad) {
935 nuv->priv->last_audio_return = ret;
938 /* verify anothers flow if is necessary stop task */
939 if (gst_nuv_combine_flow (nuv) != FALSE) {
942 GST_WARNING_OBJECT (nuv, "error: on push");
950 gst_buffer_unref (buf);
953 if (ret == GST_FLOW_OK) {
954 nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
955 memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
961 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
963 GstFlowReturn ret = GST_FLOW_OK;
965 /* ffmpeg extra data */
966 if (nuv->priv->new_file) {
968 ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE, &buf);
969 gst_buffer_unref (buf);
971 ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
972 &nuv->priv->mpeg_buffer);
975 if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
979 GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
980 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
985 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
987 GstFlowReturn ret = GST_FLOW_OK;
992 if (nuv->priv->new_file)
993 ret = gst_nuv_demux_frame_header_load (nuv, NULL);
995 ret = gst_nuv_demux_frame_header_load (nuv, &h);
997 if (ret != GST_FLOW_OK)
1000 if (h.i_type != 'D') {
1001 GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
1002 return GST_FLOW_NOT_SUPPORTED;
1005 if (h.i_length > 0) {
1006 if (h.i_compression == 'F') {
1007 nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
1009 GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
1010 return GST_FLOW_NOT_SUPPORTED;
1013 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
1019 static GstFlowReturn
1020 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
1022 GstFlowReturn ret = GST_FLOW_OK;
1024 if (nuv->priv->new_file)
1025 ret = gst_nuv_demux_extended_header_load (nuv, NULL);
1027 ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
1028 if (ret != GST_FLOW_OK)
1030 gst_nuv_demux_create_pads (nuv);
1033 nuv->priv->state = GST_NUV_DEMUX_INDEX_CREATE;
1037 static GstFlowReturn
1038 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
1040 GstBuffer *buf = NULL;
1041 GstFlowReturn res = GST_FLOW_OK;
1043 res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
1044 if ((res != GST_FLOW_OK) || (buf == NULL)) {
1046 gst_buffer_unref (buf);
1051 if (buf->data[0] == 'X') {
1052 gst_buffer_unref (buf);
1056 res = gst_nuv_demux_frame_header_load (nuv, &h);
1057 if (res != GST_FLOW_OK)
1060 if (h.i_length != 512) {
1061 return GST_FLOW_ERROR;
1063 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
1065 nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
1066 g_object_unref (buf);
1067 GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
1068 (_("incomplete NUV support")), ("incomplete NUV support"));
1069 return GST_FLOW_ERROR;
1075 gst_nuv_demux_create_seek_index (GstNuvDemux * nuv)
1080 while (gst_nuv_demux_frame_header_load (nuv, &h) == GST_FLOW_OK) {
1081 if ((h.i_type == 'V') && (h.i_keyframe == 0)) {
1082 frame_index_data *f = g_new0 (frame_index_data, 1);
1084 f->offset = nuv->priv->offset - 12;
1085 f->timecode = h.i_timecode * GST_MSECOND;
1087 nuv->priv->index = g_slist_append (nuv->priv->index, f);
1089 if (h.i_type != 'R') {
1090 nuv->priv->offset += h.i_length;
1091 if (h.i_type == 'A' || h.i_type == 'V')
1092 nuv->priv->duration_time = h.i_timecode * GST_MSECOND;
1095 GST_DEBUG_OBJECT (nuv, "CREATING INDEX: DONE : DURATION Bytes/Sec: %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1096 nuv->priv->offset, nuv->priv->duration_time);
1098 nuv->priv->duration_bytes = nuv->priv->offset;
1099 nuv->priv->offset = nuv->priv->header_lengh;
1101 msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_time);
1102 gst_element_post_message (GST_ELEMENT (nuv), msg);
1105 static GstFlowReturn
1106 gst_nuv_demux_play (GstPad * pad)
1108 GstFlowReturn res = GST_FLOW_OK;
1109 GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
1111 switch (nuv->priv->state) {
1112 case GST_NUV_DEMUX_START:
1113 res = gst_nuv_demux_stream_file_header (nuv);
1114 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1119 case GST_NUV_DEMUX_HEADER_DATA:
1120 res = gst_nuv_demux_stream_header_data (nuv);
1121 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1126 case GST_NUV_DEMUX_EXTRA_DATA:
1127 res = gst_nuv_demux_stream_extra_data (nuv);
1128 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1133 case GST_NUV_DEMUX_MPEG_DATA:
1134 res = gst_nuv_demux_stream_mpeg_data (nuv);
1135 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1140 case GST_NUV_DEMUX_EXTEND_HEADER:
1141 res = gst_nuv_demux_stream_extend_header (nuv);
1142 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1147 case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
1148 res = gst_nuv_demux_stream_extend_header_data (nuv);
1149 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1152 //store file header size
1153 nuv->priv->header_lengh = nuv->priv->offset;
1156 case GST_NUV_DEMUX_INDEX_CREATE:
1157 if ((nuv->priv->mode == NUV_PULL_MODE) && (!nuv->priv->new_file)) {
1158 gst_nuv_demux_create_seek_index (nuv);
1161 case GST_NUV_DEMUX_FRAME_HEADER:
1162 res = gst_nuv_demux_read_head_frame (nuv);
1163 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1168 case GST_NUV_DEMUX_MOVI:
1169 res = gst_nuv_demux_stream_data (nuv);
1170 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1175 case GST_NUV_DEMUX_INVALID_DATA:
1179 g_assert_not_reached ();
1185 GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
1186 gst_pad_pause_task (nuv->priv->sinkpad);
1188 if (res == GST_FLOW_ERROR_EOS) {
1189 gst_nuv_demux_send_eos (nuv);
1190 nuv->priv->eos = TRUE;
1194 if (GST_FLOW_IS_FATAL (res)) {
1195 GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
1196 (_("Internal data stream error.")),
1197 ("streaming stopped, reason %s", gst_flow_get_name (res)));
1199 gst_nuv_demux_send_eos (nuv);
1205 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
1207 gst_element_post_message (GST_ELEMENT (nuv),
1208 gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
1210 if (nuv->priv->src_video_pad)
1211 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
1212 if (nuv->priv->src_audio_pad)
1213 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
1216 static GstFlowReturn
1217 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
1218 GstBuffer ** buffer)
1220 GstFlowReturn ret = GST_FLOW_OK;
1226 if (nuv->priv->mode == NUV_PULL_MODE) {
1227 ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
1228 if (ret == GST_FLOW_OK) {
1230 nuv->priv->offset += size;
1233 } else if (ret == GST_FLOW_UNEXPECTED) {
1234 return GST_FLOW_ERROR_EOS;
1237 if (gst_adapter_available (nuv->priv->adapter) < size) {
1238 nuv->priv->more_data = TRUE;
1239 return GST_FLOW_ERROR_NO_DATA;
1242 *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
1244 guint8 *data = NULL;
1245 data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
1246 *buffer = gst_buffer_new ();
1247 gst_buffer_set_data (*buffer, data, size);
1253 static GstFlowReturn
1254 gst_nuv_demux_move_bytes (GstNuvDemux * nuv, guint64 size)
1256 GstFlowReturn ret = GST_FLOW_OK;
1262 if (nuv->priv->mode == NUV_PULL_MODE) {
1263 nuv->priv->offset += size;
1265 if (gst_adapter_available (nuv->priv->adapter) < size) {
1266 nuv->priv->more_data = TRUE;
1267 return GST_FLOW_ERROR_NO_DATA;
1269 gst_adapter_flush (nuv->priv->adapter, size);
1275 gst_nuv_demux_sink_activate (GstPad * sinkpad)
1277 gboolean res = TRUE;
1278 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1280 if (gst_pad_check_pull_range (sinkpad)) {
1281 gst_adapter_clear (nuv->priv->adapter);
1282 res = gst_pad_activate_pull (sinkpad, TRUE);
1284 gst_adapter_clear (nuv->priv->adapter);
1285 res = gst_pad_activate_push (sinkpad, TRUE);
1288 g_object_unref (nuv);
1293 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
1295 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1298 GST_DEBUG_OBJECT (nuv, "activating pull function");
1299 nuv->priv->mode = NUV_PULL_MODE;
1300 gst_adapter_clear (nuv->priv->adapter);
1302 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
1304 GST_DEBUG_OBJECT (nuv, "deactivating pull function");
1305 gst_pad_stop_task (sinkpad);
1307 gst_object_unref (nuv);
1313 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
1315 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1318 nuv->priv->mode = NUV_PUSH_MODE;
1319 gst_adapter_clear (nuv->priv->adapter);
1321 GST_DEBUG_OBJECT (nuv, "activating push/chain function");
1323 GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
1326 gst_object_unref (nuv);
1331 static frame_index_data *
1332 gst_nuv_demux_do_seek_index (GstNuvDemux *nuv, gint64 seek_pos,
1333 gint64 segment_stop, GstFormat format)
1336 frame_index_data *ret = NULL;
1338 if (nuv->priv->index == NULL) {
1342 /* find keyframe closest to the requested position */
1343 for (l = nuv->priv->index; l != NULL; l = l->next) {
1344 frame_index_data *f = (frame_index_data *) l->data;
1347 if (format == GST_FORMAT_BYTES) {
1349 } else if (format == GST_FORMAT_TIME) {
1355 if (pos >= seek_pos) {
1360 if ((segment_stop != -1) && (segment_stop != GST_CLOCK_TIME_NONE) && (pos > segment_stop)) {
1369 gst_nuv_demux_do_seek (GstNuvDemux *nuv, GstEvent * event)
1374 GstSeekType cur_type;
1376 GstSeekType stop_type;
1379 frame_index_data *entry;
1380 gint64 segment_start;
1381 gint64 segment_stop;
1382 GstEvent *newsegment_event;
1384 if (nuv->priv->eos) {
1388 if (nuv->priv->mode == NUV_PUSH_MODE) {
1393 gst_event_parse_seek (event, &rate, &format, &flags,
1394 &cur_type, &cur, &stop_type, &stop);
1399 if (format == GST_FORMAT_TIME) {
1400 GST_DEBUG_OBJECT (nuv, "Can only seek on BYTES");
1406 GST_DEBUG_OBJECT (nuv, "Can only seek with positive rate");
1410 if (cur_type == GST_SEEK_TYPE_SET) {
1411 GST_OBJECT_LOCK (nuv);
1412 if (gst_nuv_demux_do_seek_index (nuv, cur, -1, format) == NULL) {
1413 GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
1414 GST_OBJECT_UNLOCK (nuv);
1417 GST_OBJECT_UNLOCK (nuv);
1420 flush = !!(flags & GST_SEEK_FLAG_FLUSH);
1423 gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_start ());
1424 if (nuv->priv->src_video_pad != NULL) {
1425 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_start ());
1428 if (nuv->priv->src_audio_pad != NULL) {
1429 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_start ());
1433 gst_pad_pause_task (nuv->priv->sinkpad);
1436 GST_PAD_STREAM_LOCK (nuv->priv->sinkpad);
1437 GST_OBJECT_LOCK (nuv);
1440 if (cur == GST_CLOCK_TIME_NONE)
1442 if (stop == GST_CLOCK_TIME_NONE)
1443 stop = nuv->priv->duration_time;
1445 if (cur_type == GST_SEEK_TYPE_SET)
1446 segment_start = cur;
1447 else if (cur_type == GST_SEEK_TYPE_CUR)
1448 segment_start = nuv->priv->segment_start + cur;
1450 segment_start = nuv->priv->segment_start;
1452 if (stop_type == GST_SEEK_TYPE_SET)
1453 segment_stop = stop;
1454 else if (stop_type == GST_SEEK_TYPE_CUR)
1455 segment_stop = nuv->priv->segment_stop + stop;
1457 segment_stop = nuv->priv->segment_stop;
1459 segment_start = CLAMP (segment_start, 0, nuv->priv->duration_time);
1460 segment_stop = CLAMP (segment_stop, 0, nuv->priv->duration_time);
1462 entry = gst_nuv_demux_do_seek_index (nuv, segment_start,
1463 segment_stop, format);
1465 if (entry == NULL) {
1466 GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
1470 segment_start = entry->timecode;
1472 nuv->priv->segment_start = segment_start;
1473 nuv->priv->segment_stop = segment_stop;
1475 GST_OBJECT_UNLOCK (nuv);
1477 if (!nuv->priv->eos) {
1479 msg = gst_message_new_segment_start (GST_OBJECT (nuv), GST_FORMAT_TIME,
1480 nuv->priv->segment_start);
1482 gst_element_post_message (GST_ELEMENT (nuv), msg);
1485 GST_DEBUG_OBJECT (nuv, "NEW SEGMENT START %" G_GUINT64_FORMAT ", STOP %" G_GUINT64_FORMAT,
1486 segment_start, segment_stop);
1487 newsegment_event = gst_event_new_new_segment (FALSE, rate,
1488 GST_FORMAT_TIME, segment_start, segment_stop, segment_start);
1492 if (nuv->priv->src_video_pad != NULL) {
1493 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_stop ());
1496 if (nuv->priv->src_audio_pad != NULL) {
1497 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_stop ());
1500 gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_stop ());
1504 if (nuv->priv->src_video_pad != NULL) {
1505 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
1507 if (nuv->priv->src_audio_pad != NULL) {
1508 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
1511 gst_event_unref (newsegment_event);
1513 nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
1514 nuv->priv->offset = entry->offset;
1516 gst_pad_start_task (nuv->priv->sinkpad, (GstTaskFunction) gst_nuv_demux_loop,
1517 nuv->priv->sinkpad);
1519 GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
1523 GST_DEBUG_OBJECT (nuv, "Got a seek error");
1524 GST_OBJECT_UNLOCK (nuv);
1525 GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
1531 gst_nuv_demux_srcpad_event (GstPad * pad, GstEvent * event)
1533 gboolean res = FALSE;
1536 nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1538 switch (GST_EVENT_TYPE (event)) {
1539 case GST_EVENT_SEEK:
1540 res = gst_nuv_demux_do_seek (nuv, event);
1547 gst_object_unref (nuv);
1552 gst_nuv_demux_sink_event (GstPad * pad, GstEvent * event)
1554 gboolean res = FALSE;
1557 nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1559 switch (GST_EVENT_TYPE (event)) {
1560 case GST_EVENT_NEWSEGMENT:
1569 gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &position);
1570 if ((format == GST_FORMAT_BYTES) && (start == 0)) {
1571 g_debug ("NEW SEGMENT 0");
1572 if (nuv->priv->segment > 0) {
1573 nuv->priv->new_file = TRUE;
1574 nuv->priv->state = GST_NUV_DEMUX_START;
1575 nuv->priv->offset = 0;
1577 nuv->priv->segment++;
1580 newsegment_event = gst_event_new_new_segment (FALSE, rate,
1581 GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1583 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
1584 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
1585 gst_event_unref (newsegment_event);
1587 res = gst_pad_event_default(pad, event);
1591 case GST_EVENT_CUSTOM_DOWNSTREAM:
1594 nuv->priv->new_file = TRUE;
1595 nuv->priv->state = GST_NUV_DEMUX_START;
1596 nuv->priv->offset = 0;
1598 GST_PAD_STREAM_LOCK (pad);
1599 gst_nuv_demux_reset( nuv );
1600 GST_PAD_STREAM_UNLOCK (pad);
1602 res = gst_pad_event_default (pad, event);
1606 res = gst_pad_event_default (pad, event);
1613 static GstFlowReturn
1614 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
1616 GstFlowReturn ret = GST_FLOW_OK;
1617 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1619 if (nuv->priv->mode != NUV_PUSH_MODE)
1622 gst_adapter_push (nuv->priv->adapter, buf);
1624 while ((ret == GST_FLOW_OK) && (nuv->priv->more_data == FALSE)) {
1625 ret = gst_nuv_demux_play (pad);
1628 nuv->priv->more_data = FALSE;
1629 gst_object_unref (nuv);
1635 gst_nuv_demux_loop (GstPad * pad)
1637 gst_nuv_demux_play (pad);
1641 gst_nuv_demux_index_free (gpointer data, gpointer user_data)
1647 gst_nuv_demux_reset (GstNuvDemux * nuv)
1649 nuv->priv->eos = FALSE;
1650 nuv->priv->more_data = FALSE;
1651 nuv->priv->state = GST_NUV_DEMUX_START;
1652 nuv->priv->mode = NUV_PUSH_MODE;
1653 nuv->priv->offset = 0;
1654 nuv->priv->time_start = 0;
1655 nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
1656 nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
1657 nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
1658 nuv->priv->last_video_return = GST_FLOW_OK;
1659 nuv->priv->last_audio_return = GST_FLOW_OK;
1660 nuv->priv->header_lengh = 0;
1661 nuv->priv->segment_stop = GST_CLOCK_TIME_NONE;
1662 nuv->priv->segment_start = GST_CLOCK_TIME_NONE;
1663 nuv->priv->new_file = FALSE;
1666 g_slist_foreach (nuv->priv->index, gst_nuv_demux_index_free, NULL);
1667 g_slist_free (nuv->priv->index);
1668 nuv->priv->index = NULL;
1670 gst_adapter_clear (nuv->priv->adapter);
1672 if (nuv->priv->mpeg_buffer != NULL) {
1673 gst_buffer_unref (nuv->priv->mpeg_buffer);
1674 nuv->priv->mpeg_buffer = NULL;
1679 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
1681 if (nuv->priv->src_video_pad) {
1682 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
1683 nuv->priv->src_video_pad = NULL;
1686 if (nuv->priv->src_audio_pad) {
1687 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
1688 nuv->priv->src_audio_pad = NULL;
1692 static GstStateChangeReturn
1693 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
1695 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1697 g_debug ("Nuvdemux state_change");
1698 switch (transition) {
1699 case GST_STATE_CHANGE_NULL_TO_READY:
1700 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1701 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1707 g_debug ("Nuvdemux state_change: 1");
1709 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1710 if (ret == GST_STATE_CHANGE_FAILURE) {
1715 g_debug ("Nuvdemux state_change: 2");
1717 switch (transition) {
1718 case GST_STATE_CHANGE_READY_TO_NULL:
1719 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1720 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1727 g_debug ("Nuvdemux state_change: DONE");
1733 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6)
1735 gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
1741 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1742 g_return_val_if_fail (nbytes > 0, NULL);
1744 GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);
1746 /* we don't have enough data, return NULL. This is unlikely
1747 * as one usually does an _available() first instead of peeking a
1749 if (G_UNLIKELY (nbytes > adapter->size))
1752 /* our head buffer has enough data left, return it */
1753 cur = adapter->buflist->data;
1754 if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) {
1755 GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
1757 buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes);
1759 gst_adapter_flush (adapter, nbytes);
1764 data = gst_adapter_take (adapter, nbytes);
1768 buffer = gst_buffer_new ();
1769 GST_BUFFER_DATA (buffer) = data;
1770 GST_BUFFER_MALLOCDATA (buffer) = data;
1771 GST_BUFFER_SIZE (buffer) = nbytes;
1778 gst_nuv_typefind (GstTypeFind * tf, gpointer unused)
1780 guint8 *data = gst_type_find_peek (tf, 0, 11);
1783 if (memcmp (data, "MythTVVideo", 11) == 0
1784 || memcmp (data, "NuppelVideo", 11) == 0) {
1785 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
1786 gst_caps_new_simple ("video/x-nuv", NULL));
1792 plugin_init (GstPlugin * plugin)
1794 static gchar *exts[] = { "nuv", NULL };
1796 setlocale (LC_ALL, "");
1797 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1798 #endif /* ENABLE_NLS */
1800 if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
1801 GST_TYPE_NUV_DEMUX)) {
1805 if (!gst_type_find_register (plugin, "video/x-nuv", GST_RANK_SECONDARY,
1808 gst_caps_new_simple ("video/x-nuv", NULL), NULL, NULL)) {
1809 GST_WARNING ("can't register typefind");
1816 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1819 "Demuxes and muxes audio and video",
1820 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)