static GstFlowReturn gst_gsmdec_parse (GstAudioDecoder * dec, GstAdapter * adapter, gint * offset, gint * length) { GstGSMDec *gsmdec = GST_GSMDEC (dec); guint size; size = gst_adapter_available (adapter); /* if input format is TIME each buffer should be self-contained and * the data is presumably packetised, and we should start with a clean * slate/state at the beginning of each buffer (for wav49 case) */ if (dec->input_segment.format == GST_FORMAT_TIME) { *offset = 0; *length = size; gsmdec->needed = 33; return GST_FLOW_OK; } g_return_val_if_fail (size > 0, GST_FLOW_ERROR); if (size < gsmdec->needed) return GST_FLOW_EOS; *offset = 0; *length = gsmdec->needed; /* WAV49 requires alternating 33 and 32 bytes of input */ if (gsmdec->use_wav49) { gsmdec->needed = (gsmdec->needed == 33 ? 32 : 33); } return GST_FLOW_OK; }
static gboolean gst_gsmdec_sink_setcaps (GstPad * pad, GstCaps * caps) { GstGSMDec *gsmdec; GstCaps *srccaps; GstStructure *s; gboolean ret = FALSE; gsmdec = GST_GSMDEC (gst_pad_get_parent (pad)); s = gst_caps_get_structure (caps, 0); if (s == NULL) goto wrong_caps; /* figure out if we deal with plain or MSGSM */ if (gst_structure_has_name (s, "audio/x-gsm")) gsmdec->use_wav49 = 0; else if (gst_structure_has_name (s, "audio/ms-gsm")) gsmdec->use_wav49 = 1; else goto wrong_caps; if (!gst_structure_get_int (s, "rate", &gsmdec->rate)) { GST_WARNING_OBJECT (gsmdec, "missing sample rate parameter from sink caps"); goto beach; } /* MSGSM needs different framing */ gsm_option (gsmdec->state, GSM_OPT_WAV49, &gsmdec->use_wav49); gsmdec->duration = gst_util_uint64_scale (ENCODED_SAMPLES, GST_SECOND, gsmdec->rate); /* Setting up src caps based on the input sample rate. */ srccaps = gst_caps_new_simple ("audio/x-raw-int", "endianness", G_TYPE_INT, G_BYTE_ORDER, "signed", G_TYPE_BOOLEAN, TRUE, "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, "rate", G_TYPE_INT, gsmdec->rate, "channels", G_TYPE_INT, 1, NULL); ret = gst_pad_set_caps (gsmdec->srcpad, srccaps); gst_caps_unref (srccaps); gst_object_unref (gsmdec); return ret; /* ERRORS */ wrong_caps: GST_ERROR_OBJECT (gsmdec, "invalid caps received"); beach: gst_object_unref (gsmdec); return ret; }
static GstFlowReturn gst_gsmdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer) { GstGSMDec *gsmdec; gsm_signal *out_data; gsm_byte *data; GstFlowReturn ret = GST_FLOW_OK; GstBuffer *outbuf; GstMapInfo map, omap; gsize outsize; guint frames, i, errors = 0; /* no fancy draining */ if (G_UNLIKELY (!buffer)) return GST_FLOW_OK; gsmdec = GST_GSMDEC (dec); gst_buffer_map (buffer, &map, GST_MAP_READ); frames = gst_gsmdec_get_frame_count (gsmdec, map.size); /* always the same amount of output samples (20ms worth per frame) */ outsize = ENCODED_SAMPLES * frames * sizeof (gsm_signal); outbuf = gst_buffer_new_and_alloc (outsize); gst_buffer_map (outbuf, &omap, GST_MAP_WRITE); out_data = (gsm_signal *) omap.data; data = (gsm_byte *) map.data; for (i = 0; i < frames; ++i) { /* now encode frame into the output buffer */ if (gsm_decode (gsmdec->state, data, out_data) < 0) { /* invalid frame */ GST_AUDIO_DECODER_ERROR (gsmdec, 1, STREAM, DECODE, (NULL), ("tried to decode an invalid frame"), ret); memset (out_data, 0, ENCODED_SAMPLES * sizeof (gsm_signal)); ++errors; } out_data += ENCODED_SAMPLES; data += gsmdec->needed; if (gsmdec->use_wav49) gsmdec->needed = (gsmdec->needed == 33 ? 32 : 33); } gst_buffer_unmap (outbuf, &omap); gst_buffer_unmap (buffer, &map); if (errors == frames) { gst_buffer_unref (outbuf); outbuf = NULL; } gst_audio_decoder_finish_frame (dec, outbuf, 1); return ret; }
static gboolean gst_gsmdec_stop (GstAudioDecoder * dec) { GstGSMDec *gsmdec = GST_GSMDEC (dec); GST_DEBUG_OBJECT (dec, "stop"); gsm_destroy (gsmdec->state); return TRUE; }
static gboolean gst_gsmdec_start (GstAudioDecoder * dec) { GstGSMDec *gsmdec = GST_GSMDEC (dec); GST_DEBUG_OBJECT (dec, "start"); gsmdec->state = gsm_create (); return TRUE; }
static void gst_gsmdec_finalize (GObject * object) { GstGSMDec *gsmdec; gsmdec = GST_GSMDEC (object); g_object_unref (gsmdec->adapter); gsm_destroy (gsmdec->state); G_OBJECT_CLASS (parent_class)->finalize (object); }
static gboolean gst_gsmdec_set_format (GstAudioDecoder * dec, GstCaps * caps) { GstGSMDec *gsmdec; GstStructure *s; gboolean ret = FALSE; gint rate; GstAudioInfo info; gsmdec = GST_GSMDEC (dec); s = gst_caps_get_structure (caps, 0); if (s == NULL) goto wrong_caps; /* figure out if we deal with plain or MSGSM */ if (gst_structure_has_name (s, "audio/x-gsm")) gsmdec->use_wav49 = 0; else if (gst_structure_has_name (s, "audio/ms-gsm")) gsmdec->use_wav49 = 1; else goto wrong_caps; gsmdec->needed = 33; if (!gst_structure_get_int (s, "rate", &rate)) { GST_WARNING_OBJECT (gsmdec, "missing sample rate parameter from sink caps"); goto beach; } /* MSGSM needs different framing */ gsm_option (gsmdec->state, GSM_OPT_WAV49, &gsmdec->use_wav49); /* Setting up src caps based on the input sample rate. */ gst_audio_info_init (&info); gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, 1, NULL); ret = gst_audio_decoder_set_output_format (dec, &info); return ret; /* ERRORS */ wrong_caps: GST_ERROR_OBJECT (gsmdec, "invalid caps received"); beach: return ret; }
static gboolean gst_gsmdec_sink_event (GstPad * pad, GstEvent * event) { gboolean res; GstGSMDec *gsmdec; gsmdec = GST_GSMDEC (gst_pad_get_parent (pad)); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_FLUSH_START: res = gst_pad_push_event (gsmdec->srcpad, event); break; case GST_EVENT_FLUSH_STOP: gst_segment_init (&gsmdec->segment, GST_FORMAT_UNDEFINED); res = gst_pad_push_event (gsmdec->srcpad, event); break; case GST_EVENT_NEWSEGMENT: { gboolean update; GstFormat format; gdouble rate, arate; gint64 start, stop, time; gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format, &start, &stop, &time); /* now configure the values */ gst_segment_set_newsegment_full (&gsmdec->segment, update, rate, arate, format, start, stop, time); /* and forward */ res = gst_pad_push_event (gsmdec->srcpad, event); break; } case GST_EVENT_EOS: default: res = gst_pad_push_event (gsmdec->srcpad, event); break; } gst_object_unref (gsmdec); return res; }
static GstFlowReturn gst_gsmdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer) { GstGSMDec *gsmdec; gsm_byte *data; GstFlowReturn ret = GST_FLOW_OK; GstBuffer *outbuf; GstMapInfo map, omap; /* no fancy draining */ if (G_UNLIKELY (!buffer)) return GST_FLOW_OK; gsmdec = GST_GSMDEC (dec); /* always the same amount of output samples */ outbuf = gst_buffer_new_and_alloc (ENCODED_SAMPLES * sizeof (gsm_signal)); /* now encode frame into the output buffer */ gst_buffer_map (buffer, &map, GST_MAP_READ); gst_buffer_map (outbuf, &omap, GST_MAP_WRITE); data = (gsm_byte *) map.data; if (gsm_decode (gsmdec->state, data, (gsm_signal *) omap.data) < 0) { /* invalid frame */ GST_AUDIO_DECODER_ERROR (gsmdec, 1, STREAM, DECODE, (NULL), ("tried to decode an invalid frame"), ret); gst_buffer_unmap (outbuf, &omap); gst_buffer_unref (outbuf); outbuf = NULL; } else { gst_buffer_unmap (outbuf, &omap); } gst_buffer_unmap (buffer, &map); gst_audio_decoder_finish_frame (dec, outbuf, 1); return ret; }
static GstFlowReturn gst_gsmdec_parse (GstAudioDecoder * dec, GstAdapter * adapter, gint * offset, gint * length) { GstGSMDec *gsmdec = GST_GSMDEC (dec); guint size; size = gst_adapter_available (adapter); g_return_val_if_fail (size > 0, GST_FLOW_ERROR); /* WAV49 requires alternating 33 and 32 bytes of input */ if (gsmdec->use_wav49) { gsmdec->needed = (gsmdec->needed == 33 ? 32 : 33); } if (size < gsmdec->needed) return GST_FLOW_EOS; *offset = 0; *length = gsmdec->needed; return GST_FLOW_OK; }
static GstFlowReturn gst_gsmdec_chain (GstPad * pad, GstBuffer * buf) { GstGSMDec *gsmdec; gsm_byte *data; GstFlowReturn ret = GST_FLOW_OK; GstClockTime timestamp; gint needed; gsmdec = GST_GSMDEC (gst_pad_get_parent (pad)); timestamp = GST_BUFFER_TIMESTAMP (buf); if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT)) { gst_adapter_clear (gsmdec->adapter); gsmdec->next_ts = GST_CLOCK_TIME_NONE; /* FIXME, do some good offset */ gsmdec->next_of = 0; } gst_adapter_push (gsmdec->adapter, buf); needed = 33; /* do we have enough bytes to read a frame */ while (gst_adapter_available (gsmdec->adapter) >= needed) { GstBuffer *outbuf; /* always the same amount of output samples */ outbuf = gst_buffer_new_and_alloc (ENCODED_SAMPLES * sizeof (gsm_signal)); /* If we are not given any timestamp, interpolate from last seen * timestamp (if any). */ if (timestamp == GST_CLOCK_TIME_NONE) timestamp = gsmdec->next_ts; GST_BUFFER_TIMESTAMP (outbuf) = timestamp; /* interpolate in the next run */ if (timestamp != GST_CLOCK_TIME_NONE) gsmdec->next_ts = timestamp + gsmdec->duration; timestamp = GST_CLOCK_TIME_NONE; GST_BUFFER_DURATION (outbuf) = gsmdec->duration; GST_BUFFER_OFFSET (outbuf) = gsmdec->next_of; if (gsmdec->next_of != -1) gsmdec->next_of += ENCODED_SAMPLES; GST_BUFFER_OFFSET_END (outbuf) = gsmdec->next_of; gst_buffer_set_caps (outbuf, GST_PAD_CAPS (gsmdec->srcpad)); /* now encode frame into the output buffer */ data = (gsm_byte *) gst_adapter_peek (gsmdec->adapter, needed); if (gsm_decode (gsmdec->state, data, (gsm_signal *) GST_BUFFER_DATA (outbuf)) < 0) { /* invalid frame */ GST_WARNING_OBJECT (gsmdec, "tried to decode an invalid frame, skipping"); } gst_adapter_flush (gsmdec->adapter, needed); /* WAV49 requires alternating 33 and 32 bytes of input */ if (gsmdec->use_wav49) needed = (needed == 33 ? 32 : 33); GST_DEBUG_OBJECT (gsmdec, "Pushing buffer of size %d ts %" GST_TIME_FORMAT, GST_BUFFER_SIZE (outbuf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf))); /* push */ ret = gst_pad_push (gsmdec->srcpad, outbuf); } gst_object_unref (gsmdec); return ret; }