static void gst_amc_audio_dec_init (GstAmcAudioDec * self) { gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (self), TRUE); gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (self), TRUE); g_mutex_init (&self->drain_lock); g_cond_init (&self->drain_cond); }
static void gst_ffmpegauddec_init (GstFFMpegAudDec * ffmpegdec) { GstFFMpegAudDecClass *klass = (GstFFMpegAudDecClass *) G_OBJECT_GET_CLASS (ffmpegdec); /* some ffmpeg data */ ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin); ffmpegdec->opened = FALSE; gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (ffmpegdec), TRUE); gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (ffmpegdec), TRUE); }
static void gst_ffmpegauddec_drain (GstFFMpegAudDec * ffmpegdec) { GstFFMpegAudDecClass *oclass; oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec)); if (oclass->in_plugin->capabilities & CODEC_CAP_DELAY) { gint have_data, len; GST_LOG_OBJECT (ffmpegdec, "codec has delay capabilities, calling until libav has drained everything"); do { GstFlowReturn ret; len = gst_ffmpegauddec_frame (ffmpegdec, NULL, 0, &have_data, &ret); } while (len >= 0 && have_data == 1); avcodec_flush_buffers (ffmpegdec->context); } if (ffmpegdec->outbuf) gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (ffmpegdec), ffmpegdec->outbuf, 1); ffmpegdec->outbuf = NULL; }
static gboolean gst_dtsdec_renegotiate (GstDtsDec * dts) { gint channels; gboolean result = FALSE; GstAudioChannelPosition from[6], to[6]; GstAudioInfo info; channels = gst_dtsdec_channels (dts->using_channels, from); if (!channels) goto done; GST_INFO_OBJECT (dts, "dtsdec renegotiate, channels=%d, rate=%d", channels, dts->sample_rate); memcpy (to, from, sizeof (GstAudioChannelPosition) * channels); gst_audio_channel_positions_to_valid_order (to, channels); gst_audio_get_channel_reorder_map (channels, from, to, dts->channel_reorder_map); gst_audio_info_init (&info); gst_audio_info_set_format (&info, SAMPLE_TYPE, dts->sample_rate, channels, (channels > 1 ? to : NULL)); if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dts), &info)) goto done; result = TRUE; done: return result; }
static void gst_mulawdec_init (GstMuLawDec * mulawdec) { gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (mulawdec), TRUE); gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST (mulawdec), TRUE); GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (mulawdec)); }
static void gst_droidadec_init (GstDroidADec * dec) { gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE); gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (dec), TRUE); dec->codec = NULL; dec->codec_type = NULL; dec->downstream_flow_ret = GST_FLOW_OK; dec->eos = FALSE; dec->codec_data = NULL; dec->channels = 0; dec->rate = 0; g_mutex_init (&dec->eos_lock); g_cond_init (&dec->eos_cond); dec->adapter = gst_adapter_new (); }
static void gst_wavpack_dec_init (GstWavpackDec * dec) { dec->context = NULL; dec->stream_reader = gst_wavpack_stream_reader_new (); gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE); gst_wavpack_dec_reset (dec); }
static void gst_opus_dec_init (GstOpusDec * dec) { dec->use_inband_fec = FALSE; dec->apply_gain = DEFAULT_APPLY_GAIN; gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE); gst_opus_dec_reset (dec); }
static void gst_sbc_dec_init (GstSbcDec * dec) { gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE); gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST (dec), TRUE); GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec)); dec->samples_per_frame = 0; dec->frame_len = 0; }
static void gst_ffmpegauddec_init (GstFFMpegAudDec * ffmpegdec) { GstFFMpegAudDecClass *klass = (GstFFMpegAudDecClass *) G_OBJECT_GET_CLASS (ffmpegdec); /* some ffmpeg data */ ffmpegdec->context = avcodec_alloc_context3 (klass->in_plugin); ffmpegdec->context->opaque = ffmpegdec; ffmpegdec->opened = FALSE; ffmpegdec->frame = av_frame_alloc (); GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (ffmpegdec)); gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST (ffmpegdec), TRUE); gst_audio_decoder_set_drainable (GST_AUDIO_DECODER (ffmpegdec), TRUE); gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (ffmpegdec), TRUE); }
/* called when ffmpeg wants us to allocate a buffer to write the decoded frame * into. We try to give it memory from our pool */ static int gst_ffmpegauddec_get_buffer (AVCodecContext * context, AVFrame * frame) { GstFFMpegAudDec *ffmpegdec; GstAudioInfo *info; BufferInfo *buffer_info; ffmpegdec = (GstFFMpegAudDec *) context->opaque; if (G_UNLIKELY (!gst_ffmpegauddec_negotiate (ffmpegdec, FALSE))) goto negotiate_failed; /* Always use the default allocator for planar audio formats because * we will have to copy and deinterleave later anyway */ if (av_sample_fmt_is_planar (ffmpegdec->context->sample_fmt)) goto fallback; info = gst_audio_decoder_get_audio_info (GST_AUDIO_DECODER (ffmpegdec)); buffer_info = g_slice_new (BufferInfo); buffer_info->buffer = gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (ffmpegdec), frame->nb_samples * info->bpf); gst_buffer_map (buffer_info->buffer, &buffer_info->map, GST_MAP_WRITE); frame->opaque = buffer_info; frame->data[0] = buffer_info->map.data; frame->extended_data = frame->data; frame->linesize[0] = buffer_info->map.size; frame->type = FF_BUFFER_TYPE_USER; return 0; /* fallbacks */ negotiate_failed: { GST_DEBUG_OBJECT (ffmpegdec, "negotiate failed"); goto fallback; } fallback: { return avcodec_default_get_buffer (context, frame); } }
static void gst_speex_dec_init (GstSpeexDec * dec) { gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE); gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST (dec), TRUE); GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec)); dec->enh = DEFAULT_ENH; gst_speex_dec_reset (dec); }
static void gst_opus_dec_init (GstOpusDec * dec) { dec->use_inband_fec = FALSE; dec->apply_gain = DEFAULT_APPLY_GAIN; gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE); gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST (dec), TRUE); GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec)); gst_opus_dec_reset (dec); }
static void gst_wavpack_dec_init (GstWavpackDec * dec) { dec->context = NULL; dec->stream_reader = gst_wavpack_stream_reader_new (); gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE); gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST (dec), TRUE); GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec)); gst_wavpack_dec_reset (dec); }
static void gst_a52dec_update_streaminfo (GstA52Dec * a52dec) { GstTagList *taglist; taglist = gst_tag_list_new_empty (); gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_BITRATE, (guint) a52dec->bit_rate, NULL); gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (a52dec), taglist, GST_TAG_MERGE_REPLACE); gst_tag_list_unref (taglist); }
static void gst_dtsdec_update_streaminfo (GstDtsDec * dts) { GstTagList *taglist; if (dts->bit_rate > 3) { taglist = gst_tag_list_new_empty (); /* 1 => open bitrate, 2 => variable bitrate, 3 => lossless */ gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_BITRATE, (guint) dts->bit_rate, NULL); gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dts), taglist, GST_TAG_MERGE_REPLACE); } }
void gst_imx_audio_uniaudio_dec_init(GstImxAudioUniaudioDec *imx_decoder) { GstAudioDecoder *base = GST_AUDIO_DECODER(imx_decoder); gst_audio_decoder_set_drainable(base, TRUE); gst_audio_decoder_set_plc_aware(base, FALSE); imx_decoder->codec = NULL; imx_decoder->handle = NULL; imx_decoder->original_channel_positions = NULL; imx_decoder->reordered_channel_positions = NULL; imx_decoder->out_adapter = gst_adapter_new(); imx_decoder->skip_header_counter = 0; imx_decoder->codec_data = NULL; }
static gboolean gst_amc_audio_dec_set_src_caps (GstAmcAudioDec * self, GstAmcFormat * format) { gint rate, channels; guint32 channel_mask = 0; GstAudioChannelPosition to[64]; GError *err = NULL; if (!gst_amc_format_get_int (format, "sample-rate", &rate, &err) || !gst_amc_format_get_int (format, "channel-count", &channels, &err)) { GST_ERROR_OBJECT (self, "Failed to get output format metadata: %s", err->message); g_clear_error (&err); return FALSE; } if (rate == 0 || channels == 0) { GST_ERROR_OBJECT (self, "Rate or channels not set"); return FALSE; } /* Not always present */ if (gst_amc_format_contains_key (format, "channel-mask", NULL)) gst_amc_format_get_int (format, "channel-mask", (gint *) & channel_mask, NULL); gst_amc_audio_channel_mask_to_positions (channel_mask, channels, self->positions); memcpy (to, self->positions, sizeof (to)); gst_audio_channel_positions_to_valid_order (to, channels); self->needs_reorder = (memcmp (self->positions, to, sizeof (GstAudioChannelPosition) * channels) != 0); if (self->needs_reorder) gst_audio_get_channel_reorder_map (channels, self->positions, to, self->reorder_map); gst_audio_info_init (&self->info); gst_audio_info_set_format (&self->info, GST_AUDIO_FORMAT_S16, rate, channels, to); if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (self), &self->info)) return FALSE; self->input_caps_changed = FALSE; return TRUE; }
static void gst_droidadec_finalize (GObject * object) { GstDroidADec *dec = GST_DROIDADEC (object); GST_DEBUG_OBJECT (dec, "finalize"); gst_droidadec_stop (GST_AUDIO_DECODER (dec)); g_mutex_clear (&dec->eos_lock); g_cond_clear (&dec->eos_cond); gst_object_unref (dec->adapter); dec->adapter = NULL; G_OBJECT_CLASS (parent_class)->finalize (object); }
static GstFlowReturn vorbis_handle_identification_packet (GstVorbisDec * vd) { GstAudioInfo info; switch (vd->vi.channels) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: { const GstAudioChannelPosition *pos; pos = gst_vorbis_default_channel_positions[vd->vi.channels - 1]; gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate, vd->vi.channels, pos); break; } default:{ GstAudioChannelPosition position[64]; gint i, max_pos = MAX (vd->vi.channels, 64); GST_ELEMENT_WARNING (vd, STREAM, DECODE, (NULL), ("Using NONE channel layout for more than 8 channels")); for (i = 0; i < max_pos; i++) position[i] = GST_AUDIO_CHANNEL_POSITION_NONE; gst_audio_info_set_format (&info, GST_VORBIS_AUDIO_FORMAT, vd->vi.rate, vd->vi.channels, position); break; } } gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (vd), &info); vd->info = info; /* select a copy_samples function, this way we can have specialized versions * for mono/stereo and avoid the depth switch in tremor case */ vd->copy_samples = gst_vorbis_get_copy_sample_func (info.channels); return GST_FLOW_OK; }
static void gst_opus_dec_negotiate (GstOpusDec * dec, const GstAudioChannelPosition * pos) { GstCaps *caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec)); GstStructure *s; GstAudioInfo info; if (caps) { caps = gst_caps_truncate (caps); caps = gst_caps_make_writable (caps); s = gst_caps_get_structure (caps, 0); gst_structure_fixate_field_nearest_int (s, "rate", 48000); gst_structure_get_int (s, "rate", &dec->sample_rate); gst_structure_fixate_field_nearest_int (s, "channels", dec->n_channels); gst_structure_get_int (s, "channels", &dec->n_channels); gst_caps_unref (caps); } else { dec->sample_rate = 48000; } GST_INFO_OBJECT (dec, "Negotiated %d channels, %d Hz", dec->n_channels, dec->sample_rate); /* pass valid order to audio info */ if (pos) { memcpy (dec->opus_pos, pos, sizeof (pos[0]) * dec->n_channels); gst_audio_channel_positions_to_valid_order (dec->opus_pos, dec->n_channels); } /* set up source format */ gst_audio_info_init (&info); gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, dec->sample_rate, dec->n_channels, pos ? dec->opus_pos : NULL); gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info); /* but we still need the opus order for later reordering */ if (pos) { memcpy (dec->opus_pos, pos, sizeof (pos[0]) * dec->n_channels); gst_audio_channel_positions_to_valid_order (dec->opus_pos, dec->n_channels); } else { dec->opus_pos[0] = GST_AUDIO_CHANNEL_POSITION_INVALID; } dec->info = info; }
static GstFlowReturn gst_speex_dec_parse_comments (GstSpeexDec * dec, GstBuffer * buf) { GstTagList *list; gchar *ver, *encoder = NULL; list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder); if (!list) { GST_WARNING_OBJECT (dec, "couldn't decode comments"); list = gst_tag_list_new (); } if (encoder) { gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, encoder, NULL); } gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_AUDIO_CODEC, "Speex", NULL); ver = g_strndup (dec->header->speex_version, SPEEX_HEADER_VERSION_LENGTH); g_strstrip (ver); if (ver != NULL && *ver != '\0') { gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER_VERSION, ver, NULL); } if (dec->header->bitrate > 0) { gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE, (guint) dec->header->bitrate, NULL); } GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list); gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dec), list, GST_TAG_MERGE_REPLACE); gst_tag_list_free (list); g_free (encoder); g_free (ver); return GST_FLOW_OK; }
static void gst_wavpack_dec_negotiate (GstWavpackDec * dec) { GstAudioInfo info; GstAudioFormat fmt; GstAudioChannelPosition pos[64] = { GST_AUDIO_CHANNEL_POSITION_INVALID, }; /* arrange for 1, 2 or 4-byte width == depth output */ dec->width = dec->depth; switch (dec->depth) { case 8: fmt = GST_AUDIO_FORMAT_S8; break; case 16: fmt = _GST_AUDIO_FORMAT_NE (S16); break; case 24: case 32: fmt = _GST_AUDIO_FORMAT_NE (S32); dec->width = 32; break; default: fmt = GST_AUDIO_FORMAT_UNKNOWN; g_assert_not_reached (); break; } g_assert (dec->channel_mask != 0); if (!gst_wavpack_get_channel_positions (dec->channels, dec->channel_mask, pos)) GST_WARNING_OBJECT (dec, "Failed to set channel layout"); gst_audio_info_init (&info); gst_audio_info_set_format (&info, fmt, dec->sample_rate, dec->channels, pos); gst_audio_channel_positions_to_valid_order (info.position, info.channels); gst_audio_get_channel_reorder_map (info.channels, info.position, pos, dec->channel_reorder_map); /* should always succeed */ gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info); }
static GstFlowReturn gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder, unsigned char const *decoded_bytes, size_t const num_decoded_bytes) { GstBuffer *output_buffer; GstFlowReturn alloc_error; GstAudioDecoder *dec; output_buffer = NULL; dec = GST_AUDIO_DECODER (mpg123_decoder); if ((num_decoded_bytes == 0) || (decoded_bytes == NULL)) { /* This occurs in the first few frames, which do not carry data; once MPG123_AUDIO_DEC_NEW_FORMAT is received, the empty frames stop occurring */ GST_TRACE_OBJECT (mpg123_decoder, "Nothing was decoded -> no output buffer to push"); return GST_FLOW_OK; } output_buffer = gst_buffer_new_allocate (NULL, num_decoded_bytes, NULL); alloc_error = (output_buffer == NULL) ? GST_FLOW_ERROR : GST_FLOW_OK; if (alloc_error != GST_FLOW_OK) { /* This is necessary to advance playback in time, even when nothing was decoded. */ return gst_audio_decoder_finish_frame (dec, NULL, 1); } else { GstMapInfo info; if (gst_buffer_map (output_buffer, &info, GST_MAP_WRITE)) { if (info.size != num_decoded_bytes) GST_ERROR_OBJECT (mpg123_decoder, "Mapped memory region has size %u instead of expected size %u", info.size, num_decoded_bytes); else memcpy (info.data, decoded_bytes, num_decoded_bytes); gst_buffer_unmap (output_buffer, &info); } else GST_ERROR_OBJECT (mpg123_decoder, "Could not map buffer"); return gst_audio_decoder_finish_frame (dec, output_buffer, 1); } }
static GstFlowReturn gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder, unsigned char const *decoded_bytes, size_t const num_decoded_bytes) { GstBuffer *output_buffer; GstAudioDecoder *dec; output_buffer = NULL; dec = GST_AUDIO_DECODER (mpg123_decoder); if ((num_decoded_bytes == 0) || (decoded_bytes == NULL)) { /* This occurs in the first few frames, which do not carry data; once * MPG123_AUDIO_DEC_NEW_FORMAT is received, the empty frames stop occurring */ GST_DEBUG_OBJECT (mpg123_decoder, "cannot decode yet, need more data -> no output buffer to push"); return GST_FLOW_OK; } output_buffer = gst_buffer_new_allocate (NULL, num_decoded_bytes, NULL); if (output_buffer == NULL) { /* This is necessary to advance playback in time, * even when nothing was decoded. */ return gst_audio_decoder_finish_frame (dec, NULL, 1); } else { GstMapInfo info; if (gst_buffer_map (output_buffer, &info, GST_MAP_WRITE)) { memcpy (info.data, decoded_bytes, num_decoded_bytes); gst_buffer_unmap (output_buffer, &info); } else { GST_ERROR_OBJECT (mpg123_decoder, "gst_buffer_map() returned NULL"); gst_buffer_unref (output_buffer); output_buffer = NULL; } return gst_audio_decoder_finish_frame (dec, output_buffer, 1); } }
static void gst_wavpack_dec_post_tags (GstWavpackDec * dec) { GstTagList *list; GstFormat format_time = GST_FORMAT_TIME, format_bytes = GST_FORMAT_BYTES; gint64 duration, size; /* try to estimate the average bitrate */ if (gst_pad_peer_query_duration (GST_AUDIO_DECODER_SINK_PAD (dec), format_bytes, &size) && gst_pad_peer_query_duration (GST_AUDIO_DECODER_SINK_PAD (dec), format_time, &duration) && size > 0 && duration > 0) { guint64 bitrate; list = gst_tag_list_new_empty (); bitrate = gst_util_uint64_scale (size, 8 * GST_SECOND, duration); gst_tag_list_add (list, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE, (guint) bitrate, NULL); gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dec), list, GST_TAG_MERGE_REPLACE); } }
static GstFlowReturn opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buffer) { GstFlowReturn res = GST_FLOW_OK; gsize size; guint8 *data; GstBuffer *outbuf; gint16 *out_data; int n, err; int samples; unsigned int packet_size; GstBuffer *buf; GstMapInfo map, omap; if (dec->state == NULL) { /* If we did not get any headers, default to 2 channels */ if (dec->n_channels == 0) { GST_INFO_OBJECT (dec, "No header, assuming single stream"); dec->n_channels = 2; dec->sample_rate = 48000; /* default stereo mapping */ dec->channel_mapping_family = 0; dec->channel_mapping[0] = 0; dec->channel_mapping[1] = 1; dec->n_streams = 1; dec->n_stereo_streams = 1; gst_opus_dec_negotiate (dec, NULL); } GST_DEBUG_OBJECT (dec, "Creating decoder with %d channels, %d Hz", dec->n_channels, dec->sample_rate); #ifndef GST_DISABLE_GST_DEBUG gst_opus_common_log_channel_mapping_table (GST_ELEMENT (dec), opusdec_debug, "Mapping table", dec->n_channels, dec->channel_mapping); #endif GST_DEBUG_OBJECT (dec, "%d streams, %d stereo", dec->n_streams, dec->n_stereo_streams); dec->state = opus_multistream_decoder_create (dec->sample_rate, dec->n_channels, dec->n_streams, dec->n_stereo_streams, dec->channel_mapping, &err); if (!dec->state || err != OPUS_OK) goto creation_failed; } if (buffer) { GST_DEBUG_OBJECT (dec, "Received buffer of size %" G_GSIZE_FORMAT, gst_buffer_get_size (buffer)); } else { GST_DEBUG_OBJECT (dec, "Received missing buffer"); } /* if using in-band FEC, we introdude one extra frame's delay as we need to potentially wait for next buffer to decode a missing buffer */ if (dec->use_inband_fec && !dec->primed) { GST_DEBUG_OBJECT (dec, "First buffer received in FEC mode, early out"); gst_buffer_replace (&dec->last_buffer, buffer); dec->primed = TRUE; goto done; } /* That's the buffer we'll be sending to the opus decoder. */ buf = (dec->use_inband_fec && gst_buffer_get_size (dec->last_buffer) > 0) ? dec->last_buffer : buffer; if (buf && gst_buffer_get_size (buf) > 0) { gst_buffer_map (buf, &map, GST_MAP_READ); data = map.data; size = map.size; GST_DEBUG_OBJECT (dec, "Using buffer of size %" G_GSIZE_FORMAT, size); } else { /* concealment data, pass NULL as the bits parameters */ GST_DEBUG_OBJECT (dec, "Using NULL buffer"); data = NULL; size = 0; } /* use maximum size (120 ms) as the number of returned samples is not constant over the stream. */ samples = 120 * dec->sample_rate / 1000; packet_size = samples * dec->n_channels * 2; outbuf = gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (dec), packet_size); if (!outbuf) { goto buffer_failed; } gst_buffer_map (outbuf, &omap, GST_MAP_WRITE); out_data = (gint16 *) omap.data; if (dec->use_inband_fec) { if (dec->last_buffer) { /* normal delayed decode */ GST_LOG_OBJECT (dec, "FEC enabled, decoding last delayed buffer"); n = opus_multistream_decode (dec->state, data, size, out_data, samples, 0); } else { /* FEC reconstruction decode */ GST_LOG_OBJECT (dec, "FEC enabled, reconstructing last buffer"); n = opus_multistream_decode (dec->state, data, size, out_data, samples, 1); } } else { /* normal decode */ GST_LOG_OBJECT (dec, "FEC disabled, decoding buffer"); n = opus_multistream_decode (dec->state, data, size, out_data, samples, 0); } gst_buffer_unmap (outbuf, &omap); if (data != NULL) gst_buffer_unmap (buf, &map); if (n < 0) { GST_ELEMENT_ERROR (dec, STREAM, DECODE, ("Decoding error: %d", n), (NULL)); gst_buffer_unref (outbuf); return GST_FLOW_ERROR; } GST_DEBUG_OBJECT (dec, "decoded %d samples", n); gst_buffer_set_size (outbuf, n * 2 * dec->n_channels); /* Skip any samples that need skipping */ if (dec->pre_skip > 0) { guint scaled_pre_skip = dec->pre_skip * dec->sample_rate / 48000; guint skip = scaled_pre_skip > n ? n : scaled_pre_skip; guint scaled_skip = skip * 48000 / dec->sample_rate; gst_buffer_resize (outbuf, skip * 2 * dec->n_channels, -1); dec->pre_skip -= scaled_skip; GST_INFO_OBJECT (dec, "Skipping %u samples (%u at 48000 Hz, %u left to skip)", skip, scaled_skip, dec->pre_skip); } if (gst_buffer_get_size (outbuf) == 0) { gst_buffer_unref (outbuf); outbuf = NULL; } else if (dec->opus_pos[0] != GST_AUDIO_CHANNEL_POSITION_INVALID) { gst_audio_buffer_reorder_channels (outbuf, GST_AUDIO_FORMAT_S16, dec->n_channels, dec->opus_pos, dec->info.position); } /* Apply gain */ /* Would be better off leaving this to a volume element, as this is a naive conversion that does too many int/float conversions. However, we don't have control over the pipeline... So make it optional if the user program wants to use a volume, but do it by default so the correct volume goes out by default */ if (dec->apply_gain && outbuf && dec->r128_gain) { gsize rsize; unsigned int i, nsamples; double volume = dec->r128_gain_volume; gint16 *samples; gst_buffer_map (outbuf, &omap, GST_MAP_READWRITE); samples = (gint16 *) omap.data; rsize = omap.size; GST_DEBUG_OBJECT (dec, "Applying gain: volume %f", volume); nsamples = rsize / 2; for (i = 0; i < nsamples; ++i) { int sample = (int) (samples[i] * volume + 0.5); samples[i] = sample < -32768 ? -32768 : sample > 32767 ? 32767 : sample; } gst_buffer_unmap (outbuf, &omap); } if (dec->use_inband_fec) { gst_buffer_replace (&dec->last_buffer, buffer); } res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1); if (res != GST_FLOW_OK) GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res)); done: return res; creation_failed: GST_ERROR_OBJECT (dec, "Failed to create Opus decoder: %d", err); return GST_FLOW_ERROR; buffer_failed: GST_ERROR_OBJECT (dec, "Failed to create %u byte buffer", packet_size); return GST_FLOW_ERROR; }
static GstFlowReturn vorbis_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer) { ogg_packet *packet; ogg_packet_wrapper packet_wrapper; GstFlowReturn result = GST_FLOW_OK; GstMapInfo map; GstVorbisDec *vd = GST_VORBIS_DEC (dec); /* no draining etc */ if (G_UNLIKELY (!buffer)) return GST_FLOW_OK; GST_LOG_OBJECT (vd, "got buffer %p", buffer); /* make ogg_packet out of the buffer */ gst_ogg_packet_wrapper_map (&packet_wrapper, buffer, &map); packet = gst_ogg_packet_from_wrapper (&packet_wrapper); /* set some more stuff */ packet->granulepos = -1; packet->packetno = 0; /* we don't care */ /* EOS does not matter, it is used in vorbis to implement clipping the last * block of samples based on the granulepos. We clip based on segments. */ packet->e_o_s = 0; GST_LOG_OBJECT (vd, "decode buffer of size %ld", packet->bytes); /* error out on empty header packets, but just skip empty data packets */ if (G_UNLIKELY (packet->bytes == 0)) { if (vd->initialized) goto empty_buffer; else goto empty_header; } /* switch depending on packet type */ if ((gst_ogg_packet_data (packet))[0] & 1) { if (vd->initialized) { GST_WARNING_OBJECT (vd, "Already initialized, so ignoring header packet"); goto done; } result = vorbis_handle_header_packet (vd, packet); if (result != GST_FLOW_OK) goto done; /* consumer header packet/frame */ result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), NULL, 1); } else { GstClockTime timestamp, duration; timestamp = GST_BUFFER_TIMESTAMP (buffer); duration = GST_BUFFER_DURATION (buffer); result = vorbis_handle_data_packet (vd, packet, timestamp, duration); } done: GST_LOG_OBJECT (vd, "unmap buffer %p", buffer); gst_ogg_packet_wrapper_unmap (&packet_wrapper, buffer, &map); return result; empty_buffer: { /* don't error out here, just ignore the buffer, it's invalid for vorbis * but not fatal. */ GST_WARNING_OBJECT (vd, "empty buffer received, ignoring"); result = GST_FLOW_OK; goto done; } /* ERRORS */ empty_header: { GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received")); result = GST_FLOW_ERROR; goto done; } }
static GstFlowReturn vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet, GstClockTime timestamp, GstClockTime duration) { #ifdef USE_TREMOLO vorbis_sample_t *pcm; #else vorbis_sample_t **pcm; #endif guint sample_count; GstBuffer *out = NULL; GstFlowReturn result; GstMapInfo map; gsize size; if (G_UNLIKELY (!vd->initialized)) { result = vorbis_dec_handle_header_caps (vd); if (result != GST_FLOW_OK) goto not_initialized; } /* normal data packet */ /* FIXME, we can skip decoding if the packet is outside of the * segment, this is however not very trivial as we need a previous * packet to decode the current one so we must be careful not to * throw away too much. For now we decode everything and clip right * before pushing data. */ #ifdef USE_TREMOLO if (G_UNLIKELY (vorbis_dsp_synthesis (&vd->vd, packet, 1))) goto could_not_read; #else if (G_UNLIKELY (vorbis_synthesis (&vd->vb, packet))) goto could_not_read; if (G_UNLIKELY (vorbis_synthesis_blockin (&vd->vd, &vd->vb) < 0)) goto not_accepted; #endif /* assume all goes well here */ result = GST_FLOW_OK; /* count samples ready for reading */ #ifdef USE_TREMOLO if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0) #else if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0) goto done; #endif size = sample_count * vd->info.bpf; GST_LOG_OBJECT (vd, "%d samples ready for reading, size %" G_GSIZE_FORMAT, sample_count, size); /* alloc buffer for it */ out = gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (vd), size); gst_buffer_map (out, &map, GST_MAP_WRITE); /* get samples ready for reading now, should be sample_count */ #ifdef USE_TREMOLO if (G_UNLIKELY (vorbis_dsp_pcmout (&vd->vd, map.data, sample_count) != sample_count)) #else if (G_UNLIKELY (vorbis_synthesis_pcmout (&vd->vd, &pcm) != sample_count)) #endif goto wrong_samples; #ifdef USE_TREMOLO if (vd->info.channels < 9) gst_audio_reorder_channels (map.data, map.size, GST_VORBIS_AUDIO_FORMAT, vd->info.channels, gst_vorbis_channel_positions[vd->info.channels - 1], gst_vorbis_default_channel_positions[vd->info.channels - 1]); #else /* copy samples in buffer */ vd->copy_samples ((vorbis_sample_t *) map.data, pcm, sample_count, vd->info.channels); #endif GST_LOG_OBJECT (vd, "have output size of %" G_GSIZE_FORMAT, size); gst_buffer_unmap (out, &map); done: /* whether or not data produced, consume one frame and advance time */ result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), out, 1); #ifdef USE_TREMOLO vorbis_dsp_read (&vd->vd, sample_count); #else vorbis_synthesis_read (&vd->vd, sample_count); #endif return result; /* ERRORS */ not_initialized: { GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE, (NULL), ("no header sent yet")); return GST_FLOW_NOT_NEGOTIATED; } could_not_read: { GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE, (NULL), ("couldn't read data packet")); return GST_FLOW_ERROR; } not_accepted: { GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE, (NULL), ("vorbis decoder did not accept data packet")); return GST_FLOW_ERROR; } wrong_samples: { gst_buffer_unref (out); GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE, (NULL), ("vorbis decoder reported wrong number of samples")); return GST_FLOW_ERROR; } }
static gboolean gst_amc_audio_dec_set_format (GstAudioDecoder * decoder, GstCaps * caps) { GstAmcAudioDec *self; GstStructure *s; GstAmcFormat *format; const gchar *mime; gboolean is_format_change = FALSE; gboolean needs_disable = FALSE; gchar *format_string; gint rate, channels; GError *err = NULL; self = GST_AMC_AUDIO_DEC (decoder); GST_DEBUG_OBJECT (self, "Setting new caps %" GST_PTR_FORMAT, caps); /* Check if the caps change is a real format change or if only irrelevant * parts of the caps have changed or nothing at all. */ is_format_change |= (!self->input_caps || !gst_caps_is_equal (self->input_caps, caps)); needs_disable = self->started; /* If the component is not started and a real format change happens * we have to restart the component. If no real format change * happened we can just exit here. */ if (needs_disable && !is_format_change) { /* Framerate or something minor changed */ self->input_caps_changed = TRUE; GST_DEBUG_OBJECT (self, "Already running and caps did not change the format"); return TRUE; } if (needs_disable && is_format_change) { gst_amc_audio_dec_drain (self); GST_AUDIO_DECODER_STREAM_UNLOCK (self); gst_amc_audio_dec_stop (GST_AUDIO_DECODER (self)); GST_AUDIO_DECODER_STREAM_LOCK (self); gst_amc_audio_dec_close (GST_AUDIO_DECODER (self)); if (!gst_amc_audio_dec_open (GST_AUDIO_DECODER (self))) { GST_ERROR_OBJECT (self, "Failed to open codec again"); return FALSE; } if (!gst_amc_audio_dec_start (GST_AUDIO_DECODER (self))) { GST_ERROR_OBJECT (self, "Failed to start codec again"); } } /* srcpad task is not running at this point */ mime = caps_to_mime (caps); if (!mime) { GST_ERROR_OBJECT (self, "Failed to convert caps to mime"); return FALSE; } s = gst_caps_get_structure (caps, 0); if (!gst_structure_get_int (s, "rate", &rate) || !gst_structure_get_int (s, "channels", &channels)) { GST_ERROR_OBJECT (self, "Failed to get rate/channels"); return FALSE; } format = gst_amc_format_new_audio (mime, rate, channels, &err); if (!format) { GST_ELEMENT_ERROR_FROM_ERROR (self, err); return FALSE; } /* FIXME: These buffers needs to be valid until the codec is stopped again */ g_list_foreach (self->codec_datas, (GFunc) gst_buffer_unref, NULL); g_list_free (self->codec_datas); self->codec_datas = NULL; if (gst_structure_has_field (s, "codec_data")) { const GValue *h = gst_structure_get_value (s, "codec_data"); GstBuffer *codec_data = gst_value_get_buffer (h); GstMapInfo minfo; guint8 *data; gst_buffer_map (codec_data, &minfo, GST_MAP_READ); data = g_memdup (minfo.data, minfo.size); self->codec_datas = g_list_prepend (self->codec_datas, data); gst_amc_format_set_buffer (format, "csd-0", data, minfo.size, &err); if (err) GST_ELEMENT_WARNING_FROM_ERROR (self, err); gst_buffer_unmap (codec_data, &minfo); } else if (gst_structure_has_field (s, "streamheader")) { const GValue *sh = gst_structure_get_value (s, "streamheader"); gint nsheaders = gst_value_array_get_size (sh); GstBuffer *buf; const GValue *h; gint i, j; gchar *fname; GstMapInfo minfo; guint8 *data; for (i = 0, j = 0; i < nsheaders; i++) { h = gst_value_array_get_value (sh, i); buf = gst_value_get_buffer (h); if (strcmp (mime, "audio/vorbis") == 0) { guint8 header_type; gst_buffer_extract (buf, 0, &header_type, 1); /* Only use the identification and setup packets */ if (header_type != 0x01 && header_type != 0x05) continue; } fname = g_strdup_printf ("csd-%d", j); gst_buffer_map (buf, &minfo, GST_MAP_READ); data = g_memdup (minfo.data, minfo.size); self->codec_datas = g_list_prepend (self->codec_datas, data); gst_amc_format_set_buffer (format, fname, data, minfo.size, &err); if (err) GST_ELEMENT_WARNING_FROM_ERROR (self, err); gst_buffer_unmap (buf, &minfo); g_free (fname); j++; } } format_string = gst_amc_format_to_string (format, &err); if (err) GST_ELEMENT_WARNING_FROM_ERROR (self, err); GST_DEBUG_OBJECT (self, "Configuring codec with format: %s", GST_STR_NULL (format_string)); g_free (format_string); if (!gst_amc_codec_configure (self->codec, format, 0, &err)) { GST_ERROR_OBJECT (self, "Failed to configure codec"); GST_ELEMENT_ERROR_FROM_ERROR (self, err); return FALSE; } gst_amc_format_free (format); if (!gst_amc_codec_start (self->codec, &err)) { GST_ERROR_OBJECT (self, "Failed to start codec"); GST_ELEMENT_ERROR_FROM_ERROR (self, err); return FALSE; } self->spf = -1; /* TODO: Implement for other codecs too */ if (gst_structure_has_name (s, "audio/mpeg")) { gint mpegversion = -1; gst_structure_get_int (s, "mpegversion", &mpegversion); if (mpegversion == 1) { gint layer = -1, mpegaudioversion = -1; gst_structure_get_int (s, "layer", &layer); gst_structure_get_int (s, "mpegaudioversion", &mpegaudioversion); if (layer == 1) self->spf = 384; else if (layer == 2) self->spf = 1152; else if (layer == 3 && mpegaudioversion != -1) self->spf = (mpegaudioversion == 1 ? 1152 : 576); } } self->started = TRUE; self->input_caps_changed = TRUE; /* Start the srcpad loop again */ self->flushing = FALSE; self->downstream_flow_ret = GST_FLOW_OK; gst_pad_start_task (GST_AUDIO_DECODER_SRC_PAD (self), (GstTaskFunction) gst_amc_audio_dec_loop, decoder, NULL); return TRUE; }