[svn r66] Refactoring some remoce_encoder functions, which actually needs to be just recorder; added channel-related recorder functions.
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 #define GETTEXT_PACKAGE "gst-plugins-nuvdemux-0.10.3"
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
65 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_EVENT);
67 static const GstElementDetails gst_nuv_demux_details =
68 GST_ELEMENT_DETAILS ("Nuv demuxer",
70 "Demultiplex a .nuv file into audio and video",
71 "Renato Araujo Oliveira Filho <renato.filho@indt.org.br>,"
72 "Rosfran Borges <rosfran.borges@indt.org.br>");
74 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_STATIC_CAPS ("video/x-nuv"));
79 static GstStaticPadTemplate audio_src_template =
80 GST_STATIC_PAD_TEMPLATE ("audio_src",
85 static GstStaticPadTemplate video_src_template =
86 GST_STATIC_PAD_TEMPLATE ("video_src",
91 /* NUV Demux indexes init/dispose callers */
92 static void gst_nuv_demux_index_init( nuv_demux_index **p_idx );
93 static void gst_nuv_demux_index_clean( nuv_demux_index **p_idx );
95 /* NUV Demux indexes manipulation functions */
96 static gint64 gst_nuv_demux_index_find_offset( GstNuvDemux *nuv, gint64 i_offset );
97 static void gst_nuv_demux_index_append( GstNuvDemux *nuv, gint64 i_time, gint64 i_offset );
98 static gint64 gst_nuv_demux_index_convert_time( GstNuvDemux *nuv, gint64 i_time );
100 /* NUV Demux plug-in time-line functions */
101 static void gst_nuv_demux_finalize (GObject * object);
102 static GstStateChangeReturn gst_nuv_demux_change_state (GstElement * element,
103 GstStateChange transition);
104 static void gst_nuv_demux_loop (GstPad * pad);
105 static GstFlowReturn gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf);
106 static GstFlowReturn gst_nuv_demux_play (GstPad * pad);
107 static gboolean gst_nuv_demux_sink_activate_pull (GstPad * sinkpad,
109 static gboolean gst_nuv_demux_sink_activate (GstPad * sinkpad);
110 static GstFlowReturn gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size,
111 gboolean move, GstBuffer ** buffer);
112 static void gst_nuv_demux_reset (GstNuvDemux * nuv);
113 static gboolean gst_nuv_demux_handle_sink_event (GstPad * sinkpad,
115 static void gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv);
116 static void gst_nuv_demux_send_eos (GstNuvDemux * nuv);
118 /* GObject methods */
119 GST_BOILERPLATE (GstNuvDemux, gst_nuv_demux, GstElement, GST_TYPE_ELEMENT);
121 #if G_BYTE_ORDER == G_BIG_ENDIAN
122 static inline gdouble
123 _gdouble_swap_le_be (gdouble * d)
132 u.i = GUINT64_SWAP_LE_BE (u.i);
136 #define READ_DOUBLE_FROM_LE(d) (_gdouble_swap_le_be((gdouble* ) d))
137 #else /* G_BYTE_ORDER != G_BIG_ENDIAN */
138 #define READ_DOUBLE_FROM_LE(d) *((gdouble* ) (d))
139 #endif /* G_BYTE_ORDER != G_BIG_ENDIAN */
142 gst_nuv_demux_base_init (gpointer klass)
144 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
146 gst_element_class_add_pad_template (element_class,
147 gst_static_pad_template_get (&audio_src_template));
149 gst_element_class_add_pad_template (element_class,
150 gst_static_pad_template_get (&video_src_template));
152 gst_element_class_add_pad_template (element_class,
153 gst_static_pad_template_get (&sink_template));
154 gst_element_class_set_details (element_class, &gst_nuv_demux_details);
158 gst_nuv_demux_class_init (GstNuvDemuxClass * klass)
160 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
161 GObjectClass *gobject_class = (GObjectClass *) klass;
163 GST_DEBUG_CATEGORY_INIT (nuvdemux_debug, "nuvdemux",
164 0, "Demuxer for NUV streams");
166 parent_class = g_type_class_peek_parent (klass);
168 gobject_class->finalize = gst_nuv_demux_finalize;
169 gstelement_class->change_state = gst_nuv_demux_change_state;
173 gst_nuv_demux_init (GstNuvDemux * nuv, GstNuvDemuxClass * nuv_class)
175 nuv->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
177 gst_pad_set_activate_function (nuv->sinkpad, gst_nuv_demux_sink_activate);
179 gst_pad_set_activatepull_function (nuv->sinkpad,
180 gst_nuv_demux_sink_activate_pull);
182 gst_pad_set_chain_function (nuv->sinkpad,
183 GST_DEBUG_FUNCPTR (gst_nuv_demux_chain));
185 gst_pad_set_event_function (nuv->sinkpad, gst_nuv_demux_handle_sink_event);
187 gst_element_add_pad (GST_ELEMENT (nuv), nuv->sinkpad);
189 gst_nuv_demux_index_init( &nuv->index_entries );
192 nuv->mpeg_buffer = NULL;
196 gst_nuv_demux_reset (nuv);
200 gst_nuv_demux_finalize (GObject * object)
202 GstNuvDemux *nuv = GST_NUV_DEMUX (object);
204 if (nuv->mpeg_buffer != NULL) {
205 gst_buffer_unref (nuv->mpeg_buffer);
208 if ( nuv->index_entries != NULL ) {
209 gst_nuv_demux_index_clean( &nuv->index_entries );
210 nuv->index_entries = NULL;
213 gst_nuv_demux_destoy_src_pad (nuv);
214 gst_nuv_demux_reset (nuv);
215 if (nuv->adapter != NULL) {
216 gst_object_unref (nuv->adapter);
218 G_OBJECT_CLASS (parent_class)->finalize (object);
222 /*****************************************************************************
223 * Indexes (timecode offset conversion) functions
224 *****************************************************************************/
227 gst_nuv_demux_index_init( nuv_demux_index **p_idx )
229 *p_idx = g_new0( nuv_demux_index, 1 );
231 (*p_idx)->i_idx_max = 0;
235 gst_nuv_demux_index_clean( nuv_demux_index **p_idx )
237 if ( *p_idx != NULL ) {
245 gst_nuv_demux_index_append( GstNuvDemux *nuv, gint64 i_time, gint64 i_offset )
247 nuv_demux_index *p_idx = nuv->index_entries;
249 //if ( p_idx == NULL )
252 /* Be sure to append new entry (we don't insert point) */
253 if( p_idx != NULL && p_idx->i_idx > 0 && p_idx->idx[p_idx->i_idx-1].i_time >= i_time )
257 if( p_idx->i_idx >= p_idx->i_idx_max )
259 if( p_idx->i_idx >= DEMUX_INDEX_SIZE_MAX )
261 /* Avoid too big index */
262 const gint64 i_length = p_idx->idx[p_idx->i_idx-1].i_time -
263 p_idx->idx[0].i_time;
264 const gint i_count = DEMUX_INDEX_SIZE_MAX/2;
267 /* We try to reduce the resolution of the index by a factor 2 */
268 for( i = 1, j = 1; i < p_idx->i_idx; i++ )
270 if( p_idx->idx[i].i_time < j * i_length / i_count )
273 p_idx->idx[j++] = p_idx->idx[i];
277 if( p_idx->i_idx > 3 * DEMUX_INDEX_SIZE_MAX / 4 )
279 /* We haven't created enough space
280 * (This method won't create a good index but work for sure) */
281 for( i = 0; i < p_idx->i_idx/2; i++ )
282 p_idx->idx[i] = p_idx->idx[2*i];
288 p_idx->i_idx_max += 1000;
289 //p_idx->idx = g_realloc( p_idx->idx,
290 //p_idx->i_idx_max*sizeof(nuv_demux_index_entry));
295 p_idx->idx[p_idx->i_idx].i_time = i_time;
296 p_idx->idx[p_idx->i_idx].i_offset = i_offset;
302 gst_nuv_demux_index_convert_time( GstNuvDemux *nuv, gint64 i_time )
304 nuv_demux_index *p_idx = nuv->index_entries;
306 g_return_val_if_fail( p_idx != NULL , i_time );
309 gint i_max = p_idx->i_idx-1;
312 if( p_idx->i_idx <= 0 )
315 /* Special border case */
316 if( i_time <= p_idx->idx[0].i_time )
317 return p_idx->idx[0].i_offset;
318 if( i_time >= p_idx->idx[i_max].i_time )
319 return p_idx->idx[i_max].i_offset;
326 if( i_max - i_min <= 1 )
329 i_med = (i_min+i_max)/2;
330 if( p_idx->idx[i_med].i_time < i_time )
332 else if( p_idx->idx[i_med].i_time > i_time )
335 return p_idx->idx[i_med].i_offset;
338 /* return nearest in time */
339 if( i_time - p_idx->idx[i_min].i_time < p_idx->idx[i_max].i_time - i_time )
340 return p_idx->idx[i_min].i_offset;
342 return p_idx->idx[i_max].i_offset;
346 gst_nuv_demux_index_find_offset( GstNuvDemux *nuv, gint64 i_offset )
348 nuv_demux_index *p_idx = nuv->index_entries;
350 g_return_val_if_fail( p_idx != NULL && p_idx->idx != NULL, i_offset );
353 gint i_max = p_idx->i_idx-1;
356 if( p_idx->i_idx <= 0 )
359 /* Special border case */
360 if( i_offset <= p_idx->idx[0].i_offset )
361 return p_idx->idx[0].i_offset;
362 if( i_offset == p_idx->idx[i_max].i_offset )
363 return p_idx->idx[i_max].i_offset;
364 if( i_offset > p_idx->idx[i_max].i_offset )
372 if ( i_max - i_min <= 1 )
375 i_med = (i_min+i_max)/2;
376 if( p_idx->idx[i_med].i_offset < i_offset )
378 else if( p_idx->idx[i_med].i_offset > i_offset )
381 return p_idx->idx[i_med].i_offset;
385 if( i_offset - p_idx->idx[i_min].i_offset < p_idx->idx[i_max].i_offset - i_offset )
386 return p_idx->idx[i_min].i_offset;
388 return p_idx->idx[i_max].i_offset;
391 /*****************************************************************************
393 *****************************************************************************/
396 gst_nuv_demux_handle_sink_event (GstPad * sinkpad, GstEvent * event)
398 gboolean res = FALSE;
400 switch (GST_EVENT_TYPE (event)) {
401 case GST_EVENT_NEWSEGMENT:
405 return gst_pad_event_default (sinkpad, event);
409 gst_event_unref (event);
417 gst_nuv_demux_header_load (GstNuvDemux * nuv, nuv_header ** h_ret)
419 GstBuffer *buffer = NULL;
420 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 72, TRUE, &buffer);
422 if (res != GST_FLOW_OK)
425 nuv_header *h = g_new0 (nuv_header, 1);
427 memcpy (h->id, buffer->data, 12);
428 memcpy (h->version, buffer->data + 12, 5);
429 h->i_width = GST_READ_UINT32_LE (&buffer->data[20]);
430 h->i_height = GST_READ_UINT32_LE (&buffer->data[24]);
431 h->i_width_desired = GST_READ_UINT32_LE (&buffer->data[28]);
432 h->i_height_desired = GST_READ_UINT32_LE (&buffer->data[32]);
433 h->i_mode = buffer->data[36];
434 h->d_aspect = READ_DOUBLE_FROM_LE (&buffer->data[40]);
435 h->d_fps = READ_DOUBLE_FROM_LE (&buffer->data[48]);
436 h->i_video_blocks = GST_READ_UINT32_LE (&buffer->data[56]);
437 h->i_audio_blocks = GST_READ_UINT32_LE (&buffer->data[60]);
438 h->i_text_blocks = GST_READ_UINT32_LE (&buffer->data[64]);
439 h->i_keyframe_distance = GST_READ_UINT32_LE (&buffer->data[68]);
441 GST_DEBUG_OBJECT (nuv,
442 "nuv: h=%s v=%s %dx%d a=%f fps=%f v=%d a=%d t=%d kfd=%d", h->id,
443 h->version, h->i_width, h->i_height, h->d_aspect, h->d_fps,
444 h->i_video_blocks, h->i_audio_blocks, h->i_text_blocks,
445 h->i_keyframe_distance);
448 gst_buffer_unref (buffer);
453 gst_nuv_demux_stream_header_data (GstNuvDemux * nuv)
455 GstFlowReturn res = gst_nuv_demux_header_load (nuv, &nuv->h);
457 if (res == GST_FLOW_OK)
458 nuv->state = GST_NUV_DEMUX_EXTRA_DATA;
466 gst_nuv_demux_stream_file_header (GstNuvDemux * nuv)
468 GstFlowReturn res = GST_FLOW_OK;
469 GstBuffer *file_header = NULL;
471 res = gst_nuv_demux_read_bytes (nuv, 12, FALSE, &file_header);
472 if (res != GST_FLOW_OK) {
475 if (strncmp ((gchar *) file_header->data, "MythTVVideo", 11) ||
476 strncmp ((gchar *) file_header->data, "NuppelVideo", 11)) {
477 nuv->state = GST_NUV_DEMUX_HEADER_DATA;
479 GST_DEBUG_OBJECT (nuv, "error parsing file header");
480 nuv->state = GST_NUV_DEMUX_INVALID_DATA;
481 res = GST_FLOW_ERROR;
484 if (file_header != NULL) {
485 gst_buffer_unref (file_header);
493 gst_nuv_demux_frame_header_load (GstNuvDemux * nuv, nuv_frame_header ** h_ret)
497 GstBuffer *buf = NULL;
499 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 12, TRUE, &buf);
501 if (res != GST_FLOW_OK) {
503 gst_buffer_unref (buf);
508 h = g_new0 (nuv_frame_header, 1);
512 h->i_compression = data[1];
513 h->i_keyframe = data[2];
514 h->i_filters = data[3];
516 h->i_timecode = GST_READ_UINT32_LE (&data[4]);
519 h->i_length = GST_READ_UINT32_LE (&data[8]);
520 GST_DEBUG_OBJECT (nuv, "frame hdr: t=%c c=%c k=%d f=0x%x timecode=%d l=%d",
522 h->i_compression ? h->i_compression : ' ',
523 h->i_keyframe ? h->i_keyframe : ' ',
524 h->i_filters, h->i_timecode, h->i_length);
527 gst_buffer_unref (buf);
532 gst_nuv_demux_extended_header_load (GstNuvDemux * nuv,
533 nuv_extended_header ** h_ret)
536 GstBuffer *buff = NULL;
537 nuv_extended_header *h;
540 GstFlowReturn res = gst_nuv_demux_read_bytes (nuv, 512, TRUE, &buff);
542 if (res != GST_FLOW_OK) {
544 gst_buffer_unref (buff);
549 h = g_new0 (nuv_extended_header, 1);
551 h->i_version = GST_READ_UINT32_LE (&data[0]);
552 h->i_video_fcc = GST_MAKE_FOURCC (data[4], data[5], data[6], data[7]);
553 h->i_audio_fcc = GST_MAKE_FOURCC (data[8], data[9], data[10], data[11]);
554 h->i_audio_sample_rate = GST_READ_UINT32_LE (&data[12]);
555 h->i_audio_bits_per_sample = GST_READ_UINT32_LE (&data[16]);
556 h->i_audio_channels = GST_READ_UINT32_LE (&data[20]);
557 h->i_audio_compression_ratio = GST_READ_UINT32_LE (&data[24]);
558 h->i_audio_quality = GST_READ_UINT32_LE (&data[28]);
559 h->i_rtjpeg_quality = GST_READ_UINT32_LE (&data[32]);
560 h->i_rtjpeg_luma_filter = GST_READ_UINT32_LE (&data[36]);
561 h->i_rtjpeg_chroma_filter = GST_READ_UINT32_LE (&data[40]);
562 h->i_lavc_bitrate = GST_READ_UINT32_LE (&data[44]);
563 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[48]);
564 h->i_lavc_qmin = GST_READ_UINT32_LE (&data[52]);
565 h->i_lavc_maxqdiff = GST_READ_UINT32_LE (&data[56]);
566 h->i_seekable_offset = GST_READ_UINT64_LE (&data[60]);
567 h->i_keyframe_adjust_offset = GST_READ_UINT64_LE (&data[68]);
569 GST_DEBUG_OBJECT (nuv,
570 "ex hdr: v=%d vffc=%4.4s afcc=%4.4s %dHz %dbits ach=%d acr=%d aq=%d"
571 "rtjpeg q=%d lf=%d lc=%d lavc br=%d qmin=%d qmax=%d maxqdiff=%d seekableoff=%lld keyfao=%lld",
572 h->i_version, (gchar *) & h->i_video_fcc, (gchar *) & h->i_audio_fcc,
573 h->i_audio_sample_rate, h->i_audio_bits_per_sample, h->i_audio_channels,
574 h->i_audio_compression_ratio, h->i_audio_quality, h->i_rtjpeg_quality,
575 h->i_rtjpeg_luma_filter, h->i_rtjpeg_chroma_filter, h->i_lavc_bitrate,
576 h->i_lavc_qmin, h->i_lavc_qmax, h->i_lavc_maxqdiff, h->i_seekable_offset,
577 h->i_keyframe_adjust_offset);
580 gst_buffer_unref (buff);
585 gst_nuv_demux_handle_src_event (GstPad * pad, GstEvent * event)
587 gst_event_unref (event);
593 gst_nuv_demux_create_pads (GstNuvDemux * nuv)
595 if (nuv->h->i_video_blocks != 0) {
596 GstCaps *video_caps = NULL;
599 gst_pad_new_from_static_template (&video_src_template, "video_src");
601 video_caps = gst_caps_new_simple ("video/x-divx",
602 "divxversion", G_TYPE_INT, 4,
603 "width", G_TYPE_INT, nuv->h->i_width,
604 "height", G_TYPE_INT, nuv->h->i_height,
605 "framerate", GST_TYPE_FRACTION, (gint) (nuv->h->d_fps * 1000.0f), 1000,
606 "pixel-aspect-ratio", GST_TYPE_FRACTION,
607 (gint) (nuv->h->d_aspect * 1000.0f), 1000, NULL);
609 gst_pad_use_fixed_caps (nuv->src_video_pad);
610 gst_pad_set_active (nuv->src_video_pad, TRUE);
611 gst_pad_set_caps (nuv->src_video_pad, video_caps);
613 gst_pad_set_event_function (nuv->src_video_pad,
614 gst_nuv_demux_handle_src_event);
615 gst_pad_set_active (nuv->src_video_pad, TRUE);
616 gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
618 gst_caps_unref (video_caps);
621 if (nuv->h->i_audio_blocks != 0) {
622 GstCaps *audio_caps = NULL;
625 gst_pad_new_from_static_template (&audio_src_template, "audio_src");
627 audio_caps = gst_caps_new_simple ("audio/mpeg",
628 "rate", G_TYPE_INT, nuv->eh->i_audio_sample_rate,
629 "format", GST_TYPE_FOURCC, nuv->eh->i_audio_fcc,
630 "channels", G_TYPE_INT, nuv->eh->i_audio_channels,
631 "mpegversion", G_TYPE_INT, nuv->eh->i_version, NULL);
633 gst_pad_use_fixed_caps (nuv->src_audio_pad);
634 gst_pad_set_active (nuv->src_audio_pad, TRUE);
635 gst_pad_set_caps (nuv->src_audio_pad, audio_caps);
636 gst_pad_set_active (nuv->src_audio_pad, TRUE);
637 gst_element_add_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
639 gst_pad_set_event_function (nuv->src_audio_pad,
640 gst_nuv_demux_handle_src_event);
642 gst_caps_unref (audio_caps);
645 gst_element_no_more_pads (GST_ELEMENT (nuv));
649 gst_nuv_demux_read_head_frame (GstNuvDemux * nuv)
651 GstFlowReturn ret = GST_FLOW_OK;
653 ret = gst_nuv_demux_frame_header_load (nuv, &nuv->fh);
654 if (ret != GST_FLOW_OK)
657 nuv->state = GST_NUV_DEMUX_MOVI;
662 gst_nuv_demux_stream_data (GstNuvDemux * nuv)
664 GstFlowReturn ret = GST_FLOW_OK;
665 GstBuffer *buf = NULL;
666 nuv_frame_header *h = nuv->fh;
668 if (h->i_type == 'R')
672 GST_DEBUG_OBJECT (nuv, "starting append index");
673 /* append the frame's header timecode field, and the actual offset */
674 gst_nuv_demux_index_append( nuv, h->i_timecode, nuv->offset );
676 if (h->i_length > 0) {
677 ret = gst_nuv_demux_read_bytes (nuv, h->i_length, TRUE, &buf);
678 if (ret != GST_FLOW_OK)
681 /* search for a valid timecode in the indexes list (find the nearest valid timecode) */
682 if ( h->i_timecode < 0 ) {
683 /* convert this actual timecode to a valid index in the timecode's table */
684 gint64 pos = gst_nuv_demux_index_convert_time( nuv, h->i_timecode );
686 /* find the nearest empty frame slot */
687 gint64 near_off = gst_nuv_demux_index_find_offset( nuv, pos );
689 /* just get the timecode from the timecode's table */
690 GST_BUFFER_TIMESTAMP (buf) = nuv->index_entries->idx[near_off].i_time * GST_MSECOND;
692 GST_BUFFER_TIMESTAMP (buf) = h->i_timecode * GST_MSECOND;
696 GST_DEBUG_OBJECT (nuv, "index appended");
701 if (h->i_length == 0)
704 GST_BUFFER_OFFSET (buf) = nuv->video_offset;
705 gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_video_pad));
706 ret = gst_pad_push (nuv->src_video_pad, buf);
712 if (h->i_length == 0)
715 GST_BUFFER_OFFSET (buf) = nuv->audio_offset;
716 gst_buffer_set_caps (buf, GST_PAD_CAPS (nuv->src_audio_pad));
717 ret = gst_pad_push (nuv->src_audio_pad, buf);
723 switch (h->i_compression) {
725 gst_pad_push_event (nuv->src_video_pad,
726 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1,
730 gst_pad_push_event (nuv->src_audio_pad,
731 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0));
739 gst_buffer_unref (buf);
744 GST_DEBUG_OBJECT (nuv, "data sended");
747 nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
754 gst_nuv_demux_stream_mpeg_data (GstNuvDemux * nuv)
756 GstFlowReturn ret = GST_FLOW_OK;
758 /* ffmpeg extra data */
760 gst_nuv_demux_read_bytes (nuv, nuv->mpeg_data_size, TRUE,
762 if (ret != GST_FLOW_OK) {
763 return GST_FLOW_ERROR;
765 GST_BUFFER_SIZE (nuv->mpeg_buffer) = nuv->mpeg_data_size;
766 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
771 gst_nuv_demux_stream_extra_data (GstNuvDemux * nuv)
773 GstFlowReturn ret = GST_FLOW_OK;
778 ret = gst_nuv_demux_frame_header_load (nuv, &h);
779 if (ret != GST_FLOW_OK)
782 if (h->i_type != 'D') {
784 return GST_FLOW_ERROR;
787 if (h->i_length > 0) {
788 if (h->i_compression == 'F') {
789 nuv->state = GST_NUV_DEMUX_MPEG_DATA;
792 return GST_FLOW_ERROR;
795 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER;
804 gst_nuv_demux_stream_extend_header_data (GstNuvDemux * nuv)
806 GstFlowReturn ret = GST_FLOW_OK;
808 ret = gst_nuv_demux_extended_header_load (nuv, &nuv->eh);
809 if (ret != GST_FLOW_OK)
812 gst_nuv_demux_create_pads (nuv);
813 nuv->state = GST_NUV_DEMUX_FRAME_HEADER;
818 gst_nuv_demux_stream_extend_header (GstNuvDemux * nuv)
820 GstBuffer *buf = NULL;
821 GstFlowReturn res = GST_FLOW_OK;
823 res = gst_nuv_demux_read_bytes (nuv, 1, FALSE, &buf);
824 if (res != GST_FLOW_OK) {
826 gst_buffer_unref (buf);
831 if (buf->data[0] == 'X') {
832 gst_buffer_unref (buf);
834 nuv_frame_header *h = NULL;
836 res = gst_nuv_demux_frame_header_load (nuv, &h);
837 if (res != GST_FLOW_OK)
840 if (h->i_length != 512) {
842 return GST_FLOW_ERROR;
846 nuv->state = GST_NUV_DEMUX_EXTEND_HEADER_DATA;
848 nuv->state = GST_NUV_DEMUX_INVALID_DATA;
849 g_object_unref (buf);
850 GST_ELEMENT_WARNING (nuv, STREAM, FAILED,
851 (_("incomplete NUV support")), ("incomplete NUV support"));
852 return GST_FLOW_ERROR;
858 gst_nuv_demux_play (GstPad * pad)
860 GstFlowReturn res = GST_FLOW_OK;
861 GstNuvDemux *nuv = GST_NUV_DEMUX (GST_PAD_PARENT (pad));
863 switch (nuv->state) {
864 case GST_NUV_DEMUX_START:
865 res = gst_nuv_demux_stream_file_header (nuv);
866 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
869 if (nuv->state != GST_NUV_DEMUX_HEADER_DATA)
872 case GST_NUV_DEMUX_HEADER_DATA:
873 res = gst_nuv_demux_stream_header_data (nuv);
874 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
877 if (nuv->state != GST_NUV_DEMUX_EXTRA_DATA)
880 case GST_NUV_DEMUX_EXTRA_DATA:
881 res = gst_nuv_demux_stream_extra_data (nuv);
882 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
885 if (nuv->state != GST_NUV_DEMUX_MPEG_DATA)
888 case GST_NUV_DEMUX_MPEG_DATA:
889 res = gst_nuv_demux_stream_mpeg_data (nuv);
890 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
894 if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER)
897 case GST_NUV_DEMUX_EXTEND_HEADER:
898 res = gst_nuv_demux_stream_extend_header (nuv);
899 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
902 if (nuv->state != GST_NUV_DEMUX_EXTEND_HEADER_DATA)
905 case GST_NUV_DEMUX_EXTEND_HEADER_DATA:
906 res = gst_nuv_demux_stream_extend_header_data (nuv);
907 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
911 if (nuv->state != GST_NUV_DEMUX_FRAME_HEADER)
914 case GST_NUV_DEMUX_FRAME_HEADER:
915 res = gst_nuv_demux_read_head_frame (nuv);
916 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
919 if (nuv->state != GST_NUV_DEMUX_MOVI)
922 case GST_NUV_DEMUX_MOVI:
923 res = gst_nuv_demux_stream_data (nuv);
924 if ((res != GST_FLOW_OK) && (res != GST_FLOW_ERROR_NO_DATA)) {
928 case GST_NUV_DEMUX_INVALID_DATA:
932 g_assert_not_reached ();
935 GST_DEBUG_OBJECT (nuv, "state: %d res:%s", nuv->state,
936 gst_flow_get_name (res));
941 GST_LOG_OBJECT (nuv, "pausing task, reason %s", gst_flow_get_name (res));
942 gst_pad_pause_task (nuv->sinkpad);
943 if (GST_FLOW_IS_FATAL (res)) {
944 GST_ELEMENT_ERROR (nuv, STREAM, FAILED,
945 (_("Internal data stream error.")),
946 ("streaming stopped, reason %s", gst_flow_get_name (res)));
948 gst_nuv_demux_send_eos (nuv);
954 gst_nuv_demux_send_eos (GstNuvDemux * nuv)
956 gst_element_post_message (GST_ELEMENT (nuv),
957 gst_message_new_segment_done (GST_OBJECT (nuv), GST_FORMAT_TIME, -1));
959 if (nuv->src_video_pad)
960 gst_pad_push_event (nuv->src_video_pad, gst_event_new_eos ());
961 if (nuv->src_audio_pad)
962 gst_pad_push_event (nuv->src_audio_pad, gst_event_new_eos ());
966 gst_nuv_demux_read_bytes (GstNuvDemux * nuv, guint64 size, gboolean move,
969 GstFlowReturn ret = GST_FLOW_OK;
972 *buffer = gst_buffer_new ();
976 if (nuv->mode == 0) {
977 ret = gst_pad_pull_range (nuv->sinkpad, nuv->offset, size, buffer);
978 if (ret == GST_FLOW_OK) {
983 } else if (ret == GST_FLOW_UNEXPECTED) {
984 gst_nuv_demux_send_eos (nuv);
985 return GST_FLOW_WRONG_STATE;
988 if (gst_adapter_available (nuv->adapter) < size)
989 return GST_FLOW_ERROR_NO_DATA;
992 *buffer = gst_adapter_take_buffer (nuv->adapter, size);
996 data = (guint8 *) gst_adapter_peek (nuv->adapter, size);
997 *buffer = gst_buffer_new ();
998 gst_buffer_set_data (*buffer, data, size);
1005 gst_nuv_demux_sink_activate (GstPad * sinkpad)
1007 gboolean res = TRUE;
1008 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1010 if (gst_pad_check_pull_range (sinkpad)) {
1012 nuv->adapter = NULL;
1013 res = gst_pad_activate_pull (sinkpad, TRUE);
1014 GST_DEBUG_OBJECT (nuv, "starting PULL mode");
1017 nuv->adapter = gst_adapter_new ();
1018 res = gst_pad_activate_push (sinkpad, TRUE);
1019 GST_DEBUG_OBJECT (nuv, "starting PUSH mode");
1022 g_object_unref (nuv);
1027 gst_nuv_demux_sink_activate_pull (GstPad * sinkpad, gboolean active)
1029 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (sinkpad));
1032 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_nuv_demux_loop, sinkpad);
1034 gst_pad_stop_task (sinkpad);
1036 gst_object_unref (nuv);
1041 static GstFlowReturn
1042 gst_nuv_demux_chain (GstPad * pad, GstBuffer * buf)
1044 GstNuvDemux *nuv = GST_NUV_DEMUX (gst_pad_get_parent (pad));
1046 gst_adapter_push (nuv->adapter, buf);
1048 GST_DEBUG_OBJECT (nuv, "PUSH adapter %d", gst_adapter_available (nuv->adapter));
1050 return gst_nuv_demux_play (pad);
1054 gst_nuv_demux_loop (GstPad * pad)
1056 gst_nuv_demux_play (pad);
1060 gst_nuv_demux_reset (GstNuvDemux * nuv)
1062 nuv->state = GST_NUV_DEMUX_START;
1065 nuv->video_offset = 0;
1066 nuv->audio_offset = 0;
1068 if (nuv->adapter != NULL)
1069 gst_adapter_clear (nuv->adapter);
1071 if (nuv->mpeg_buffer != NULL) {
1072 gst_buffer_unref (nuv->mpeg_buffer);
1073 nuv->mpeg_buffer = NULL;
1087 gst_nuv_demux_destoy_src_pad (GstNuvDemux * nuv)
1089 if (nuv->src_video_pad) {
1090 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_video_pad);
1091 nuv->src_video_pad = NULL;
1094 if (nuv->src_audio_pad) {
1095 gst_element_remove_pad (GST_ELEMENT (nuv), nuv->src_audio_pad);
1096 nuv->src_audio_pad = NULL;
1100 static GstStateChangeReturn
1101 gst_nuv_demux_change_state (GstElement * element, GstStateChange transition)
1103 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1105 switch (transition) {
1106 case GST_STATE_CHANGE_READY_TO_PAUSED:
1112 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1113 if (ret == GST_STATE_CHANGE_FAILURE)
1116 switch (transition) {
1117 case GST_STATE_CHANGE_PAUSED_TO_READY:
1118 //gst_nuv_demux_destoy_src_pad (GST_NUV_DEMUX (element));
1119 gst_nuv_demux_reset (GST_NUV_DEMUX (element));
1130 plugin_init (GstPlugin * plugin)
1133 setlocale (LC_ALL, "");
1134 #endif /* ENABLE_NLS */
1136 if (!gst_element_register (plugin, "nuvdemux", GST_RANK_SECONDARY,
1137 GST_TYPE_NUV_DEMUX)) {
1143 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1146 "Demuxes and muxes audio and video",
1147 plugin_init, VERSION, "LGPL", "NuvDemux", "")