static void gst_a52dec_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstA52Dec *src = GST_A52DEC (object); switch (prop_id) { case ARG_DRC: GST_OBJECT_LOCK (src); g_value_set_boolean (value, src->dynamic_range_compression); GST_OBJECT_UNLOCK (src); break; case ARG_MODE: GST_OBJECT_LOCK (src); g_value_set_enum (value, src->request_channels & A52_CHANNEL_MASK); GST_OBJECT_UNLOCK (src); break; case ARG_LFE: GST_OBJECT_LOCK (src); g_value_set_boolean (value, src->request_channels & A52_LFE); GST_OBJECT_UNLOCK (src); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static GstStateChangeReturn gst_a52dec_change_state (GstElement * element, GstStateChange transition) { GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; GstA52Dec *a52dec = GST_A52DEC (element); switch (transition) { case GST_STATE_CHANGE_NULL_TO_READY:{ GstA52DecClass *klass; klass = GST_A52DEC_CLASS (G_OBJECT_GET_CLASS (a52dec)); a52dec->state = a52_init (klass->a52_cpuflags); break; } case GST_STATE_CHANGE_READY_TO_PAUSED: a52dec->samples = a52_samples (a52dec->state); a52dec->bit_rate = -1; a52dec->sample_rate = -1; a52dec->stream_channels = A52_CHANNEL; a52dec->using_channels = A52_CHANNEL; a52dec->level = 1; a52dec->bias = 0; a52dec->time = 0; a52dec->sent_segment = FALSE; a52dec->flag_update = TRUE; gst_segment_init (&a52dec->segment, GST_FORMAT_UNDEFINED); break; case GST_STATE_CHANGE_PAUSED_TO_PLAYING: break; default: break; } ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); switch (transition) { case GST_STATE_CHANGE_PLAYING_TO_PAUSED: break; case GST_STATE_CHANGE_PAUSED_TO_READY: a52dec->samples = NULL; if (a52dec->cache) { gst_buffer_unref (a52dec->cache); a52dec->cache = NULL; } clear_queued (a52dec); break; case GST_STATE_CHANGE_READY_TO_NULL: a52_free (a52dec->state); a52dec->state = NULL; break; default: break; } return ret; }
static gboolean gst_a52dec_set_format (GstAudioDecoder * bdec, GstCaps * caps) { GstA52Dec *a52dec = GST_A52DEC (bdec); GstStructure *structure; structure = gst_caps_get_structure (caps, 0); if (structure && gst_structure_has_name (structure, "audio/x-private1-ac3")) a52dec->dvdmode = TRUE; else a52dec->dvdmode = FALSE; return TRUE; }
static gboolean gst_a52dec_stop (GstAudioDecoder * dec) { GstA52Dec *a52dec = GST_A52DEC (dec); GST_DEBUG_OBJECT (dec, "stop"); a52dec->samples = NULL; if (a52dec->state) { a52_free (a52dec->state); a52dec->state = NULL; } return TRUE; }
static gboolean gst_a52dec_sink_setcaps (GstPad * pad, GstCaps * caps) { GstA52Dec *a52dec = GST_A52DEC (gst_pad_get_parent (pad)); GstStructure *structure; structure = gst_caps_get_structure (caps, 0); if (structure && gst_structure_has_name (structure, "audio/x-private1-ac3")) a52dec->dvdmode = TRUE; else a52dec->dvdmode = FALSE; gst_object_unref (a52dec); return TRUE; }
static GstFlowReturn gst_a52dec_parse (GstAudioDecoder * bdec, GstAdapter * adapter, gint * _offset, gint * len) { GstA52Dec *a52dec; const guint8 *data; gint av, size; gint length = 0, flags, sample_rate, bit_rate; GstFlowReturn result = GST_FLOW_EOS; a52dec = GST_A52DEC (bdec); size = av = gst_adapter_available (adapter); data = (const guint8 *) gst_adapter_map (adapter, av); /* find and read header */ bit_rate = a52dec->bit_rate; sample_rate = a52dec->sample_rate; flags = 0; while (size >= 7) { length = a52_syncinfo ((guint8 *) data, &flags, &sample_rate, &bit_rate); if (length == 0) { /* shift window to re-find sync */ data++; size--; } else if (length <= size) { GST_LOG_OBJECT (a52dec, "Sync: frame size %d", length); result = GST_FLOW_OK; break; } else { GST_LOG_OBJECT (a52dec, "Not enough data available (needed %d had %d)", length, size); break; } } gst_adapter_unmap (adapter); *_offset = av - size; *len = length; return result; }
static gboolean gst_a52dec_start (GstAudioDecoder * dec) { GstA52Dec *a52dec = GST_A52DEC (dec); GstA52DecClass *klass; GST_DEBUG_OBJECT (dec, "start"); klass = GST_A52DEC_CLASS (G_OBJECT_GET_CLASS (a52dec)); #if defined(A52_ACCEL_DETECT) a52dec->state = a52_init (); /* This line is just to avoid being accused of not using klass */ a52_accel (klass->a52_cpuflags & A52_ACCEL_DETECT); #else a52dec->state = a52_init (klass->a52_cpuflags); #endif if (!a52dec->state) { GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), LIBRARY, INIT, (NULL), ("failed to initialize a52 state")); return FALSE; } a52dec->samples = a52_samples (a52dec->state); a52dec->bit_rate = -1; a52dec->sample_rate = -1; a52dec->stream_channels = A52_CHANNEL; a52dec->using_channels = A52_CHANNEL; a52dec->level = 1; a52dec->bias = 0; a52dec->flag_update = TRUE; /* call upon legacy upstream byte support (e.g. seeking) */ gst_audio_decoder_set_estimate_rate (dec, TRUE); return TRUE; }
static GstFlowReturn gst_a52dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) { GstA52Dec *a52dec = GST_A52DEC (parent); GstFlowReturn ret = GST_FLOW_OK; gint first_access; if (a52dec->dvdmode) { gsize size; guint8 data[2]; gint offset; gint len; GstBuffer *subbuf; size = gst_buffer_get_size (buf); if (size < 2) goto not_enough_data; gst_buffer_extract (buf, 0, data, 2); first_access = (data[0] << 8) | data[1]; /* Skip the first_access header */ offset = 2; if (first_access > 1) { /* Length of data before first_access */ len = first_access - 1; if (len <= 0 || offset + len > size) goto bad_first_access_parameter; subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len); GST_BUFFER_TIMESTAMP (subbuf) = GST_CLOCK_TIME_NONE; ret = a52dec->base_chain (pad, parent, subbuf); if (ret != GST_FLOW_OK) { gst_buffer_unref (buf); goto done; } offset += len; len = size - offset; if (len > 0) { subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, len); GST_BUFFER_TIMESTAMP (subbuf) = GST_BUFFER_TIMESTAMP (buf); ret = a52dec->base_chain (pad, parent, subbuf); } gst_buffer_unref (buf); } else { /* first_access = 0 or 1, so if there's a timestamp it applies to the first byte */ subbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, offset, size - offset); GST_BUFFER_TIMESTAMP (subbuf) = GST_BUFFER_TIMESTAMP (buf); gst_buffer_unref (buf); ret = a52dec->base_chain (pad, parent, subbuf); } } else { ret = a52dec->base_chain (pad, parent, buf); } done: return ret; /* ERRORS */ not_enough_data: { GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), STREAM, DECODE, (NULL), ("Insufficient data in buffer. Can't determine first_acess")); gst_buffer_unref (buf); return GST_FLOW_ERROR; } bad_first_access_parameter: { GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), STREAM, DECODE, (NULL), ("Bad first_access parameter (%d) in buffer", first_access)); gst_buffer_unref (buf); return GST_FLOW_ERROR; } }
static GstFlowReturn gst_a52dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buffer) { GstA52Dec *a52dec; gint channels, i; gboolean need_reneg = FALSE; gint chans; gint length = 0, flags, sample_rate, bit_rate; GstMapInfo map; GstFlowReturn result = GST_FLOW_OK; GstBuffer *outbuf; const gint num_blocks = 6; a52dec = GST_A52DEC (bdec); /* no fancy draining */ if (G_UNLIKELY (!buffer)) return GST_FLOW_OK; /* parsed stuff already, so this should work out fine */ gst_buffer_map (buffer, &map, GST_MAP_READ); g_assert (map.size >= 7); /* re-obtain some sync header info, * should be same as during _parse and could also be cached there, * but anyway ... */ bit_rate = a52dec->bit_rate; sample_rate = a52dec->sample_rate; flags = 0; length = a52_syncinfo (map.data, &flags, &sample_rate, &bit_rate); g_assert (length == map.size); /* update stream information, renegotiate or re-streaminfo if needed */ need_reneg = FALSE; if (a52dec->sample_rate != sample_rate) { GST_DEBUG_OBJECT (a52dec, "sample rate changed"); need_reneg = TRUE; a52dec->sample_rate = sample_rate; } if (flags) { if (a52dec->stream_channels != (flags & (A52_CHANNEL_MASK | A52_LFE))) { GST_DEBUG_OBJECT (a52dec, "stream channel flags changed, marking update"); a52dec->flag_update = TRUE; } a52dec->stream_channels = flags & (A52_CHANNEL_MASK | A52_LFE); } if (bit_rate != a52dec->bit_rate) { a52dec->bit_rate = bit_rate; gst_a52dec_update_streaminfo (a52dec); } /* If we haven't had an explicit number of channels chosen through properties * at this point, choose what to downmix to now, based on what the peer will * accept - this allows a52dec to do downmixing in preference to a * downstream element such as audioconvert. */ if (a52dec->request_channels != A52_CHANNEL) { flags = a52dec->request_channels; } else if (a52dec->flag_update) { GstCaps *caps; a52dec->flag_update = FALSE; caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (a52dec)); if (caps && gst_caps_get_size (caps) > 0) { GstCaps *copy = gst_caps_copy_nth (caps, 0); GstStructure *structure = gst_caps_get_structure (copy, 0); gint orig_channels = flags ? gst_a52dec_channels (flags, NULL) : 6; gint fixed_channels = 0; const int a52_channels[6] = { A52_MONO, A52_STEREO, A52_STEREO | A52_LFE, A52_2F2R, A52_2F2R | A52_LFE, A52_3F2R | A52_LFE, }; /* Prefer the original number of channels, but fixate to something * preferred (first in the caps) downstream if possible. */ gst_structure_fixate_field_nearest_int (structure, "channels", orig_channels); if (gst_structure_get_int (structure, "channels", &fixed_channels) && fixed_channels <= 6) { if (fixed_channels < orig_channels) flags = a52_channels[fixed_channels - 1]; } else { flags = a52_channels[5]; } gst_caps_unref (copy); } else if (flags) flags = a52dec->stream_channels; else flags = A52_3F2R | A52_LFE; if (caps) gst_caps_unref (caps); } else { flags = a52dec->using_channels; } /* process */ flags |= A52_ADJUST_LEVEL; a52dec->level = 1; if (a52_frame (a52dec->state, map.data, &flags, &a52dec->level, a52dec->bias)) { gst_buffer_unmap (buffer, &map); GST_AUDIO_DECODER_ERROR (a52dec, 1, STREAM, DECODE, (NULL), ("a52_frame error"), result); goto exit; } gst_buffer_unmap (buffer, &map); channels = flags & (A52_CHANNEL_MASK | A52_LFE); if (a52dec->using_channels != channels) { need_reneg = TRUE; a52dec->using_channels = channels; } /* negotiate if required */ if (need_reneg) { GST_DEBUG_OBJECT (a52dec, "a52dec reneg: sample_rate:%d stream_chans:%d using_chans:%d", a52dec->sample_rate, a52dec->stream_channels, a52dec->using_channels); if (!gst_a52dec_reneg (a52dec)) goto failed_negotiation; } if (a52dec->dynamic_range_compression == FALSE) { a52_dynrng (a52dec->state, NULL, NULL); } flags &= (A52_CHANNEL_MASK | A52_LFE); chans = gst_a52dec_channels (flags, NULL); if (!chans) goto invalid_flags; /* handle decoded data; * each frame has 6 blocks, one block is 256 samples, ea */ outbuf = gst_buffer_new_and_alloc (256 * chans * (SAMPLE_WIDTH / 8) * num_blocks); gst_buffer_map (outbuf, &map, GST_MAP_WRITE); { guint8 *ptr = map.data; for (i = 0; i < num_blocks; i++) { if (a52_block (a52dec->state)) { /* also marks discont */ GST_AUDIO_DECODER_ERROR (a52dec, 1, STREAM, DECODE, (NULL), ("error decoding block %d", i), result); if (result != GST_FLOW_OK) { gst_buffer_unmap (outbuf, &map); goto exit; } } else { gint n, c; gint *reorder_map = a52dec->channel_reorder_map; for (n = 0; n < 256; n++) { for (c = 0; c < chans; c++) { ((sample_t *) ptr)[n * chans + reorder_map[c]] = a52dec->samples[c * 256 + n]; } } } ptr += 256 * chans * (SAMPLE_WIDTH / 8); } } gst_buffer_unmap (outbuf, &map); result = gst_audio_decoder_finish_frame (bdec, outbuf, 1); exit: return result; /* ERRORS */ failed_negotiation: { GST_ELEMENT_ERROR (a52dec, CORE, NEGOTIATION, (NULL), (NULL)); return GST_FLOW_ERROR; } invalid_flags: { GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), STREAM, DECODE, (NULL), ("Invalid channel flags: %d", flags)); return GST_FLOW_ERROR; } }
static GstFlowReturn gst_a52dec_chain_raw (GstPad * pad, GstBuffer * buf) { GstA52Dec *a52dec; guint8 *data; guint size; gint length = 0, flags, sample_rate, bit_rate; GstFlowReturn result = GST_FLOW_OK; a52dec = GST_A52DEC (GST_PAD_PARENT (pad)); if (!a52dec->sent_segment) { GstSegment segment; /* Create a basic segment. Usually, we'll get a new-segment sent by * another element that will know more information (a demuxer). If we're * just looking at a raw AC3 stream, we won't - so we need to send one * here, but we don't know much info, so just send a minimal TIME * new-segment event */ gst_segment_init (&segment, GST_FORMAT_TIME); gst_pad_push_event (a52dec->srcpad, gst_event_new_new_segment (FALSE, segment.rate, segment.format, segment.start, segment.duration, segment.start)); a52dec->sent_segment = TRUE; } /* merge with cache, if any. Also make sure timestamps match */ if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) { a52dec->time = GST_BUFFER_TIMESTAMP (buf); GST_DEBUG_OBJECT (a52dec, "Received buffer with ts %" GST_TIME_FORMAT " duration %" GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_TIME_ARGS (GST_BUFFER_DURATION (buf))); } if (a52dec->cache) { buf = gst_buffer_join (a52dec->cache, buf); a52dec->cache = NULL; } data = GST_BUFFER_DATA (buf); size = GST_BUFFER_SIZE (buf); /* find and read header */ bit_rate = a52dec->bit_rate; sample_rate = a52dec->sample_rate; flags = 0; while (size >= 7) { length = a52_syncinfo (data, &flags, &sample_rate, &bit_rate); if (length == 0) { /* no sync */ data++; size--; } else if (length <= size) { GST_DEBUG ("Sync: %d", length); if (flags != a52dec->prev_flags) a52dec->flag_update = TRUE; a52dec->prev_flags = flags; result = gst_a52dec_handle_frame (a52dec, data, length, flags, sample_rate, bit_rate); if (result != GST_FLOW_OK) { size = 0; break; } size -= length; data += length; } else { /* not enough data */ GST_LOG ("Not enough data available"); break; } } /* keep cache */ if (length == 0) { GST_LOG ("No sync found"); } if (size > 0) { a52dec->cache = gst_buffer_create_sub (buf, GST_BUFFER_SIZE (buf) - size, size); } gst_buffer_unref (buf); return result; }
static GstFlowReturn gst_a52dec_chain (GstPad * pad, GstBuffer * buf) { GstA52Dec *a52dec = GST_A52DEC (GST_PAD_PARENT (pad)); GstFlowReturn ret; gint first_access; if (GST_BUFFER_IS_DISCONT (buf)) { GST_LOG_OBJECT (a52dec, "received DISCONT"); gst_a52dec_drain (a52dec); /* clear cache on discont and mark a discont in the element */ if (a52dec->cache) { gst_buffer_unref (a52dec->cache); a52dec->cache = NULL; } a52dec->discont = TRUE; } if (a52dec->dvdmode) { gint size = GST_BUFFER_SIZE (buf); guchar *data = GST_BUFFER_DATA (buf); gint offset; gint len; GstBuffer *subbuf; if (size < 2) goto not_enough_data; first_access = (data[0] << 8) | data[1]; /* Skip the first_access header */ offset = 2; if (first_access > 1) { /* Length of data before first_access */ len = first_access - 1; if (len <= 0 || offset + len > size) goto bad_first_access_parameter; subbuf = gst_buffer_create_sub (buf, offset, len); GST_BUFFER_TIMESTAMP (subbuf) = GST_CLOCK_TIME_NONE; ret = gst_a52dec_chain_raw (pad, subbuf); if (ret != GST_FLOW_OK) goto done; offset += len; len = size - offset; if (len > 0) { subbuf = gst_buffer_create_sub (buf, offset, len); GST_BUFFER_TIMESTAMP (subbuf) = GST_BUFFER_TIMESTAMP (buf); ret = gst_a52dec_chain_raw (pad, subbuf); } } else { /* first_access = 0 or 1, so if there's a timestamp it applies to the first byte */ subbuf = gst_buffer_create_sub (buf, offset, size - offset); GST_BUFFER_TIMESTAMP (subbuf) = GST_BUFFER_TIMESTAMP (buf); ret = gst_a52dec_chain_raw (pad, subbuf); } } else { gst_buffer_ref (buf); ret = gst_a52dec_chain_raw (pad, buf); } done: gst_buffer_unref (buf); return ret; /* ERRORS */ not_enough_data: { GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), STREAM, DECODE, (NULL), ("Insufficient data in buffer. Can't determine first_acess")); gst_buffer_unref (buf); return GST_FLOW_ERROR; } bad_first_access_parameter: { GST_ELEMENT_ERROR (GST_ELEMENT (a52dec), STREAM, DECODE, (NULL), ("Bad first_access parameter (%d) in buffer", first_access)); gst_buffer_unref (buf); return GST_FLOW_ERROR; } }
static gboolean gst_a52dec_sink_event (GstPad * pad, GstEvent * event) { GstA52Dec *a52dec = GST_A52DEC (gst_pad_get_parent (pad)); gboolean ret = FALSE; GST_LOG ("Handling %s event", GST_EVENT_TYPE_NAME (event)); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_NEWSEGMENT: { GstFormat fmt; gboolean update; gint64 start, end, pos; gdouble rate, arate; gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt, &start, &end, &pos); /* drain queued buffers before activating the segment so that we can clip * against the old segment first */ gst_a52dec_drain (a52dec); if (fmt != GST_FORMAT_TIME || !GST_CLOCK_TIME_IS_VALID (start)) { GST_WARNING ("No time in newsegment event %p (format is %s)", event, gst_format_get_name (fmt)); gst_event_unref (event); a52dec->sent_segment = FALSE; /* set some dummy values, FIXME: do proper conversion */ a52dec->time = start = pos = 0; fmt = GST_FORMAT_TIME; end = -1; } else { a52dec->time = start; a52dec->sent_segment = TRUE; GST_DEBUG_OBJECT (a52dec, "Pushing newseg rate %g, applied rate %g, format %d, start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", pos %" G_GINT64_FORMAT, rate, arate, fmt, start, end, time); ret = gst_pad_push_event (a52dec->srcpad, event); } gst_segment_set_newsegment (&a52dec->segment, update, rate, fmt, start, end, pos); break; } case GST_EVENT_TAG: ret = gst_pad_push_event (a52dec->srcpad, event); break; case GST_EVENT_EOS: gst_a52dec_drain (a52dec); ret = gst_pad_push_event (a52dec->srcpad, event); break; case GST_EVENT_FLUSH_START: ret = gst_pad_push_event (a52dec->srcpad, event); break; case GST_EVENT_FLUSH_STOP: if (a52dec->cache) { gst_buffer_unref (a52dec->cache); a52dec->cache = NULL; } clear_queued (a52dec); gst_segment_init (&a52dec->segment, GST_FORMAT_UNDEFINED); ret = gst_pad_push_event (a52dec->srcpad, event); break; default: ret = gst_pad_push_event (a52dec->srcpad, event); break; } gst_object_unref (a52dec); return ret; }