[svn r721] Added GStreamer-like indentation.
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, FALSE, &buf);
522 if ((res != GST_FLOW_OK) || (buf == NULL)) {
532 /* verifies if it is a MythTV file header */
533 if ( ( data[0] == 'M' || data[0] == 'N') && data[6] == 'V' ) {
534 nuv->priv->state = GST_NUV_DEMUX_HEADER_DATA;
537 /* it is not a NUV file header, so now can move the 12 bytes */
538 res = gst_nuv_demux_move_bytes (nuv, 12);
541 h->i_type = GPOINTER_TO_INT (data[0]);
542 h->i_compression = GPOINTER_TO_INT (data[1]);
543 h->i_keyframe = GPOINTER_TO_INT (data[2]);
544 h->i_filters = GPOINTER_TO_INT (data[3]);
545 h->i_timecode = GST_READ_UINT32_LE (&data[4]);
546 h->i_length = GST_READ_UINT32_LE (&data[8]);
548 GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
550 h->i_compression ? h->i_compression : ' ',
551 h->i_keyframe ? h->i_keyframe : ' ',
552 h->i_filters, h->i_timecode, h->i_length);
556 gst_buffer_unref (buf);
564 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
565 nuv_extended_header * h)
568 GstBuffer *buff = NULL;
570 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
572 if ((res != GST_FLOW_OK) || (buff == NULL)) {
580 h->i_version = GST_READ_UINT32_LE (&data[0]);
581 h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
582 h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
583 h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
584 h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
585 h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
586 h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
587 h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
588 h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
589 h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
590 h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
591 h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
592 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
593 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
594 h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
595 h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
596 h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
598 GST_DEBUG_OBJECT (nuv,
599 "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
600 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
601 h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
602 h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
603 h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
604 h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
605 h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
606 h->i_keyframe_adjust_offset);
610 gst_buffer_unref (buff);
617 /* Query Functions */
618 static const GstQueryType *
619 gst_nuv_demux_get_src_query_types (GstPad * pad)
621 static const GstQueryType src_types[] = {
631 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
633 gboolean res = FALSE;
634 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
637 switch (GST_QUERY_TYPE (query))
639 case GST_QUERY_POSITION:
642 gst_query_parse_position (query, &format, NULL);
645 case GST_FORMAT_TIME:
646 if (GST_CLOCK_TIME_IS_VALID (nuv->priv->last_frame_time)) {
647 gst_query_set_position (query, GST_FORMAT_TIME, nuv->priv->last_frame_time);
656 case GST_QUERY_DURATION:
659 gst_query_parse_duration (query, &format, NULL);
662 case GST_FORMAT_TIME:
663 if (nuv->priv->duration_time != GST_CLOCK_TIME_NONE) {
664 gst_query_set_duration (query, GST_FORMAT_TIME, nuv->priv->duration_time);
678 res = gst_pad_query_default (pad, query);
681 gst_object_unref (nuv);
686 gst_nuv_demux_create_pad (GstNuvDemux *nuv, GstCaps *caps, GstStaticPadTemplate *template, const gchar* name)
689 pad = gst_pad_new_from_static_template (template, name);
690 gst_pad_set_caps (pad, caps);
691 gst_pad_set_active (pad, TRUE);
692 gst_pad_use_fixed_caps (pad);
693 gst_element_add_pad (GST_ELEMENT (nuv), pad);
695 gst_pad_set_event_function (pad,
696 GST_DEBUG_FUNCPTR (gst_nuv_demux_srcpad_event));
698 gst_pad_set_query_type_function (pad,
699 GST_DEBUG_FUNCPTR (gst_nuv_demux_get_src_query_types));
701 gst_pad_set_query_function (pad,
702 GST_DEBUG_FUNCPTR (gst_nuv_demux_handle_src_query));
709 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
711 if (nuv->priv->h.i_video_blocks != 0) {
712 GstCaps *video_caps = NULL;
714 video_caps = gst_caps_new_simple ("video/x-divx",
715 "divxversion", G_TYPE_INT, 4,
716 "width", G_TYPE_INT, nuv->priv->h.i_width,
717 "height", G_TYPE_INT, nuv->priv->h.i_height,
718 "framerate", GST_TYPE_FRACTION, nuv->priv->h.i_fpsn, nuv->priv->h.i_fpsd,
719 "format", GST_TYPE_FOURCC, nuv->priv->eh.i_video_fcc,
720 "pixel-aspect-ratio", GST_TYPE_FRACTION,
721 (gint) (nuv->priv->h.d_aspect * 1000.0f), 1000, NULL);
723 nuv->priv->src_video_pad = gst_nuv_demux_create_pad (nuv, video_caps, &video_src_template, "video_src");
724 gst_caps_unref (video_caps);
727 if (nuv->priv->h.i_audio_blocks != 0) {
728 GstCaps *audio_caps = NULL;
730 audio_caps = gst_caps_new_simple ("audio/mpeg",
731 "rate", G_TYPE_INT, nuv->priv->eh.i_audio_sample_rate,
732 "format", GST_TYPE_FOURCC, nuv->priv->eh.i_audio_fcc,
733 "channels", G_TYPE_INT, nuv->priv->eh.i_audio_channels,
734 "layer", G_TYPE_INT, 3, // fixme: magic number
735 "mpegversion", G_TYPE_INT, nuv->priv->eh.i_version, NULL);
737 nuv->priv->src_audio_pad = gst_nuv_demux_create_pad (nuv, audio_caps, &audio_src_template, "audio_src");
738 gst_caps_unref (audio_caps);
741 gst_element_no_more_pads (GST_ELEMENT (nuv));
745 gst_nuv_demux_validate_header (nuv_frame_header *h)
747 gboolean valid = FALSE;
748 //g_usleep (1 * G_USEC_PER_SEC );
752 if (h->i_compression == 0 ||
753 h->i_compression == 1 ||
754 h->i_compression == 2 ||
755 h->i_compression == 'N' ||
756 h->i_compression == 'L') {
761 if (h->i_compression == 0 ||
762 h->i_compression == 1 ||
763 h->i_compression == 2 ||
764 h->i_compression == 3 ||
765 h->i_compression == 'F' ||
766 h->i_compression == 'S' ||
767 h->i_compression == 'N' ||
768 h->i_compression == 'L') {
773 if (h->i_compression == 'B' ||
774 h->i_compression == 'A' ||
775 h->i_compression == 'V' ||
776 h->i_compression == 'S') {
797 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
799 GstFlowReturn ret = GST_FLOW_OK;
800 gboolean valid = FALSE;
803 ret = gst_nuv_demux_frame_header_load (nuv, &nuv->priv->fh);
804 if (ret != GST_FLOW_OK) {
808 if (gst_nuv_demux_validate_header (&nuv->priv->fh) == TRUE)
811 } while (valid == FALSE);
813 nuv->priv->state = GST_NUV_DEMUX_MOVI;
818 gst_nuv_combine_flow (GstNuvDemux *nuv)
820 GstFlowReturn ret_video = nuv->priv->last_video_return;
821 GstFlowReturn ret_audio = nuv->priv->last_audio_return;
823 if ((ret_video != GST_FLOW_OK) &&
824 (ret_audio != GST_FLOW_OK))
827 if (GST_FLOW_IS_FATAL (ret_video))
830 if (GST_FLOW_IS_FATAL (ret_audio))
837 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
839 GstFlowReturn ret = GST_FLOW_OK;
842 GstBuffer *buf = NULL;
847 if (h.i_type == 'R') {
851 if (h.i_length > 0) {
852 ret = gst_nuv_demux_read_bytes (nuv, h.i_length, TRUE, &buf);
853 if ((ret != GST_FLOW_OK) || (buf == NULL)) {
857 if ((h.i_timecode < 0)) {
862 timestamp = h.i_timecode * GST_MSECOND;
863 GST_BUFFER_TIMESTAMP (buf) = timestamp;
873 pad = nuv->priv->src_video_pad;
875 if (nuv->priv->new_video_segment) {
877 /* send new segment event*/
878 gst_pad_push_event (nuv->priv->src_video_pad,
879 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
880 GST_CLOCK_TIME_NONE, 0));
882 if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
883 nuv->priv->time_start = timestamp;
885 nuv->priv->new_video_segment = FALSE;
892 pad = nuv->priv->src_audio_pad;
894 if (nuv->priv->new_audio_segment) {
895 /* send new segment event*/
896 gst_pad_push_event (nuv->priv->src_audio_pad,
897 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
898 GST_CLOCK_TIME_NONE, 0));
900 if (nuv->priv->time_start == GST_CLOCK_TIME_NONE) {
901 nuv->priv->time_start = timestamp;
903 nuv->priv->new_audio_segment = FALSE;
910 switch (h.i_compression) {
912 GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h.i_timecode);
913 gst_pad_push_event (nuv->priv->src_video_pad,
914 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h.i_timecode * GST_MSECOND,
915 GST_CLOCK_TIME_NONE, 0));
918 GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h.i_timecode);
919 gst_pad_push_event (nuv->priv->src_audio_pad,
920 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
921 GST_CLOCK_TIME_NONE, 0));
932 if ((buf != NULL) && (pad != NULL)) {
933 /* pushing the buffer */
934 gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
935 ret = gst_pad_push (pad, buf);
938 if (ret != GST_FLOW_OK) {
939 GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s", ret, gst_pad_get_name (pad));
941 if (pad == nuv->priv->src_video_pad) {
942 nuv->priv->last_video_return = ret;
944 else if (pad == nuv->priv->src_audio_pad) {
945 nuv->priv->last_audio_return = ret;
948 /* verify anothers flow if is necessary stop task */
949 if (gst_nuv_combine_flow (nuv) != FALSE) {
952 GST_WARNING_OBJECT (nuv, "error: on push");
960 gst_buffer_unref (buf);
963 if (ret == GST_FLOW_OK) {
964 nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
965 memset (&nuv->priv->fh, 0, sizeof (nuv->priv->fh));
971 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
973 GstFlowReturn ret = GST_FLOW_OK;
975 /* ffmpeg extra data */
976 if (nuv->priv->new_file) {
978 ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE, &buf);
979 gst_buffer_unref (buf);
981 ret = gst_nuv_demux_read_bytes (nuv, nuv->priv->mpeg_data_size, TRUE,
982 &nuv->priv->mpeg_buffer);
985 if ((ret != GST_FLOW_OK) || (nuv->priv->mpeg_buffer == NULL)) {
989 GST_BUFFER_SIZE (nuv->priv->mpeg_buffer) = nuv->priv->mpeg_data_size;
990 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
995 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
997 GstFlowReturn ret = GST_FLOW_OK;
1002 if (nuv->priv->new_file)
1003 ret = gst_nuv_demux_frame_header_load (nuv, NULL);
1005 ret = gst_nuv_demux_frame_header_load (nuv, &h);
1007 if (ret != GST_FLOW_OK)
1010 if (h.i_type != 'D') {
1011 GST_WARNING_OBJECT (nuv, "Unsuported rtjpeg");
1012 return GST_FLOW_NOT_SUPPORTED;
1015 if (h.i_length > 0) {
1016 if (h.i_compression == 'F') {
1017 nuv->priv->state = GST_NUV_DEMUX_MPEG_DATA;
1019 GST_WARNING_OBJECT (nuv, "only file with extended chunk are supported");
1020 return GST_FLOW_NOT_SUPPORTED;
1023 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER;
1029 static GstFlowReturn
1030 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
1032 GstFlowReturn ret = GST_FLOW_OK;
1034 if (nuv->priv->new_file)
1035 ret = gst_nuv_demux_extended_header_load (nuv, NULL);
1037 ret = gst_nuv_demux_extended_header_load (nuv, &nuv->priv->eh);
1038 if (ret != GST_FLOW_OK)
1040 gst_nuv_demux_create_pads (nuv);
1043 nuv->priv->state = GST_NUV_DEMUX_INDEX_CREATE;
1047 static GstFlowReturn
1048 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
1050 GstBuffer *buf = NULL;
1051 GstFlowReturn res = GST_FLOW_OK;
1053 res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
1054 if ((res != GST_FLOW_OK) || (buf == NULL)) {
1056 gst_buffer_unref (buf);
1061 if (buf->data[0] == 'X') {
1062 gst_buffer_unref (buf);
1066 res = gst_nuv_demux_frame_header_load (nuv, &h);
1067 if (res != GST_FLOW_OK)
1070 if (h.i_length != 512) {
1071 return GST_FLOW_ERROR;
1073 nuv->priv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
1075 nuv->priv->state = GST_NUV_DEMUX_INVALID_DATA;
1076 g_object_unref (buf);
1077 GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
1078 (_("incomplete NUV support")), ("incomplete NUV support"));
1079 return GST_FLOW_ERROR;
1085 gst_nuv_demux_create_seek_index (GstNuvDemux * nuv)
1090 while (gst_nuv_demux_frame_header_load (nuv, &h) == GST_FLOW_OK) {
1091 if ((h.i_type == 'V') && (h.i_keyframe == 0)) {
1092 frame_index_data *f = g_new0 (frame_index_data, 1);
1094 f->offset = nuv->priv->offset - 12;
1095 f->timecode = h.i_timecode * GST_MSECOND;
1097 nuv->priv->index = g_slist_append (nuv->priv->index, f);
1099 if (h.i_type != 'R') {
1100 nuv->priv->offset += h.i_length;
1101 if (h.i_type == 'A' || h.i_type == 'V')
1102 nuv->priv->duration_time = h.i_timecode * GST_MSECOND;
1105 GST_DEBUG_OBJECT (nuv, "CREATING INDEX: DONE : DURATION Bytes/Sec: %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1106 nuv->priv->offset, nuv->priv->duration_time);
1108 nuv->priv->duration_bytes = nuv->priv->offset;
1109 nuv->priv->offset = nuv->priv->header_lengh;
1111 msg = gst_message_new_duration (GST_OBJECT (nuv), GST_FORMAT_TIME, nuv->priv->duration_time);
1112 gst_element_post_message (GST_ELEMENT (nuv), msg);
1115 static GstFlowReturn
1116 gst_nuv_demux_play (GstPad * pad)
1118 GstFlowReturn res = GST_FLOW_OK;
1119 GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
1121 switch (nuv->priv->state) {
1122 case GST_NUV_DEMUX_START:
1123 res = gst_nuv_demux_stream_file_header (nuv);
1124 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1129 case GST_NUV_DEMUX_HEADER_DATA:
1130 res = gst_nuv_demux_stream_header_data (nuv);
1131 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1136 case GST_NUV_DEMUX_EXTRA_DATA:
1137 res = gst_nuv_demux_stream_extra_data (nuv);
1138 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1143 case GST_NUV_DEMUX_MPEG_DATA:
1144 res = gst_nuv_demux_stream_mpeg_data (nuv);
1145 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1150 case GST_NUV_DEMUX_EXTEND_HEADER:
1151 res = gst_nuv_demux_stream_extend_header (nuv);
1152 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1157 case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
1158 res = gst_nuv_demux_stream_extend_header_data (nuv);
1159 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1162 //store file header size
1163 nuv->priv->header_lengh = nuv->priv->offset;
1166 case GST_NUV_DEMUX_INDEX_CREATE:
1167 if ((nuv->priv->mode == NUV_PULL_MODE) && (!nuv->priv->new_file)) {
1168 gst_nuv_demux_create_seek_index (nuv);
1171 case GST_NUV_DEMUX_FRAME_HEADER:
1172 res = gst_nuv_demux_read_head_frame (nuv);
1173 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1178 case GST_NUV_DEMUX_MOVI:
1179 res = gst_nuv_demux_stream_data (nuv);
1180 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
1185 case GST_NUV_DEMUX_INVALID_DATA:
1189 g_assert_not_reached ();
1195 GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
1196 gst_pad_pause_task (nuv->priv->sinkpad);
1198 if (res == GST_FLOW_ERROR_EOS) {
1199 gst_nuv_demux_send_eos (nuv);
1200 nuv->priv->eos = TRUE;
1204 if (GST_FLOW_IS_FATAL (res)) {
1205 GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
1206 (_("Internal data stream error.")),
1207 ("streaming stopped, reason %s", gst_flow_get_name (res)));
1209 gst_nuv_demux_send_eos (nuv);
1215 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
1217 gst_element_post_message (GST_ELEMENT (nuv),
1218 gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
1220 if (nuv->priv->src_video_pad)
1221 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_eos ());
1222 if (nuv->priv->src_audio_pad)
1223 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_eos ());
1226 static GstFlowReturn
1227 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
1228 GstBuffer ** buffer)
1230 GstFlowReturn ret = GST_FLOW_OK;
1236 if (nuv->priv->mode == NUV_PULL_MODE) {
1237 ret = gst_pad_pull_range (nuv->priv->sinkpad, nuv->priv->offset, size, buffer);
1238 if (ret == GST_FLOW_OK) {
1240 nuv->priv->offset += size;
1243 } else if (ret == GST_FLOW_UNEXPECTED) {
1244 return GST_FLOW_ERROR_EOS;
1247 if (gst_adapter_available (nuv->priv->adapter) < size) {
1248 nuv->priv->more_data = TRUE;
1249 return GST_FLOW_ERROR_NO_DATA;
1252 *buffer = gst_adapter_take_buffer (nuv->priv->adapter, size);
1254 guint8 *data = NULL;
1255 data = (guint8 *) gst_adapter_peek (nuv->priv->adapter, size);
1256 *buffer = gst_buffer_new ();
1257 gst_buffer_set_data (*buffer, data, size);
1263 static GstFlowReturn
1264 gst_nuv_demux_move_bytes (GstNuvDemux * nuv, guint64 size)
1266 GstFlowReturn ret = GST_FLOW_OK;
1272 if (nuv->priv->mode == NUV_PULL_MODE) {
1273 nuv->priv->offset += size;
1275 if (gst_adapter_available (nuv->priv->adapter) < size) {
1276 nuv->priv->more_data = TRUE;
1277 return GST_FLOW_ERROR_NO_DATA;
1279 gst_adapter_flush (nuv->priv->adapter, size);
1285 gst_nuv_demux_sink_activate (GstPad * sinkpad)
1287 gboolean res = TRUE;
1288 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1290 if (gst_pad_check_pull_range (sinkpad)) {
1291 gst_adapter_clear (nuv->priv->adapter);
1292 res = gst_pad_activate_pull (sinkpad, TRUE);
1294 gst_adapter_clear (nuv->priv->adapter);
1295 res = gst_pad_activate_push (sinkpad, TRUE);
1298 g_object_unref (nuv);
1303 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
1305 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1308 GST_DEBUG_OBJECT (nuv, "activating pull function");
1309 nuv->priv->mode = NUV_PULL_MODE;
1310 gst_adapter_clear (nuv->priv->adapter);
1312 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
1314 GST_DEBUG_OBJECT (nuv, "deactivating pull function");
1315 gst_pad_stop_task (sinkpad);
1317 gst_object_unref (nuv);
1323 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
1325 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1328 nuv->priv->mode = NUV_PUSH_MODE;
1329 gst_adapter_clear (nuv->priv->adapter);
1331 GST_DEBUG_OBJECT (nuv, "activating push/chain function");
1333 GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
1336 gst_object_unref (nuv);
1341 static frame_index_data *
1342 gst_nuv_demux_do_seek_index (GstNuvDemux *nuv, gint64 seek_pos,
1343 gint64 segment_stop, GstFormat format)
1346 frame_index_data *ret = NULL;
1348 if (nuv->priv->index == NULL) {
1352 /* find keyframe closest to the requested position */
1353 for (l = nuv->priv->index; l != NULL; l = l->next) {
1354 frame_index_data *f = (frame_index_data *) l->data;
1357 if (format == GST_FORMAT_BYTES) {
1359 } else if (format == GST_FORMAT_TIME) {
1365 if (pos >= seek_pos) {
1370 if ((segment_stop != -1) && (segment_stop != GST_CLOCK_TIME_NONE) && (pos > segment_stop)) {
1379 gst_nuv_demux_do_seek (GstNuvDemux *nuv, GstEvent * event)
1384 GstSeekType cur_type;
1386 GstSeekType stop_type;
1389 frame_index_data *entry;
1390 gint64 segment_start;
1391 gint64 segment_stop;
1392 GstEvent *newsegment_event;
1394 if (nuv->priv->eos) {
1398 if (nuv->priv->mode == NUV_PUSH_MODE) {
1403 gst_event_parse_seek (event, &rate, &format, &flags,
1404 &cur_type, &cur, &stop_type, &stop);
1409 if (format == GST_FORMAT_TIME) {
1410 GST_DEBUG_OBJECT (nuv, "Can only seek on BYTES");
1416 GST_DEBUG_OBJECT (nuv, "Can only seek with positive rate");
1420 if (cur_type == GST_SEEK_TYPE_SET) {
1421 GST_OBJECT_LOCK (nuv);
1422 if (gst_nuv_demux_do_seek_index (nuv, cur, -1, format) == NULL) {
1423 GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
1424 GST_OBJECT_UNLOCK (nuv);
1427 GST_OBJECT_UNLOCK (nuv);
1430 flush = !!(flags & GST_SEEK_FLAG_FLUSH);
1433 gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_start ());
1434 if (nuv->priv->src_video_pad != NULL) {
1435 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_start ());
1438 if (nuv->priv->src_audio_pad != NULL) {
1439 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_start ());
1443 gst_pad_pause_task (nuv->priv->sinkpad);
1446 GST_PAD_STREAM_LOCK (nuv->priv->sinkpad);
1447 GST_OBJECT_LOCK (nuv);
1450 if (cur == GST_CLOCK_TIME_NONE)
1452 if (stop == GST_CLOCK_TIME_NONE)
1453 stop = nuv->priv->duration_time;
1455 if (cur_type == GST_SEEK_TYPE_SET)
1456 segment_start = cur;
1457 else if (cur_type == GST_SEEK_TYPE_CUR)
1458 segment_start = nuv->priv->segment_start + cur;
1460 segment_start = nuv->priv->segment_start;
1462 if (stop_type == GST_SEEK_TYPE_SET)
1463 segment_stop = stop;
1464 else if (stop_type == GST_SEEK_TYPE_CUR)
1465 segment_stop = nuv->priv->segment_stop + stop;
1467 segment_stop = nuv->priv->segment_stop;
1469 segment_start = CLAMP (segment_start, 0, nuv->priv->duration_time);
1470 segment_stop = CLAMP (segment_stop, 0, nuv->priv->duration_time);
1472 entry = gst_nuv_demux_do_seek_index (nuv, segment_start,
1473 segment_stop, format);
1475 if (entry == NULL) {
1476 GST_DEBUG_OBJECT (nuv, "No matching seek entry in index");
1480 segment_start = entry->timecode;
1482 nuv->priv->segment_start = segment_start;
1483 nuv->priv->segment_stop = segment_stop;
1485 GST_OBJECT_UNLOCK (nuv);
1487 if (!nuv->priv->eos) {
1489 msg = gst_message_new_segment_start (GST_OBJECT (nuv), GST_FORMAT_TIME,
1490 nuv->priv->segment_start);
1492 gst_element_post_message (GST_ELEMENT (nuv), msg);
1495 GST_DEBUG_OBJECT (nuv, "NEW SEGMENT START %" G_GUINT64_FORMAT ", STOP %" G_GUINT64_FORMAT,
1496 segment_start, segment_stop);
1497 newsegment_event = gst_event_new_new_segment (FALSE, rate,
1498 GST_FORMAT_TIME, segment_start, segment_stop, segment_start);
1502 if (nuv->priv->src_video_pad != NULL) {
1503 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_new_flush_stop ());
1506 if (nuv->priv->src_audio_pad != NULL) {
1507 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_new_flush_stop ());
1510 gst_pad_push_event (nuv->priv->sinkpad, gst_event_new_flush_stop ());
1514 if (nuv->priv->src_video_pad != NULL) {
1515 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
1517 if (nuv->priv->src_audio_pad != NULL) {
1518 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
1521 gst_event_unref (newsegment_event);
1523 nuv->priv->state = GST_NUV_DEMUX_FRAME_HEADER;
1524 nuv->priv->offset = entry->offset;
1526 gst_pad_start_task (nuv->priv->sinkpad, (GstTaskFunction) gst_nuv_demux_loop,
1527 nuv->priv->sinkpad);
1529 GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
1533 GST_DEBUG_OBJECT (nuv, "Got a seek error");
1534 GST_OBJECT_UNLOCK (nuv);
1535 GST_PAD_STREAM_UNLOCK (nuv->priv->sinkpad);
1541 gst_nuv_demux_srcpad_event (GstPad * pad, GstEvent * event)
1543 gboolean res = FALSE;
1546 nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1548 switch (GST_EVENT_TYPE (event)) {
1549 case GST_EVENT_SEEK:
1550 res = gst_nuv_demux_do_seek (nuv, event);
1557 gst_object_unref (nuv);
1562 gst_nuv_demux_sink_event (GstPad * pad, GstEvent * event)
1564 gboolean res = FALSE;
1567 nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1569 switch (GST_EVENT_TYPE (event)) {
1570 case GST_EVENT_NEWSEGMENT:
1579 gst_event_parse_new_segment (event, &update, &rate, &format, &start, &stop, &position);
1580 if ((format == GST_FORMAT_BYTES) && (start == 0)) {
1581 g_debug ("NEW SEGMENT 0");
1582 if (nuv->priv->segment > 0) {
1583 nuv->priv->new_file = TRUE;
1584 nuv->priv->state = GST_NUV_DEMUX_START;
1585 nuv->priv->offset = 0;
1587 nuv->priv->segment++;
1590 newsegment_event = gst_event_new_new_segment (FALSE, rate,
1591 GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1593 gst_pad_push_event (nuv->priv->src_audio_pad, gst_event_ref (newsegment_event));
1594 gst_pad_push_event (nuv->priv->src_video_pad, gst_event_ref (newsegment_event));
1595 gst_event_unref (newsegment_event);
1597 res = gst_pad_event_default(pad, event);
1602 res = gst_pad_event_default (pad, event);
1609 static GstFlowReturn
1610 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
1612 GstFlowReturn ret = GST_FLOW_OK;
1613 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1615 if (nuv->priv->mode != NUV_PUSH_MODE)
1618 gst_adapter_push (nuv->priv->adapter, buf);
1620 while ((ret == GST_FLOW_OK) && (nuv->priv->more_data == FALSE)) {
1621 ret = gst_nuv_demux_play (pad);
1624 nuv->priv->more_data = FALSE;
1625 gst_object_unref (nuv);
1631 gst_nuv_demux_loop (GstPad * pad)
1633 gst_nuv_demux_play (pad);
1637 gst_nuv_demux_index_free (gpointer data, gpointer user_data)
1643 gst_nuv_demux_reset (GstNuvDemux * nuv)
1645 nuv->priv->eos = FALSE;
1646 nuv->priv->more_data = FALSE;
1647 nuv->priv->state = GST_NUV_DEMUX_START;
1648 nuv->priv->mode = NUV_PUSH_MODE;
1649 nuv->priv->offset = 0;
1650 nuv->priv->time_start = 0;
1651 nuv->priv->time_qos = GST_CLOCK_TIME_NONE;
1652 nuv->priv->duration_bytes = GST_CLOCK_TIME_NONE;
1653 nuv->priv->duration_time = GST_CLOCK_TIME_NONE;
1654 nuv->priv->last_video_return = GST_FLOW_OK;
1655 nuv->priv->last_audio_return = GST_FLOW_OK;
1656 nuv->priv->header_lengh = 0;
1657 nuv->priv->segment_stop = GST_CLOCK_TIME_NONE;
1658 nuv->priv->segment_start = GST_CLOCK_TIME_NONE;
1659 nuv->priv->new_file = FALSE;
1662 g_slist_foreach (nuv->priv->index, gst_nuv_demux_index_free, NULL);
1663 g_slist_free (nuv->priv->index);
1664 nuv->priv->index = NULL;
1666 gst_adapter_clear (nuv->priv->adapter);
1668 if (nuv->priv->mpeg_buffer != NULL) {
1669 gst_buffer_unref (nuv->priv->mpeg_buffer);
1670 nuv->priv->mpeg_buffer = NULL;
1675 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
1677 if (nuv->priv->src_video_pad) {
1678 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_video_pad);
1679 nuv->priv->src_video_pad = NULL;
1682 if (nuv->priv->src_audio_pad) {
1683 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->priv->src_audio_pad);
1684 nuv->priv->src_audio_pad = NULL;
1688 static GstStateChangeReturn
1689 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
1691 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1693 g_debug ("Nuvdemux state_change");
1694 switch (transition) {
1695 case GST_STATE_CHANGE_NULL_TO_READY:
1696 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1697 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1703 g_debug ("Nuvdemux state_change: 1");
1705 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1706 if (ret == GST_STATE_CHANGE_FAILURE) {
1711 g_debug ("Nuvdemux state_change: 2");
1713 switch (transition) {
1714 case GST_STATE_CHANGE_READY_TO_NULL:
1715 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1716 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1723 g_debug ("Nuvdemux state_change: DONE");
1729 #if (GST_VERSION_MINOR == 10) && (GST_VERSION_MICRO < 6)
1731 gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
1737 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1738 g_return_val_if_fail (nbytes > 0, NULL);
1740 GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);
1742 /* we don't have enough data, return NULL. This is unlikely
1743 * as one usually does an _available() first instead of peeking a
1745 if (G_UNLIKELY (nbytes > adapter->size))
1748 /* our head buffer has enough data left, return it */
1749 cur = adapter->buflist->data;
1750 if (GST_BUFFER_SIZE (cur) >= nbytes + adapter->skip) {
1751 GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
1753 buffer = gst_buffer_create_sub (cur, adapter->skip, nbytes);
1755 gst_adapter_flush (adapter, nbytes);
1760 data = gst_adapter_take (adapter, nbytes);
1764 buffer = gst_buffer_new ();
1765 GST_BUFFER_DATA (buffer) = data;
1766 GST_BUFFER_MALLOCDATA (buffer) = data;
1767 GST_BUFFER_SIZE (buffer) = nbytes;
1774 gst_nuv_typefind (GstTypeFind * tf, gpointer unused)
1776 guint8 *data = gst_type_find_peek (tf, 0, 11);
1779 if (memcmp (data, "MythTVVideo", 11) == 0
1780 || memcmp (data, "NuppelVideo", 11) == 0) {
1781 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
1782 gst_caps_new_simple ("video/x-nuv", NULL));
1788 plugin_init (GstPlugin * plugin)
1790 static gchar *exts[] = { "nuv", NULL };
1792 setlocale (LC_ALL, "");
1793 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1794 #endif /* ENABLE_NLS */
1796 if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
1797 GST_TYPE_NUV_DEMUX)) {
1801 if (!gst_type_find_register (plugin, "video/x-nuv", GST_RANK_SECONDARY,
1804 gst_caps_new_simple ("video/x-nuv", NULL), NULL, NULL)) {
1805 GST_WARNING ("can't register typefind");
1812 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1815 "Demuxes and muxes audio and video",
1816 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)