[svn r88] Fixes the autotools prefix directory, added do_seek callback to the source.
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 GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug);
60 #define GST_CAT_DEFAULT nuvdemux_debug
63 #define GST_FLOW_ERROR_NO_DATA -101
70 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
72 static const GstElementDetails gst_nuv_demux_details =
73 GST_ELEMENT_DETAILS ("Nuv demuxer",
75 "Demultiplex a .nuv file into audio and video",
76 "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
77 "Rosfran Borges <rosfran.borges@indt.org.br>");
79 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
82 GST_STATIC_CAPS ("video/x-nuv"));
84 static GstStaticPadTemplate audio_src_template =
85 GST_STATIC_PAD_TEMPLATE ("audio_src",
90 static GstStaticPadTemplate video_src_template =
91 GST_STATIC_PAD_TEMPLATE ("video_src",
96 static void gst_nuv_demux_finalize (GObject * object);
97 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
98 GstStateChange transition);
99 static void gst_nuv_demux_loop (GstPad * pad);
100 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
101 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
102 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
104 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
106 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
107 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
108 gboolean move, GstBuffer ** buffer);
109 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
110 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
111 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
113 /* GObject methods */
114 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
116 #if G_BYTE_ORDER == G_BIG_ENDIAN
117 static inline gdouble
118 _gdouble_swap_le_be (gdouble * d)
127 u.i = GUINT64_SWAP_LE_BE (u.i);
131 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
132 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
133 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
134 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
137 double2fraction (double in, int *num, int *denom)
142 } else if (in == 23.976) {
147 while (in - floor(in) >= 0.1) {
151 *num = (int)floor(in);
156 gst_nuv_demux_base_init (gpointer klass)
158 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
160 gst_element_class_add_pad_template (element_class,
161 gst_static_pad_template_get (&audio_src_template));
163 gst_element_class_add_pad_template (element_class,
164 gst_static_pad_template_get (&video_src_template));
166 gst_element_class_add_pad_template (element_class,
167 gst_static_pad_template_get (&sink_template));
168 gst_element_class_set_details (element_class, &gst_nuv_demux_details);
172 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
174 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
175 GObjectClass *gobject_class = (GObjectClass *) klass;
177 GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
178 0, "Demuxer for NUV streams");
180 parent_class = g_type_class_peek_parent (klass);
182 gobject_class->finalize = gst_nuv_demux_finalize;
183 gstelement_class->change_state = gst_nuv_demux_change_state;
187 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
189 nuv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
191 gst_pad_set_activate_function (nuv->sinkpad, gst_nuv_demux_sink_activate);
193 gst_pad_set_activatepull_function (nuv->sinkpad,
194 gst_nuv_demux_sink_activate_pull);
196 gst_pad_set_activatepush_function (nuv->sinkpad,
197 gst_nuv_demux_sink_activate_push);
199 gst_pad_set_chain_function (nuv->sinkpad,
200 GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
202 gst_element_add_pad (GST_ELEMENT (nuv), nuv->sinkpad);
205 nuv->mpeg_buffer = NULL;
210 nuv->new_audio_segment = TRUE;
211 nuv->new_video_segment = TRUE;
213 gst_nuv_demux_reset (nuv);
217 gst_nuv_demux_finalize (GObject * object)
219 GstNuvDemux *nuv = GST_NUV_DEMUX (object);
221 if (nuv->mpeg_buffer != NULL) {
222 gst_buffer_unref (nuv->mpeg_buffer);
225 gst_nuv_demux_destoy_src_pad (nuv);
226 gst_nuv_demux_reset (nuv);
227 if (nuv->adapter != NULL) {
228 gst_object_unref (nuv->adapter);
230 G_OBJECT_CLASS (parent_class)->finalize (object);
237 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header ** h_ret)
239 GstBuffer *buffer = NULL;
240 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
242 if ((res != GST_FLOW_OK) || (buffer == NULL))
245 nuv_header *h = g_new0 (nuv_header, 1);
247 memcpy (h->id, buffer->data, 12);
248 memcpy (h->version, buffer->data + 12, 5);
249 h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
250 h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
251 h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
252 h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
253 h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
254 h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
255 h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
256 /* get the num and denom values from fps */
257 double2fraction (h->d_fps, &h->i_fpsn, &h->i_fpsd);
258 h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
259 h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
260 h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
261 h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
263 GST_DEBUG_OBJECT (nuv,
264 "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
265 h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
266 h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
267 h->i_keyframe_distance);
270 gst_buffer_unref (buffer);
275 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
277 GstFlowReturn res = gst_nuv_demux_header_load (nuv, &nuv->h);
279 if (res == GST_FLOW_OK)
280 nuv->state = GST_NUV_DEMUX_EXTRA_DATA;
288 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
290 GstFlowReturn res = GST_FLOW_OK;
291 GstBuffer *file_header = NULL;
293 res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
294 if ((res != GST_FLOW_OK) || (file_header == NULL)) {
297 if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
298 strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
299 nuv->state = GST_NUV_DEMUX_HEADER_DATA;
301 GST_DEBUG_OBJECT (nuv, "error parsing file header");
302 nuv->state = GST_NUV_DEMUX_INVALID_DATA;
303 res = GST_FLOW_ERROR;
306 if (file_header != NULL) {
307 gst_buffer_unref (file_header);
315 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header ** h_ret)
319 GstBuffer *buf = NULL;
321 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
323 if ((res != GST_FLOW_OK) || (buf == NULL)) {
325 gst_buffer_unref (buf);
330 h = g_new0 (nuv_frame_header, 1);
333 h->i_type = GPOINTER_TO_INT (data[0]);
334 h->i_compression = GPOINTER_TO_INT (data[1]);
335 h->i_keyframe = GPOINTER_TO_INT (data[2]);
336 h->i_filters = GPOINTER_TO_INT (data[3]);
337 h->i_timecode = GST_READ_UINT32_LE (&data[4]);
338 h->i_length = GST_READ_UINT32_LE (&data[8]);
340 GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
342 h->i_compression ? h->i_compression : ' ',
343 h->i_keyframe ? h->i_keyframe : ' ',
344 h->i_filters, h->i_timecode, h->i_length);
347 gst_buffer_unref (buf);
352 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
353 nuv_extended_header ** h_ret)
356 GstBuffer *buff = NULL;
357 nuv_extended_header *h;
360 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
362 if ((res != GST_FLOW_OK) || (buff == NULL)) {
364 gst_buffer_unref (buff);
369 h = g_new0 (nuv_extended_header, 1);
371 h->i_version = GST_READ_UINT32_LE (&data[0]);
372 h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
373 h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
374 h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
375 h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
376 h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
377 h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
378 h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
379 h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
380 h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
381 h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
382 h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
383 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
384 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
385 h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
386 h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
387 h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
389 GST_DEBUG_OBJECT (nuv,
390 "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
391 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
392 h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
393 h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
394 h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
395 h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
396 h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
397 h->i_keyframe_adjust_offset);
400 gst_buffer_unref (buff);
403 static const GstQueryType *
404 gst_nuv_demux_get_src_query_types (GstPad * pad)
406 static const GstQueryType src_types[] = {
416 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
418 gboolean res = FALSE;
419 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
421 switch (GST_QUERY_TYPE (query)) {
422 case GST_QUERY_POSITION:
423 if (GST_CLOCK_TIME_IS_VALID (nuv->last_frame_time)) {
424 gst_query_set_position (query, GST_FORMAT_TIME,
425 nuv->last_frame_time);
427 GST_DEBUG_OBJECT (nuv, "POS %d", nuv->last_frame_time);
430 case GST_QUERY_DURATION:
431 if (nuv->duration != 0) {
435 fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
436 total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
437 gst_query_set_duration (query, GST_FORMAT_TIME, total);
438 GST_DEBUG_OBJECT (nuv, "DURATION %d", total);
447 gst_object_unref (nuv);
452 //TODO: create a function to control events and send to src pads
454 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
456 if (nuv->h->i_video_blocks != 0) {
457 GstCaps *video_caps = NULL;
460 gst_pad_new_from_static_template (&video_src_template, "video_src");
462 video_caps = gst_caps_new_simple ("video/x-divx",
463 "divxversion", G_TYPE_INT, 4,
464 "width", G_TYPE_INT, nuv->h->i_width,
465 "height", G_TYPE_INT, nuv->h->i_height,
466 "framerate", GST_TYPE_FRACTION, nuv->h->i_fpsn, nuv->h->i_fpsd,
467 "format", GST_TYPE_FOURCC, nuv->eh->i_video_fcc,
468 "pixel-aspect-ratio", GST_TYPE_FRACTION,
469 (gint) (nuv->h->d_aspect * 1000.0f), 1000, NULL);
471 gst_pad_use_fixed_caps (nuv->src_video_pad);
472 gst_pad_set_caps (nuv->src_video_pad, video_caps);
473 gst_pad_set_active (nuv->src_video_pad, TRUE);
474 gst_pad_set_query_type_function (nuv->src_video_pad, gst_nuv_demux_get_src_query_types);
475 gst_pad_set_query_function (nuv->src_video_pad, gst_nuv_demux_handle_src_query);
476 gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
477 gst_caps_unref (video_caps);
480 if (nuv->h->i_audio_blocks != 0) {
481 GstCaps *audio_caps = NULL;
484 gst_pad_new_from_static_template (&audio_src_template, "audio_src");
486 audio_caps = gst_caps_new_simple ("audio/mpeg",
487 "rate", G_TYPE_INT, nuv->eh->i_audio_sample_rate,
488 "format", GST_TYPE_FOURCC, nuv->eh->i_audio_fcc,
489 "channels", G_TYPE_INT, nuv->eh->i_audio_channels,
490 "mpegversion", G_TYPE_INT, nuv->eh->i_version, NULL);
492 gst_pad_use_fixed_caps (nuv->src_audio_pad);
493 gst_pad_set_caps (nuv->src_audio_pad, audio_caps);
494 gst_pad_set_active (nuv->src_audio_pad, TRUE);
495 gst_pad_set_query_type_function (nuv->src_video_pad, gst_nuv_demux_get_src_query_types);
496 gst_pad_set_query_function (nuv->src_video_pad, gst_nuv_demux_handle_src_query);
497 gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
498 gst_caps_unref (audio_caps);
501 gst_element_no_more_pads (GST_ELEMENT (nuv));
505 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
507 GstFlowReturn ret = GST_FLOW_OK;
515 ret = gst_nuv_demux_frame_header_load (nuv, &nuv->fh);
516 if (ret != GST_FLOW_OK)
519 nuv->state = GST_NUV_DEMUX_MOVI;
524 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
526 GstFlowReturn ret = GST_FLOW_OK;
527 GstBuffer *buf = NULL;
528 nuv_frame_header *h = nuv->fh;
530 if (h->i_type == 'R')
533 if (h->i_length > 0) {
534 ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
535 if ((ret != GST_FLOW_OK) || (buf == NULL))
538 if (h->i_timecode < 0) {
539 h->i_timecode = h->i_timecode * -1;
540 nuv->time_offset = h->i_timecode;
543 h->i_timecode += nuv->time_offset;
545 GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND;
546 nuv->last_frame_time = h->i_timecode * GST_MSECOND;
552 if (h->i_length == 0)
555 if (!gst_pad_is_linked (nuv->src_video_pad)) {
556 gst_buffer_unref (buf);
560 if (nuv->new_video_segment) {
561 /* send new segment event*/
562 gst_pad_push_event (nuv->src_video_pad,
563 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
564 GST_CLOCK_TIME_NONE, 0));
565 nuv->new_video_segment = FALSE;
568 GST_BUFFER_SIZE (buf) = h->i_length;
569 gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_video_pad));
570 ret = gst_pad_push (nuv->src_video_pad, buf);
571 if (ret != GST_FLOW_OK) {
572 GST_WARNING_OBJECT (nuv, "error pushing on srcpad %s, is linked? = %d",
573 gst_pad_get_name (nuv->src_video_pad), gst_pad_is_linked (nuv->src_video_pad));
579 if (h->i_length == 0)
582 if (!gst_pad_is_linked (nuv->src_audio_pad)) {
583 gst_buffer_unref (buf);
587 if (nuv->new_audio_segment) {
588 /* send new segment event*/
589 gst_pad_push_event (nuv->src_audio_pad,
590 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
591 GST_CLOCK_TIME_NONE, 0));
592 nuv->new_audio_segment = FALSE;
595 GST_BUFFER_SIZE (buf) = h->i_length;
596 gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_audio_pad));
597 ret = gst_pad_push (nuv->src_audio_pad, buf);
598 if (ret != GST_FLOW_OK) {
599 GST_WARNING_OBJECT (nuv, "Error pushing on srcpad %s, is linked? = %d",
600 gst_pad_get_name (nuv->src_audio_pad), gst_pad_is_linked (nuv->src_audio_pad));
606 switch (h->i_compression) {
608 if ( !gst_pad_is_linked( nuv->src_video_pad ) )
611 GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h->i_timecode);
612 gst_pad_push_event (nuv->src_video_pad,
613 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h->i_timecode * GST_MSECOND,
614 GST_CLOCK_TIME_NONE, 0));
617 if ( !gst_pad_is_linked( nuv->src_audio_pad ) )
620 GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h->i_timecode);
621 gst_pad_push_event (nuv->src_audio_pad,
622 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
623 GST_CLOCK_TIME_NONE, 0));
629 gst_buffer_unref (buf);
633 gst_buffer_unref (buf);
639 nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
646 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
648 GstFlowReturn ret = GST_FLOW_OK;
650 /* ffmpeg extra data */
652 gst_nuv_demux_read_bytes (nuv, nuv->mpeg_data_size, TRUE,
654 if ((ret != GST_FLOW_OK) || (nuv->mpeg_buffer == NULL)) {
657 GST_BUFFER_SIZE (nuv->mpeg_buffer) = nuv->mpeg_data_size;
658 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
663 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
665 GstFlowReturn ret = GST_FLOW_OK;
670 ret = gst_nuv_demux_frame_header_load (nuv, &h);
671 if (ret != GST_FLOW_OK)
674 if (h->i_type != 'D') {
676 return GST_FLOW_ERROR;
679 if (h->i_length > 0) {
680 if (h->i_compression == 'F') {
681 nuv->state = GST_NUV_DEMUX_MPEG_DATA;
684 return GST_FLOW_ERROR;
687 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
696 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
698 GstFlowReturn ret = GST_FLOW_OK;
700 ret = gst_nuv_demux_extended_header_load (nuv, &nuv->eh);
701 if (ret != GST_FLOW_OK)
704 gst_nuv_demux_create_pads (nuv);
705 nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
710 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
712 GstBuffer *buf = NULL;
713 GstFlowReturn res = GST_FLOW_OK;
715 res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
716 if ((res != GST_FLOW_OK) || (buf == NULL)) {
718 gst_buffer_unref (buf);
723 if (buf->data[0] == 'X') {
724 gst_buffer_unref (buf);
726 nuv_frame_header *h = NULL;
728 res = gst_nuv_demux_frame_header_load (nuv, &h);
729 if (res != GST_FLOW_OK)
732 if (h->i_length != 512) {
734 return GST_FLOW_ERROR;
738 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
740 nuv->state = GST_NUV_DEMUX_INVALID_DATA;
741 g_object_unref (buf);
742 GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
743 (_("incomplete NUV support")), ("incomplete NUV support"));
744 return GST_FLOW_ERROR;
750 gst_nuv_demux_play (GstPad * pad)
752 GstFlowReturn res = GST_FLOW_OK;
753 GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
755 switch (nuv->state) {
756 case GST_NUV_DEMUX_START:
757 res = gst_nuv_demux_stream_file_header (nuv);
758 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
761 if (nuv->state != GST_NUV_DEMUX_HEADER_DATA)
764 case GST_NUV_DEMUX_HEADER_DATA:
765 res = gst_nuv_demux_stream_header_data (nuv);
766 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
769 if (nuv->state != GST_NUV_DEMUX_EXTRA_DATA)
772 case GST_NUV_DEMUX_EXTRA_DATA:
773 res = gst_nuv_demux_stream_extra_data (nuv);
774 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
777 if (nuv->state != GST_NUV_DEMUX_MPEG_DATA)
780 case GST_NUV_DEMUX_MPEG_DATA:
781 res = gst_nuv_demux_stream_mpeg_data (nuv);
782 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
786 if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER)
789 case GST_NUV_DEMUX_EXTEND_HEADER:
790 res = gst_nuv_demux_stream_extend_header (nuv);
791 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
794 if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER_DATA)
797 case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
798 res = gst_nuv_demux_stream_extend_header_data (nuv);
799 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
803 if (nuv->state != GST_NUV_DEMUX_FRAME_HEADER)
806 case GST_NUV_DEMUX_FRAME_HEADER:
807 res = gst_nuv_demux_read_head_frame (nuv);
808 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
811 if (nuv->state != GST_NUV_DEMUX_MOVI)
814 case GST_NUV_DEMUX_MOVI:
815 res = gst_nuv_demux_stream_data (nuv);
816 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
820 case GST_NUV_DEMUX_INVALID_DATA:
824 g_assert_not_reached ();
827 GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->state,
828 gst_flow_get_name (res));
833 GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
834 gst_pad_pause_task (nuv->sinkpad);
835 if (GST_FLOW_IS_FATAL (res)) {
836 GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
837 (_("Internal data stream error.")),
838 ("streaming stopped, reason %s", gst_flow_get_name (res)));
840 gst_nuv_demux_send_eos (nuv);
846 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
848 gst_element_post_message (GST_ELEMENT (nuv),
849 gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
851 if (nuv->src_video_pad)
852 gst_pad_push_event (nuv->src_video_pad, gst_event_new_eos ());
853 if (nuv->src_audio_pad)
854 gst_pad_push_event (nuv->src_audio_pad, gst_event_new_eos ());
858 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
861 GstFlowReturn ret = GST_FLOW_OK;
867 if (nuv->mode == NUV_PULL_MODE) {
868 ret = gst_pad_pull_range (nuv->sinkpad, nuv->offset, size, buffer);
869 if (ret == GST_FLOW_OK) {
874 } else if (ret == GST_FLOW_UNEXPECTED) {
876 gst_buffer_unref (buffer);
878 gst_nuv_demux_send_eos (nuv);
879 return GST_FLOW_WRONG_STATE;
882 if (gst_adapter_available (nuv->adapter) < size)
883 return GST_FLOW_ERROR_NO_DATA;
887 data = (guint8 *) gst_adapter_take (nuv->adapter, size);
888 *buffer = gst_buffer_new ();
889 gst_buffer_set_data (*buffer, data, size);
892 data = (guint8 *) gst_adapter_peek (nuv->adapter, size);
893 *buffer = gst_buffer_new ();
894 gst_buffer_set_data (*buffer, data, size);
901 gst_nuv_demux_sink_activate (GstPad * sinkpad)
904 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
906 if (gst_pad_check_pull_range (sinkpad)) {
907 res = gst_pad_activate_pull (sinkpad, TRUE);
909 res = gst_pad_activate_push (sinkpad, TRUE);
911 g_object_unref (nuv);
916 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
918 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
921 GST_DEBUG_OBJECT (nuv, "activating pull function");
922 nuv->mode = NUV_PULL_MODE;
924 gst_adapter_clear (nuv->adapter);
925 g_object_unref (nuv->adapter);
928 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
930 GST_DEBUG_OBJECT (nuv, "deactivating pull function");
931 gst_pad_stop_task (sinkpad);
933 gst_object_unref (nuv);
939 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
941 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
944 nuv->mode = NUV_PUSH_MODE;
946 gst_adapter_clear (nuv->adapter);
948 nuv->adapter = gst_adapter_new ();
950 GST_DEBUG_OBJECT (nuv, "activating push/chain function");
952 GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
955 gst_object_unref (nuv);
961 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
963 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
965 GST_DEBUG_OBJECT (nuv, " gst_nuv_demux_chain function");
966 gst_adapter_push (nuv->adapter, buf);
968 gst_object_unref (nuv);
970 return gst_nuv_demux_play (pad);
974 gst_nuv_demux_loop (GstPad * pad)
976 gst_nuv_demux_play (pad);
980 gst_nuv_demux_reset (GstNuvDemux * nuv)
982 nuv->state = GST_NUV_DEMUX_START;
985 nuv->time_offset = 0;
987 if (nuv->adapter != NULL)
988 gst_adapter_clear (nuv->adapter);
990 if (nuv->mpeg_buffer != NULL) {
991 gst_buffer_unref (nuv->mpeg_buffer);
992 nuv->mpeg_buffer = NULL;
1006 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
1008 if (nuv->src_video_pad) {
1009 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
1010 nuv->src_video_pad = NULL;
1013 if (nuv->src_audio_pad) {
1014 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
1015 nuv->src_audio_pad = NULL;
1019 static GstStateChangeReturn
1020 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
1022 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1024 switch (transition) {
1025 case GST_STATE_CHANGE_READY_TO_PAUSED:
1031 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1032 if (ret == GST_STATE_CHANGE_FAILURE)
1035 switch (transition) {
1036 case GST_STATE_CHANGE_PAUSED_TO_READY:
1037 GST_DEBUG_OBJECT (element, "GST_STATE_CHANGE_PAUSED_TO_READY");
1038 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1039 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1050 plugin_init (GstPlugin * plugin)
1053 setlocale (LC_ALL, "");
1054 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1055 #endif /* ENABLE_NLS */
1057 if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
1058 GST_TYPE_NUV_DEMUX)) {
1064 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1067 "Demuxes and muxes audio and video",
1068 plugin_init, VERSION, "LGPL", "NuvDemux", "")