static GstFlowReturn gst_dtsdec_chain_raw (GstPad * pad, GstBuffer * buf) { GstDtsDec *dts; guint8 *data; gint size; gint length, flags, sample_rate, bit_rate, frame_length; GstFlowReturn result = GST_FLOW_OK; dts = GST_DTSDEC (GST_PAD_PARENT (pad)); if (dts->cache) { buf = gst_buffer_join (dts->cache, buf); dts->cache = NULL; } data = GST_BUFFER_DATA (buf); size = GST_BUFFER_SIZE (buf); length = 0; while (size >= 7) { length = dts_syncinfo (dts->state, data, &flags, &sample_rate, &bit_rate, &frame_length); if (length == 0) { /* shift window to re-find sync */ data++; size--; } else if (length <= size) { GST_DEBUG ("Sync: frame size %d", length); result = gst_dtsdec_handle_frame (dts, data, length, flags, sample_rate, bit_rate); if (result != GST_FLOW_OK) { size = 0; break; } size -= length; data += length; } else { GST_LOG ("Not enough data available (needed %d had %d)", length, size); break; } } /* keep cache */ if (length == 0) { GST_LOG ("No sync found"); } if (size > 0) { dts->cache = gst_buffer_create_sub (buf, GST_BUFFER_SIZE (buf) - size, size); } gst_buffer_unref (buf); return result; }
static GstStateChangeReturn gst_dtsdec_change_state (GstElement * element, GstStateChange transition) { GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; GstDtsDec *dts = GST_DTSDEC (element); switch (transition) { case GST_STATE_CHANGE_NULL_TO_READY:{ GstDtsDecClass *klass; klass = GST_DTSDEC_CLASS (G_OBJECT_GET_CLASS (dts)); dts->state = dts_init (klass->dts_cpuflags); break; } case GST_STATE_CHANGE_READY_TO_PAUSED: dts->samples = dts_samples (dts->state); dts->bit_rate = -1; dts->sample_rate = -1; dts->stream_channels = 0; /* FIXME force stereo for now */ dts->request_channels = DTS_CHANNEL; dts->using_channels = 0; dts->level = 1; dts->bias = 0; dts->current_ts = 0; 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: dts->samples = NULL; if (dts->cache) { gst_buffer_unref (dts->cache); dts->cache = NULL; } break; case GST_STATE_CHANGE_READY_TO_NULL: dts_free (dts->state); dts->state = NULL; break; default: break; } return ret; }
static void gst_dtsdec_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstDtsDec *dts = GST_DTSDEC (object); switch (prop_id) { case PROP_DRC: g_value_set_boolean (value, dts->dynamic_range_compression); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static gboolean gst_dtsdec_set_format (GstAudioDecoder * bdec, GstCaps * caps) { GstDtsDec *dts = GST_DTSDEC (bdec); GstStructure *structure; structure = gst_caps_get_structure (caps, 0); if (structure && gst_structure_has_name (structure, "audio/x-private1-dts")) dts->dvdmode = TRUE; else dts->dvdmode = FALSE; return TRUE; }
static gboolean gst_dtsdec_stop (GstAudioDecoder * dec) { GstDtsDec *dts = GST_DTSDEC (dec); GST_DEBUG_OBJECT (dec, "stop"); dts->samples = NULL; if (dts->state) { dca_free (dts->state); dts->state = NULL; } return TRUE; }
static gboolean gst_dtsdec_sink_setcaps (GstPad * pad, GstCaps * caps) { GstDtsDec *dts = GST_DTSDEC (gst_pad_get_parent (pad)); GstStructure *structure; structure = gst_caps_get_structure (caps, 0); if (structure && gst_structure_has_name (structure, "audio/x-private1-dts")) dts->dvdmode = TRUE; else dts->dvdmode = FALSE; gst_object_unref (dts); return TRUE; }
static GstFlowReturn gst_dtsdec_parse (GstAudioDecoder * bdec, GstAdapter * adapter, gint * _offset, gint * len) { GstDtsDec *dts; guint8 *data; gint av, size; gint length = 0, flags, sample_rate, bit_rate, frame_length; GstFlowReturn result = GST_FLOW_EOS; dts = GST_DTSDEC (bdec); size = av = gst_adapter_available (adapter); data = (guint8 *) gst_adapter_map (adapter, av); /* find and read header */ bit_rate = dts->bit_rate; sample_rate = dts->sample_rate; flags = 0; while (size >= 7) { length = dca_syncinfo (dts->state, data, &flags, &sample_rate, &bit_rate, &frame_length); if (length == 0) { /* shift window to re-find sync */ data++; size--; } else if (length <= size) { GST_LOG_OBJECT (dts, "Sync: frame size %d", length); result = GST_FLOW_OK; break; } else { GST_LOG_OBJECT (dts, "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_dtsdec_sink_event (GstPad * pad, GstEvent * event) { GstDtsDec *dtsdec = GST_DTSDEC (gst_pad_get_parent (pad)); gboolean ret = FALSE; GST_LOG_OBJECT (dtsdec, "%s event", GST_EVENT_TYPE_NAME (event)); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_NEWSEGMENT:{ GstFormat format; gint64 val; gst_event_parse_new_segment (event, NULL, NULL, &format, &val, NULL, NULL); if (format != GST_FORMAT_TIME || !GST_CLOCK_TIME_IS_VALID (val)) { GST_WARNING ("No time in newsegment event %p", event); } else { dtsdec->current_ts = val; } if (dtsdec->cache) { gst_buffer_unref (dtsdec->cache); dtsdec->cache = NULL; } ret = gst_pad_event_default (pad, event); break; } case GST_EVENT_FLUSH_STOP: if (dtsdec->cache) { gst_buffer_unref (dtsdec->cache); dtsdec->cache = NULL; } ret = gst_pad_event_default (pad, event); break; default: ret = gst_pad_event_default (pad, event); break; } gst_object_unref (dtsdec); return ret; }
static gboolean gst_dtsdec_start (GstAudioDecoder * dec) { GstDtsDec *dts = GST_DTSDEC (dec); GstDtsDecClass *klass; GST_DEBUG_OBJECT (dec, "start"); klass = GST_DTSDEC_CLASS (G_OBJECT_GET_CLASS (dts)); dts->state = dca_init (klass->dts_cpuflags); dts->samples = dca_samples (dts->state); dts->bit_rate = -1; dts->sample_rate = -1; dts->stream_channels = DCA_CHANNEL; dts->using_channels = DCA_CHANNEL; dts->level = 1; dts->bias = 0; dts->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_dtsdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) { GstFlowReturn ret = GST_FLOW_OK; GstDtsDec *dts = GST_DTSDEC (parent); gint first_access; if (dts->dvdmode) { guint8 data[2]; gsize size; gint offset, 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 = dts->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 = dts->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); ret = dts->base_chain (pad, parent, subbuf); gst_buffer_unref (buf); } } else { ret = dts->base_chain (pad, parent, buf); } done: return ret; /* ERRORS */ not_enough_data: { GST_ELEMENT_ERROR (GST_ELEMENT (dts), 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 (dts), STREAM, DECODE, (NULL), ("Bad first_access parameter (%d) in buffer", first_access)); gst_buffer_unref (buf); return GST_FLOW_ERROR; } }
static GstFlowReturn gst_dtsdec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buffer) { GstDtsDec *dts; gint channels, i, num_blocks; gboolean need_renegotiation = FALSE; guint8 *data; gsize size; GstMapInfo map; gint chans; gint length = 0, flags, sample_rate, bit_rate, frame_length; GstFlowReturn result = GST_FLOW_OK; GstBuffer *outbuf; dts = GST_DTSDEC (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); data = map.data; size = map.size; g_assert (size >= 7); bit_rate = dts->bit_rate; sample_rate = dts->sample_rate; flags = 0; length = dca_syncinfo (dts->state, data, &flags, &sample_rate, &bit_rate, &frame_length); g_assert (length == size); if (flags != dts->prev_flags) { dts->prev_flags = flags; dts->flag_update = TRUE; } /* go over stream properties, renegotiate or update streaminfo if needed */ if (dts->sample_rate != sample_rate) { need_renegotiation = TRUE; dts->sample_rate = sample_rate; } if (flags) { dts->stream_channels = flags & (DCA_CHANNEL_MASK | DCA_LFE); } if (bit_rate != dts->bit_rate) { dts->bit_rate = bit_rate; gst_dtsdec_update_streaminfo (dts); } /* 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. * FIXME: Add the property back in for forcing output channels. */ if (dts->request_channels != DCA_CHANNEL) { flags = dts->request_channels; } else if (dts->flag_update) { GstCaps *caps; dts->flag_update = FALSE; caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dts)); 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 channels; const int dts_channels[6] = { DCA_MONO, DCA_STEREO, DCA_STEREO | DCA_LFE, DCA_2F2R, DCA_2F2R | DCA_LFE, DCA_3F2R | DCA_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", flags ? gst_dtsdec_channels (flags, NULL) : 6); gst_structure_get_int (structure, "channels", &channels); if (channels <= 6) flags = dts_channels[channels - 1]; else flags = dts_channels[5]; gst_caps_unref (copy); } else if (flags) { flags = dts->stream_channels; } else { flags = DCA_3F2R | DCA_LFE; } if (caps) gst_caps_unref (caps); } else { flags = dts->using_channels; } /* process */ flags |= DCA_ADJUST_LEVEL; dts->level = 1; if (dca_frame (dts->state, data, &flags, &dts->level, dts->bias)) { gst_buffer_unmap (buffer, &map); GST_AUDIO_DECODER_ERROR (dts, 1, STREAM, DECODE, (NULL), ("dts_frame error"), result); goto exit; } gst_buffer_unmap (buffer, &map); channels = flags & (DCA_CHANNEL_MASK | DCA_LFE); if (dts->using_channels != channels) { need_renegotiation = TRUE; dts->using_channels = channels; } /* negotiate if required */ if (need_renegotiation) { GST_DEBUG_OBJECT (dts, "dtsdec: sample_rate:%d stream_chans:0x%x using_chans:0x%x", dts->sample_rate, dts->stream_channels, dts->using_channels); if (!gst_dtsdec_renegotiate (dts)) goto failed_negotiation; } if (dts->dynamic_range_compression == FALSE) { dca_dynrng (dts->state, NULL, NULL); } flags &= (DCA_CHANNEL_MASK | DCA_LFE); chans = gst_dtsdec_channels (flags, NULL); if (!chans) goto invalid_flags; /* handle decoded data, one block is 256 samples */ num_blocks = dca_blocks_num (dts->state); outbuf = gst_buffer_new_and_alloc (256 * chans * (SAMPLE_WIDTH / 8) * num_blocks); gst_buffer_map (outbuf, &map, GST_MAP_WRITE); data = map.data; size = map.size; { guint8 *ptr = data; for (i = 0; i < num_blocks; i++) { if (dca_block (dts->state)) { /* also marks discont */ GST_AUDIO_DECODER_ERROR (dts, 1, STREAM, DECODE, (NULL), ("error decoding block %d", i), result); if (result != GST_FLOW_OK) goto exit; } else { gint n, c; gint *reorder_map = dts->channel_reorder_map; for (n = 0; n < 256; n++) { for (c = 0; c < chans; c++) { ((sample_t *) ptr)[n * chans + reorder_map[c]] = dts->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 (dts, CORE, NEGOTIATION, (NULL), (NULL)); return GST_FLOW_ERROR; } invalid_flags: { GST_ELEMENT_ERROR (GST_ELEMENT (dts), STREAM, DECODE, (NULL), ("Invalid channel flags: %d", flags)); return GST_FLOW_ERROR; } }
static GstFlowReturn gst_dtsdec_chain (GstPad * pad, GstBuffer * buf) { GstFlowReturn res = GST_FLOW_OK; GstDtsDec *dts = GST_DTSDEC (GST_PAD_PARENT (pad)); gint first_access; if (dts->dvdmode) { gint size = GST_BUFFER_SIZE (buf); guint8 *data = GST_BUFFER_DATA (buf); gint offset, 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; res = gst_dtsdec_chain_raw (pad, subbuf); if (res != 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); res = gst_dtsdec_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); res = gst_dtsdec_chain_raw (pad, subbuf); } } else { res = gst_dtsdec_chain_raw (pad, buf); } done: return res; /* ERRORS */ not_enough_data: { GST_ELEMENT_ERROR (GST_ELEMENT (dts), STREAM, DECODE, (NULL), ("Insufficient data in buffer. Can't determine first_acess")); return GST_FLOW_ERROR; } bad_first_access_parameter: { GST_ELEMENT_ERROR (GST_ELEMENT (dts), STREAM, DECODE, (NULL), ("Bad first_access parameter (%d) in buffer", first_access)); return GST_FLOW_ERROR; } }