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[] = {
415 gst_nuv_demux_handle_src_query (GstPad * pad, GstQuery * query)
417 gboolean res = FALSE;
418 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
420 switch (GST_QUERY_TYPE (query)) {
421 case GST_QUERY_POSITION:
422 if (GST_CLOCK_TIME_IS_VALID (nuv->last_frame_time)) {
423 gst_query_set_position (query, GST_FORMAT_TIME,
424 nuv->last_frame_time);
426 GST_DEBUG_OBJECT (nuv, "POS %d", nuv->last_frame_time);
434 gst_object_unref (nuv);
439 //TODO: create a function to control events and send to src pads
441 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
443 if (nuv->h->i_video_blocks != 0) {
444 GstCaps *video_caps = NULL;
447 gst_pad_new_from_static_template (&video_src_template, "video_src");
449 video_caps = gst_caps_new_simple ("video/x-divx",
450 "divxversion", G_TYPE_INT, 4,
451 "width", G_TYPE_INT, nuv->h->i_width,
452 "height", G_TYPE_INT, nuv->h->i_height,
453 "framerate", GST_TYPE_FRACTION, nuv->h->i_fpsn, nuv->h->i_fpsd,
454 "format", GST_TYPE_FOURCC, nuv->eh->i_video_fcc,
455 "pixel-aspect-ratio", GST_TYPE_FRACTION,
456 (gint) (nuv->h->d_aspect * 1000.0f), 1000, NULL);
458 gst_pad_use_fixed_caps (nuv->src_video_pad);
459 gst_pad_set_caps (nuv->src_video_pad, video_caps);
460 gst_pad_set_active (nuv->src_video_pad, TRUE);
461 gst_pad_set_query_type_function (nuv->src_video_pad, gst_nuv_demux_get_src_query_types);
462 gst_pad_set_query_function (nuv->src_video_pad, gst_nuv_demux_handle_src_query);
463 gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
464 gst_caps_unref (video_caps);
467 if (nuv->h->i_audio_blocks != 0) {
468 GstCaps *audio_caps = NULL;
471 gst_pad_new_from_static_template (&audio_src_template, "audio_src");
473 audio_caps = gst_caps_new_simple ("audio/mpeg",
474 "rate", G_TYPE_INT, nuv->eh->i_audio_sample_rate,
475 "format", GST_TYPE_FOURCC, nuv->eh->i_audio_fcc,
476 "channels", G_TYPE_INT, nuv->eh->i_audio_channels,
477 "mpegversion", G_TYPE_INT, nuv->eh->i_version, NULL);
479 gst_pad_use_fixed_caps (nuv->src_audio_pad);
480 gst_pad_set_caps (nuv->src_audio_pad, audio_caps);
481 gst_pad_set_active (nuv->src_audio_pad, TRUE);
482 gst_pad_set_query_type_function (nuv->src_video_pad, gst_nuv_demux_get_src_query_types);
483 gst_pad_set_query_function (nuv->src_video_pad, gst_nuv_demux_handle_src_query);
484 gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
485 gst_caps_unref (audio_caps);
488 gst_element_no_more_pads (GST_ELEMENT (nuv));
492 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
494 GstFlowReturn ret = GST_FLOW_OK;
502 ret = gst_nuv_demux_frame_header_load (nuv, &nuv->fh);
503 if (ret != GST_FLOW_OK)
506 nuv->state = GST_NUV_DEMUX_MOVI;
511 gst_nuv_combine_flow (GstNuvDemux *nuv)
513 GstFlowReturn ret_video = nuv->last_video_return;
514 GstFlowReturn ret_audio = nuv->last_audio_return;
516 if ((ret_video != GST_FLOW_OK) &&
517 (ret_audio != GST_FLOW_OK))
520 if (GST_FLOW_IS_FATAL (nuv->last_video_return))
523 if (GST_FLOW_IS_FATAL (nuv->last_audio_return))
530 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
532 GstFlowReturn ret = GST_FLOW_OK;
533 GstBuffer *buf = NULL;
534 nuv_frame_header *h = nuv->fh;
536 if (h->i_type == 'R')
539 if (h->i_length > 0) {
540 ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
541 if ((ret != GST_FLOW_OK) || (buf == NULL))
544 if (h->i_timecode < 0) {
545 h->i_timecode = h->i_timecode * -1;
546 nuv->time_offset = h->i_timecode;
549 h->i_timecode += nuv->time_offset;
551 GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND;
552 nuv->last_frame_time = h->i_timecode * GST_MSECOND;
558 if (h->i_length == 0)
561 if (nuv->new_video_segment) {
562 /* send new segment event*/
563 gst_pad_push_event (nuv->src_video_pad,
564 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
565 GST_CLOCK_TIME_NONE, 0));
566 nuv->new_video_segment = FALSE;
569 GST_BUFFER_SIZE (buf) = h->i_length;
570 gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_video_pad));
571 nuv->last_video_return = gst_pad_push (nuv->src_video_pad, buf);
572 if (!gst_nuv_combine_flow (nuv)) {
573 ret = nuv->last_video_return;
574 GST_WARNING_OBJECT (nuv, "error: %d pushing on srcpad %s, is linked? = %d",
575 nuv->last_video_return, gst_pad_get_name (nuv->src_video_pad), gst_pad_is_linked (nuv->src_video_pad));
581 if (h->i_length == 0)
584 if (nuv->new_audio_segment) {
585 /* send new segment event*/
586 gst_pad_push_event (nuv->src_audio_pad,
587 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
588 GST_CLOCK_TIME_NONE, 0));
589 nuv->new_audio_segment = FALSE;
592 GST_BUFFER_SIZE (buf) = h->i_length;
593 gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_audio_pad));
594 nuv->last_audio_return = gst_pad_push (nuv->src_audio_pad, buf);
595 if (!gst_nuv_combine_flow (nuv)) {
596 ret = nuv->last_audio_return;
597 GST_WARNING_OBJECT (nuv, "Error %d pushing on srcpad %s, is linked? = %d",
598 nuv->last_audio_return, gst_pad_get_name (nuv->src_audio_pad), gst_pad_is_linked (nuv->src_audio_pad));
604 switch (h->i_compression) {
606 if ( !gst_pad_is_linked( nuv->src_video_pad ) )
609 GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h->i_timecode);
610 gst_pad_push_event (nuv->src_video_pad,
611 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h->i_timecode * GST_MSECOND,
612 GST_CLOCK_TIME_NONE, 0));
615 if ( !gst_pad_is_linked( nuv->src_audio_pad ) )
618 GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h->i_timecode);
619 gst_pad_push_event (nuv->src_audio_pad,
620 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
621 GST_CLOCK_TIME_NONE, 0));
627 gst_buffer_unref (buf);
631 gst_buffer_unref (buf);
637 nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
644 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
646 GstFlowReturn ret = GST_FLOW_OK;
648 /* ffmpeg extra data */
650 gst_nuv_demux_read_bytes (nuv, nuv->mpeg_data_size, TRUE,
652 if ((ret != GST_FLOW_OK) || (nuv->mpeg_buffer == NULL)) {
655 GST_BUFFER_SIZE (nuv->mpeg_buffer) = nuv->mpeg_data_size;
656 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
661 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
663 GstFlowReturn ret = GST_FLOW_OK;
668 ret = gst_nuv_demux_frame_header_load (nuv, &h);
669 if (ret != GST_FLOW_OK)
672 if (h->i_type != 'D') {
674 return GST_FLOW_ERROR;
677 if (h->i_length > 0) {
678 if (h->i_compression == 'F') {
679 nuv->state = GST_NUV_DEMUX_MPEG_DATA;
682 return GST_FLOW_ERROR;
685 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
694 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
696 GstFlowReturn ret = GST_FLOW_OK;
698 ret = gst_nuv_demux_extended_header_load (nuv, &nuv->eh);
699 if (ret != GST_FLOW_OK)
702 gst_nuv_demux_create_pads (nuv);
703 nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
708 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
710 GstBuffer *buf = NULL;
711 GstFlowReturn res = GST_FLOW_OK;
713 res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
714 if ((res != GST_FLOW_OK) || (buf == NULL)) {
716 gst_buffer_unref (buf);
721 if (buf->data[0] == 'X') {
722 gst_buffer_unref (buf);
724 nuv_frame_header *h = NULL;
726 res = gst_nuv_demux_frame_header_load (nuv, &h);
727 if (res != GST_FLOW_OK)
730 if (h->i_length != 512) {
732 return GST_FLOW_ERROR;
736 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
738 nuv->state = GST_NUV_DEMUX_INVALID_DATA;
739 g_object_unref (buf);
740 GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
741 (_("incomplete NUV support")), ("incomplete NUV support"));
742 return GST_FLOW_ERROR;
748 gst_nuv_demux_play (GstPad * pad)
750 GstFlowReturn res = GST_FLOW_OK;
751 GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
753 switch (nuv->state) {
754 case GST_NUV_DEMUX_START:
755 res = gst_nuv_demux_stream_file_header (nuv);
756 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
759 if (nuv->state != GST_NUV_DEMUX_HEADER_DATA)
762 case GST_NUV_DEMUX_HEADER_DATA:
763 res = gst_nuv_demux_stream_header_data (nuv);
764 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
767 if (nuv->state != GST_NUV_DEMUX_EXTRA_DATA)
770 case GST_NUV_DEMUX_EXTRA_DATA:
771 res = gst_nuv_demux_stream_extra_data (nuv);
772 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
775 if (nuv->state != GST_NUV_DEMUX_MPEG_DATA)
778 case GST_NUV_DEMUX_MPEG_DATA:
779 res = gst_nuv_demux_stream_mpeg_data (nuv);
780 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
784 if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER)
787 case GST_NUV_DEMUX_EXTEND_HEADER:
788 res = gst_nuv_demux_stream_extend_header (nuv);
789 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
792 if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER_DATA)
795 case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
796 res = gst_nuv_demux_stream_extend_header_data (nuv);
797 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
801 if (nuv->state != GST_NUV_DEMUX_FRAME_HEADER)
804 case GST_NUV_DEMUX_FRAME_HEADER:
805 res = gst_nuv_demux_read_head_frame (nuv);
806 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
809 if (nuv->state != GST_NUV_DEMUX_MOVI)
812 case GST_NUV_DEMUX_MOVI:
813 res = gst_nuv_demux_stream_data (nuv);
814 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
818 case GST_NUV_DEMUX_INVALID_DATA:
822 g_assert_not_reached ();
825 GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->state,
826 gst_flow_get_name (res));
831 GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
832 gst_pad_pause_task (nuv->sinkpad);
833 if (GST_FLOW_IS_FATAL (res)) {
834 GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
835 (_("Internal data stream error.")),
836 ("streaming stopped, reason %s", gst_flow_get_name (res)));
838 gst_nuv_demux_send_eos (nuv);
844 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
846 gst_element_post_message (GST_ELEMENT (nuv),
847 gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
849 if (nuv->src_video_pad)
850 gst_pad_push_event (nuv->src_video_pad, gst_event_new_eos ());
851 if (nuv->src_audio_pad)
852 gst_pad_push_event (nuv->src_audio_pad, gst_event_new_eos ());
856 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
859 GstFlowReturn ret = GST_FLOW_OK;
865 if (nuv->mode == NUV_PULL_MODE) {
866 ret = gst_pad_pull_range (nuv->sinkpad, nuv->offset, size, buffer);
867 if (ret == GST_FLOW_OK) {
872 } else if (ret == GST_FLOW_UNEXPECTED) {
874 gst_buffer_unref (buffer);
876 gst_nuv_demux_send_eos (nuv);
877 return GST_FLOW_WRONG_STATE;
880 if (gst_adapter_available (nuv->adapter) < size)
881 return GST_FLOW_ERROR_NO_DATA;
885 data = (guint8 *) gst_adapter_take (nuv->adapter, size);
886 *buffer = gst_buffer_new ();
887 gst_buffer_set_data (*buffer, data, size);
890 data = (guint8 *) gst_adapter_peek (nuv->adapter, size);
891 *buffer = gst_buffer_new ();
892 gst_buffer_set_data (*buffer, data, size);
899 gst_nuv_demux_sink_activate (GstPad * sinkpad)
902 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
904 if (gst_pad_check_pull_range (sinkpad)) {
905 res = gst_pad_activate_pull (sinkpad, TRUE);
907 res = gst_pad_activate_push (sinkpad, TRUE);
909 g_object_unref (nuv);
914 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
916 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
919 GST_DEBUG_OBJECT (nuv, "activating pull function");
920 nuv->mode = NUV_PULL_MODE;
922 gst_adapter_clear (nuv->adapter);
923 g_object_unref (nuv->adapter);
926 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
928 GST_DEBUG_OBJECT (nuv, "deactivating pull function");
929 gst_pad_stop_task (sinkpad);
931 gst_object_unref (nuv);
937 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
939 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
942 nuv->mode = NUV_PUSH_MODE;
944 gst_adapter_clear (nuv->adapter);
946 nuv->adapter = gst_adapter_new ();
948 GST_DEBUG_OBJECT (nuv, "activating push/chain function");
950 GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
953 gst_object_unref (nuv);
959 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
961 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
963 GST_DEBUG_OBJECT (nuv, " gst_nuv_demux_chain function");
964 gst_adapter_push (nuv->adapter, buf);
966 gst_object_unref (nuv);
968 return gst_nuv_demux_play (pad);
972 gst_nuv_demux_loop (GstPad * pad)
974 gst_nuv_demux_play (pad);
978 gst_nuv_demux_reset (GstNuvDemux * nuv)
980 nuv->state = GST_NUV_DEMUX_START;
983 nuv->time_offset = 0;
985 if (nuv->adapter != NULL)
986 gst_adapter_clear (nuv->adapter);
988 if (nuv->mpeg_buffer != NULL) {
989 gst_buffer_unref (nuv->mpeg_buffer);
990 nuv->mpeg_buffer = NULL;
1004 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
1006 if (nuv->src_video_pad) {
1007 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
1008 nuv->src_video_pad = NULL;
1011 if (nuv->src_audio_pad) {
1012 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
1013 nuv->src_audio_pad = NULL;
1017 static GstStateChangeReturn
1018 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
1020 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1022 switch (transition) {
1023 case GST_STATE_CHANGE_READY_TO_PAUSED:
1029 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1030 if (ret == GST_STATE_CHANGE_FAILURE)
1033 switch (transition) {
1034 case GST_STATE_CHANGE_PAUSED_TO_READY:
1035 GST_DEBUG_OBJECT (element, "GST_STATE_CHANGE_PAUSED_TO_READY");
1036 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1037 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1048 plugin_init (GstPlugin * plugin)
1051 setlocale (LC_ALL, "");
1052 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1053 #endif /* ENABLE_NLS */
1055 if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
1056 GST_TYPE_NUV_DEMUX)) {
1062 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1065 "Demuxes and muxes audio and video",
1066 plugin_init, VERSION, "LGPL", "NuvDemux", "")