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>
55 #include "glib/gi18n.h"
56 #include "gstnuvdemux.h"
58 GST_DEBUG_CATEGORY_STATIC (nuvdemux_debug);
59 #define GST_CAT_DEFAULT nuvdemux_debug
62 #define GST_FLOW_ERROR_NO_DATA -101
64 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
66 static const GstElementDetails gst_nuv_demux_details =
67 GST_ELEMENT_DETAILS ("Nuv demuxer",
69 "Demultiplex a .nuv file into audio and video",
70 "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
71 "Rosfran Borges <rosfran.borges@indt.org.br>");
73 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
76 GST_STATIC_CAPS ("video/x-nuv"));
78 static GstStaticPadTemplate audio_src_template =
79 GST_STATIC_PAD_TEMPLATE ("audio_src",
84 static GstStaticPadTemplate video_src_template =
85 GST_STATIC_PAD_TEMPLATE ("video_src",
90 static void gst_nuv_demux_finalize (GObject * object);
91 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
92 GstStateChange transition);
93 static void gst_nuv_demux_loop (GstPad * pad);
94 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
95 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
96 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
98 static gboolean gst_nuv_demux_sink_activate_push (GstPad * pad,
100 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
101 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
102 gboolean move, GstBuffer ** buffer);
103 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
104 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
105 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
107 /* GObject methods */
108 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
110 #if G_BYTE_ORDER == G_BIG_ENDIAN
111 static inline gdouble
112 _gdouble_swap_le_be (gdouble * d)
121 u.i = GUINT64_SWAP_LE_BE (u.i);
125 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
126 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
127 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
128 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
131 gst_nuv_demux_base_init (gpointer klass)
133 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
135 gst_element_class_add_pad_template (element_class,
136 gst_static_pad_template_get (&audio_src_template));
138 gst_element_class_add_pad_template (element_class,
139 gst_static_pad_template_get (&video_src_template));
141 gst_element_class_add_pad_template (element_class,
142 gst_static_pad_template_get (&sink_template));
143 gst_element_class_set_details (element_class, &gst_nuv_demux_details);
147 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
149 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
150 GObjectClass *gobject_class = (GObjectClass *) klass;
152 GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
153 0, "Demuxer for NUV streams");
155 parent_class = g_type_class_peek_parent (klass);
157 gobject_class->finalize = gst_nuv_demux_finalize;
158 gstelement_class->change_state = gst_nuv_demux_change_state;
162 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
164 nuv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
166 gst_pad_set_activate_function (nuv->sinkpad, gst_nuv_demux_sink_activate);
168 gst_pad_set_activatepull_function (nuv->sinkpad,
169 gst_nuv_demux_sink_activate_pull);
171 gst_pad_set_activatepush_function (nuv->sinkpad,
172 gst_nuv_demux_sink_activate_push);
174 gst_pad_set_chain_function (nuv->sinkpad,
175 GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
177 gst_element_add_pad (GST_ELEMENT (nuv), nuv->sinkpad);
180 nuv->mpeg_buffer = NULL;
185 nuv->new_audio_segment = TRUE;
186 nuv->new_video_segment = TRUE;
188 gst_nuv_demux_reset (nuv);
192 gst_nuv_demux_finalize (GObject * object)
194 GstNuvDemux *nuv = GST_NUV_DEMUX (object);
196 if (nuv->mpeg_buffer != NULL) {
197 gst_buffer_unref (nuv->mpeg_buffer);
200 gst_nuv_demux_destoy_src_pad (nuv);
201 gst_nuv_demux_reset (nuv);
202 if (nuv->adapter != NULL) {
203 gst_object_unref (nuv->adapter);
205 G_OBJECT_CLASS (parent_class)->finalize (object);
212 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header ** h_ret)
214 GstBuffer *buffer = NULL;
215 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
217 if (res != GST_FLOW_OK)
220 nuv_header *h = g_new0 (nuv_header, 1);
222 memcpy (h->id, buffer->data, 12);
223 memcpy (h->version, buffer->data + 12, 5);
224 h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
225 h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
226 h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
227 h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
228 h->i_mode = GPOINTER_TO_INT (buffer->data[36]);
229 h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
230 h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
231 h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
232 h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
233 h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
234 h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
236 GST_DEBUG_OBJECT (nuv,
237 "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
238 h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
239 h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
240 h->i_keyframe_distance);
243 gst_buffer_unref (buffer);
248 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
250 GstFlowReturn res = gst_nuv_demux_header_load (nuv, &nuv->h);
252 if (res == GST_FLOW_OK)
253 nuv->state = GST_NUV_DEMUX_EXTRA_DATA;
261 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
263 GstFlowReturn res = GST_FLOW_OK;
264 GstBuffer *file_header = NULL;
266 res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
267 if (res != GST_FLOW_OK) {
270 if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
271 strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
272 nuv->state = GST_NUV_DEMUX_HEADER_DATA;
274 GST_DEBUG_OBJECT (nuv, "error parsing file header");
275 nuv->state = GST_NUV_DEMUX_INVALID_DATA;
276 res = GST_FLOW_ERROR;
279 if (file_header != NULL) {
280 gst_buffer_unref (file_header);
288 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header ** h_ret)
292 GstBuffer *buf = NULL;
294 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
296 if (res != GST_FLOW_OK) {
298 gst_buffer_unref (buf);
303 h = g_new0 (nuv_frame_header, 1);
306 h->i_type = GPOINTER_TO_INT (data[0]);
307 h->i_compression = GPOINTER_TO_INT (data[1]);
308 h->i_keyframe = GPOINTER_TO_INT (data[2]);
309 h->i_filters = GPOINTER_TO_INT (data[3]);
310 h->i_timecode = GST_READ_UINT32_LE (&data[4]);
311 h->i_length = GST_READ_UINT32_LE (&data[8]);
313 GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
315 h->i_compression ? h->i_compression : ' ',
316 h->i_keyframe ? h->i_keyframe : ' ',
317 h->i_filters, h->i_timecode, h->i_length);
320 gst_buffer_unref (buf);
325 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
326 nuv_extended_header ** h_ret)
329 GstBuffer *buff = NULL;
330 nuv_extended_header *h;
333 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
335 if (res != GST_FLOW_OK) {
337 gst_buffer_unref (buff);
342 h = g_new0 (nuv_extended_header, 1);
344 h->i_version = GST_READ_UINT32_LE (&data[0]);
345 h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
346 h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
347 h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
348 h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
349 h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
350 h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
351 h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
352 h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
353 h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
354 h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
355 h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
356 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
357 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
358 h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
359 h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
360 h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
362 GST_DEBUG_OBJECT (nuv,
363 "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
364 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
365 h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
366 h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
367 h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
368 h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
369 h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
370 h->i_keyframe_adjust_offset);
373 gst_buffer_unref (buff);
377 //TODO: create a function to control events and send to src pads
379 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
381 if (nuv->h->i_video_blocks != 0) {
382 GstCaps *video_caps = NULL;
385 gst_pad_new_from_static_template (&video_src_template, "video_src");
387 video_caps = gst_caps_new_simple ("video/x-divx",
388 "divxversion", G_TYPE_INT, 4,
389 "width", G_TYPE_INT, nuv->h->i_width,
390 "height", G_TYPE_INT, nuv->h->i_height,
391 "framerate", GST_TYPE_FRACTION, (gint) (nuv->h->d_fps * 1000.0f), 1000,
392 "format", GST_TYPE_FOURCC, nuv->eh->i_video_fcc,
393 "pixel-aspect-ratio", GST_TYPE_FRACTION,
394 (gint) (nuv->h->d_aspect * 1000.0f), 1000, NULL);
396 gst_pad_use_fixed_caps (nuv->src_video_pad);
397 gst_pad_set_caps (nuv->src_video_pad, video_caps);
398 gst_pad_set_active (nuv->src_video_pad, TRUE);
399 gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
401 gst_caps_unref (video_caps);
404 if (nuv->h->i_audio_blocks != 0) {
405 GstCaps *audio_caps = NULL;
408 gst_pad_new_from_static_template (&audio_src_template, "audio_src");
410 audio_caps = gst_caps_new_simple ("audio/mpeg",
411 "rate", G_TYPE_INT, nuv->eh->i_audio_sample_rate,
412 "format", GST_TYPE_FOURCC, nuv->eh->i_audio_fcc,
413 "channels", G_TYPE_INT, nuv->eh->i_audio_channels,
414 "mpegversion", G_TYPE_INT, nuv->eh->i_version, NULL);
416 gst_pad_use_fixed_caps (nuv->src_audio_pad);
417 gst_pad_set_caps (nuv->src_audio_pad, audio_caps);
418 gst_pad_set_active (nuv->src_audio_pad, TRUE);
419 gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
420 gst_caps_unref (audio_caps);
423 gst_element_no_more_pads (GST_ELEMENT (nuv));
427 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
429 GstFlowReturn ret = GST_FLOW_OK;
437 ret = gst_nuv_demux_frame_header_load (nuv, &nuv->fh);
438 if (ret != GST_FLOW_OK)
441 nuv->state = GST_NUV_DEMUX_MOVI;
445 //TODO: send a new segment event before the firts data
447 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
449 GstFlowReturn ret = GST_FLOW_OK;
450 GstBuffer *buf = NULL;
451 nuv_frame_header *h = nuv->fh;
453 if (h->i_type == 'R')
456 if (h->i_length > 0) {
457 ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
458 if (ret != GST_FLOW_OK)
461 GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND;
467 if (h->i_length == 0)
470 if ( !gst_pad_is_linked( nuv->src_video_pad ) )
473 if (nuv->new_video_segment) {
474 /* send new segment event*/
475 gst_pad_push_event (nuv->src_video_pad,
476 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
477 GST_CLOCK_TIME_NONE, 0));
478 nuv->new_video_segment = FALSE;
481 GST_BUFFER_SIZE (buf) = h->i_length;
482 gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_video_pad));
483 ret = gst_pad_push (nuv->src_video_pad, buf);
484 if (ret != GST_FLOW_OK) {
485 GST_WARNING_OBJECT (nuv, "error pushing on srcpad %s, is linked? = %d",
486 gst_pad_get_name (nuv->src_video_pad), gst_pad_is_linked (nuv->src_video_pad));
492 if (h->i_length == 0)
495 if ( !gst_pad_is_linked( nuv->src_audio_pad ) )
498 if (nuv->new_audio_segment) {
499 /* send new segment event*/
500 gst_pad_push_event (nuv->src_audio_pad,
501 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
502 GST_CLOCK_TIME_NONE, 0));
503 nuv->new_audio_segment = FALSE;
506 GST_BUFFER_SIZE (buf) = h->i_length;
507 gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_audio_pad));
508 ret = gst_pad_push (nuv->src_audio_pad, buf);
509 if (ret != GST_FLOW_OK) {
510 GST_WARNING_OBJECT (nuv, "Error pushing on srcpad %s, is linked? = %d",
511 gst_pad_get_name (nuv->src_audio_pad), gst_pad_is_linked (nuv->src_audio_pad));
517 switch (h->i_compression) {
519 if ( !gst_pad_is_linked( nuv->src_video_pad ) )
522 GST_DEBUG_OBJECT (nuv, "sending new video segment: %d", h->i_timecode);
523 gst_pad_push_event (nuv->src_video_pad,
524 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, h->i_timecode * GST_MSECOND,
525 GST_CLOCK_TIME_NONE, 0));
528 if ( !gst_pad_is_linked( nuv->src_audio_pad ) )
531 GST_DEBUG_OBJECT (nuv, "sending new audio segment: %d", h->i_timecode);
532 gst_pad_push_event (nuv->src_audio_pad,
533 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0,
534 GST_CLOCK_TIME_NONE, 0));
540 gst_buffer_unref (buf);
544 gst_buffer_unref (buf);
550 nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
557 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
559 GstFlowReturn ret = GST_FLOW_OK;
561 /* ffmpeg extra data */
563 gst_nuv_demux_read_bytes (nuv, nuv->mpeg_data_size, TRUE,
565 if (ret != GST_FLOW_OK) {
568 GST_BUFFER_SIZE (nuv->mpeg_buffer) = nuv->mpeg_data_size;
569 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
574 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
576 GstFlowReturn ret = GST_FLOW_OK;
581 ret = gst_nuv_demux_frame_header_load (nuv, &h);
582 if (ret != GST_FLOW_OK)
585 if (h->i_type != 'D') {
587 return GST_FLOW_ERROR;
590 if (h->i_length > 0) {
591 if (h->i_compression == 'F') {
592 nuv->state = GST_NUV_DEMUX_MPEG_DATA;
595 return GST_FLOW_ERROR;
598 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
607 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
609 GstFlowReturn ret = GST_FLOW_OK;
611 ret = gst_nuv_demux_extended_header_load (nuv, &nuv->eh);
612 if (ret != GST_FLOW_OK)
615 gst_nuv_demux_create_pads (nuv);
616 nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
621 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
623 GstBuffer *buf = NULL;
624 GstFlowReturn res = GST_FLOW_OK;
626 res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
627 if (res != GST_FLOW_OK) {
629 gst_buffer_unref (buf);
634 if (buf->data[0] == 'X') {
635 gst_buffer_unref (buf);
637 nuv_frame_header *h = NULL;
639 res = gst_nuv_demux_frame_header_load (nuv, &h);
640 if (res != GST_FLOW_OK)
643 if (h->i_length != 512) {
645 return GST_FLOW_ERROR;
649 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
651 nuv->state = GST_NUV_DEMUX_INVALID_DATA;
652 g_object_unref (buf);
653 GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
654 (_("incomplete NUV support")), ("incomplete NUV support"));
655 return GST_FLOW_ERROR;
661 gst_nuv_demux_play (GstPad * pad)
663 GstFlowReturn res = GST_FLOW_OK;
664 GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
666 switch (nuv->state) {
667 case GST_NUV_DEMUX_START:
668 res = gst_nuv_demux_stream_file_header (nuv);
669 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
672 if (nuv->state != GST_NUV_DEMUX_HEADER_DATA)
675 case GST_NUV_DEMUX_HEADER_DATA:
676 res = gst_nuv_demux_stream_header_data (nuv);
677 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
680 if (nuv->state != GST_NUV_DEMUX_EXTRA_DATA)
683 case GST_NUV_DEMUX_EXTRA_DATA:
684 res = gst_nuv_demux_stream_extra_data (nuv);
685 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
688 if (nuv->state != GST_NUV_DEMUX_MPEG_DATA)
691 case GST_NUV_DEMUX_MPEG_DATA:
692 res = gst_nuv_demux_stream_mpeg_data (nuv);
693 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
697 if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER)
700 case GST_NUV_DEMUX_EXTEND_HEADER:
701 res = gst_nuv_demux_stream_extend_header (nuv);
702 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
705 if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER_DATA)
708 case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
709 res = gst_nuv_demux_stream_extend_header_data (nuv);
710 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
714 if (nuv->state != GST_NUV_DEMUX_FRAME_HEADER)
717 case GST_NUV_DEMUX_FRAME_HEADER:
718 res = gst_nuv_demux_read_head_frame (nuv);
719 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
722 if (nuv->state != GST_NUV_DEMUX_MOVI)
725 case GST_NUV_DEMUX_MOVI:
726 res = gst_nuv_demux_stream_data (nuv);
727 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
731 case GST_NUV_DEMUX_INVALID_DATA:
735 g_assert_not_reached ();
738 GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->state,
739 gst_flow_get_name (res));
744 GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
745 gst_pad_pause_task (nuv->sinkpad);
746 if (GST_FLOW_IS_FATAL (res)) {
747 GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
748 (_("Internal data stream error.")),
749 ("streaming stopped, reason %s", gst_flow_get_name (res)));
751 gst_nuv_demux_send_eos (nuv);
757 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
759 gst_element_post_message (GST_ELEMENT (nuv),
760 gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
762 if (nuv->src_video_pad)
763 gst_pad_push_event (nuv->src_video_pad, gst_event_new_eos ());
764 if (nuv->src_audio_pad)
765 gst_pad_push_event (nuv->src_audio_pad, gst_event_new_eos ());
768 //TODO: try use "gst_pad_alloc_buffer" function
770 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
773 GstFlowReturn ret = GST_FLOW_OK;
776 *buffer = gst_buffer_new ();
780 if (nuv->mode == 0) {
781 ret = gst_pad_pull_range (nuv->sinkpad, nuv->offset, size, buffer);
782 if (ret == GST_FLOW_OK) {
787 } else if (ret == GST_FLOW_UNEXPECTED) {
789 gst_buffer_unref (buffer);
791 gst_nuv_demux_send_eos (nuv);
792 return GST_FLOW_WRONG_STATE;
795 if (gst_adapter_available (nuv->adapter) < size)
796 return GST_FLOW_ERROR_NO_DATA;
800 data = (guint8 *) gst_adapter_take (nuv->adapter, size);
801 *buffer = gst_buffer_new ();
802 gst_buffer_set_data (*buffer, data, size);
805 data = (guint8 *) gst_adapter_peek (nuv->adapter, size);
806 *buffer = gst_buffer_new ();
807 gst_buffer_set_data (*buffer, data, size);
814 gst_nuv_demux_sink_activate (GstPad * sinkpad)
817 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
819 if (gst_pad_check_pull_range (sinkpad)) {
820 res = gst_pad_activate_pull (sinkpad, TRUE);
824 gst_adapter_clear (nuv->adapter);
826 nuv->adapter = gst_adapter_new ();
828 res = gst_pad_activate_push (sinkpad, TRUE);
830 g_object_unref (nuv);
835 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
837 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
842 gst_adapter_clear (nuv->adapter);
843 g_object_unref (nuv->adapter);
846 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
848 gst_pad_stop_task (sinkpad);
850 gst_object_unref (nuv);
856 gst_nuv_demux_sink_activate_push (GstPad * pad, gboolean active)
858 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
861 GST_DEBUG_OBJECT (nuv, "activating push/chain function");
863 GST_DEBUG_OBJECT (nuv, "deactivating push/chain function");
866 gst_object_unref (nuv);
872 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
874 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
876 gst_adapter_push (nuv->adapter, buf);
878 gst_object_unref (nuv);
880 return gst_nuv_demux_play (pad);
884 gst_nuv_demux_loop (GstPad * pad)
886 gst_nuv_demux_play (pad);
890 gst_nuv_demux_reset (GstNuvDemux * nuv)
892 nuv->state = GST_NUV_DEMUX_START;
897 if (nuv->adapter != NULL)
898 gst_adapter_clear (nuv->adapter);
900 if (nuv->mpeg_buffer != NULL) {
901 gst_buffer_unref (nuv->mpeg_buffer);
902 nuv->mpeg_buffer = NULL;
916 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
918 if (nuv->src_video_pad) {
919 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
920 nuv->src_video_pad = NULL;
923 if (nuv->src_audio_pad) {
924 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
925 nuv->src_audio_pad = NULL;
929 static GstStateChangeReturn
930 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
932 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
934 switch (transition) {
935 case GST_STATE_CHANGE_READY_TO_PAUSED:
941 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
942 if (ret == GST_STATE_CHANGE_FAILURE)
945 switch (transition) {
946 case GST_STATE_CHANGE_PAUSED_TO_READY:
947 gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
948 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
959 plugin_init (GstPlugin * plugin)
962 setlocale (LC_ALL, "");
963 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
964 #endif /* ENABLE_NLS */
966 if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
967 GST_TYPE_NUV_DEMUX)) {
973 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
976 "Demuxes and muxes audio and video",
977 plugin_init, VERSION, "LGPL", "NuvDemux", "")