mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-27 20:21:24 +00:00
opusdec: rewrite logic
Parameters such as frame size, etc, are variable. Pretty much everything can change within a stream, so be prepared about it, and do not cache parameters in the decoder.
This commit is contained in:
parent
9e79a8ed01
commit
70ca2a6851
2 changed files with 53 additions and 87 deletions
|
@ -48,9 +48,6 @@
|
|||
GST_DEBUG_CATEGORY_STATIC (opusdec_debug);
|
||||
#define GST_CAT_DEFAULT opusdec_debug
|
||||
|
||||
#define DEC_MAX_FRAME_SIZE 2000
|
||||
#define DEC_MAX_OUTPUT_BUFFER_SIZE (5760)
|
||||
|
||||
static GstStaticPadTemplate opus_dec_src_factory =
|
||||
GST_STATIC_PAD_TEMPLATE ("src",
|
||||
GST_PAD_SRC,
|
||||
|
@ -110,14 +107,13 @@ static void
|
|||
gst_opus_dec_reset (GstOpusDec * dec)
|
||||
{
|
||||
dec->packetno = 0;
|
||||
dec->frame_size = 0;
|
||||
dec->frame_samples = 960;
|
||||
dec->frame_duration = 0;
|
||||
if (dec->state) {
|
||||
opus_decoder_destroy (dec->state);
|
||||
dec->state = NULL;
|
||||
}
|
||||
|
||||
dec->next_ts = 0;
|
||||
|
||||
gst_buffer_replace (&dec->streamheader, NULL);
|
||||
gst_buffer_replace (&dec->vorbiscomment, NULL);
|
||||
}
|
||||
|
@ -176,7 +172,7 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
|
|||
GstBuffer *outbuf;
|
||||
gint16 *out_data;
|
||||
int n, err;
|
||||
int samples_per_frame;
|
||||
int samples;
|
||||
unsigned int packet_size;
|
||||
|
||||
if (dec->state == NULL) {
|
||||
|
@ -192,8 +188,8 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
|
|||
"rate", G_TYPE_INT, dec->sample_rate,
|
||||
"channels", G_TYPE_INT, dec->n_channels, NULL);
|
||||
|
||||
GST_DEBUG_OBJECT (dec, "rate=%d channels=%d frame-size=%d",
|
||||
dec->sample_rate, dec->n_channels, dec->frame_size);
|
||||
GST_DEBUG_OBJECT (dec, "rate=%d channels=%d",
|
||||
dec->sample_rate, dec->n_channels);
|
||||
|
||||
if (!gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps))
|
||||
GST_ERROR ("nego failure");
|
||||
|
@ -214,12 +210,13 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
|
|||
size = 0;
|
||||
}
|
||||
|
||||
samples_per_frame =
|
||||
opus_packet_get_samples_per_frame (data, dec->sample_rate);
|
||||
samples =
|
||||
opus_packet_get_samples_per_frame (data,
|
||||
dec->sample_rate) * opus_packet_get_nb_frames (data, size);
|
||||
GST_DEBUG ("bandwidth %d", opus_packet_get_bandwidth (data));
|
||||
GST_DEBUG ("samples_per_frame %d", samples_per_frame);
|
||||
GST_DEBUG ("samples %d", samples);
|
||||
|
||||
packet_size = samples_per_frame * dec->n_channels * 2;
|
||||
packet_size = samples * dec->n_channels * 2;
|
||||
outbuf = gst_buffer_new_and_alloc (packet_size);
|
||||
if (!outbuf) {
|
||||
goto buffer_failed;
|
||||
|
@ -227,34 +224,33 @@ opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buf,
|
|||
|
||||
out_data = (gint16 *) gst_buffer_map (outbuf, &out_size, NULL, GST_MAP_WRITE);
|
||||
|
||||
GST_LOG_OBJECT (dec, "decoding frame");
|
||||
GST_LOG_OBJECT (dec, "decoding %d samples, in size %u", samples, size);
|
||||
|
||||
n = opus_decode (dec->state, data, size, out_data, dec->frame_samples, 0);
|
||||
n = opus_decode (dec->state, data, size, out_data, samples, 0);
|
||||
gst_buffer_unmap (buf, data, size);
|
||||
gst_buffer_unmap (outbuf, out_data, out_size);
|
||||
if (n < 0) {
|
||||
gst_buffer_unmap (outbuf, out_data, out_size);
|
||||
GST_ELEMENT_ERROR (dec, STREAM, DECODE, ("Decoding error: %d", n), (NULL));
|
||||
return GST_FLOW_ERROR;
|
||||
}
|
||||
GST_DEBUG_OBJECT (dec, "decoded %d samples", n);
|
||||
|
||||
if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
|
||||
GST_WARNING_OBJECT (dec, "No timestamp in -> no timestamp out");
|
||||
if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
|
||||
GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
|
||||
} else {
|
||||
GST_BUFFER_TIMESTAMP (outbuf) = dec->next_ts;
|
||||
}
|
||||
|
||||
GST_DEBUG_OBJECT (dec, "timestamp=%" GST_TIME_FORMAT,
|
||||
GST_TIME_ARGS (timestamp));
|
||||
|
||||
GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
|
||||
GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
|
||||
GST_BUFFER_DURATION (outbuf) =
|
||||
gst_util_uint64_scale (n, GST_SECOND, dec->sample_rate);
|
||||
dec->next_ts = GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf);
|
||||
|
||||
GST_LOG_OBJECT (dec, "pushing buffer with ts=%" GST_TIME_FORMAT ", dur=%"
|
||||
GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
|
||||
GST_TIME_ARGS (dec->frame_duration));
|
||||
GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
|
||||
|
||||
res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
|
||||
|
||||
gst_buffer_unmap (outbuf, out_data, out_size);
|
||||
|
||||
if (res != GST_FLOW_OK)
|
||||
GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
|
||||
|
||||
|
@ -269,37 +265,6 @@ buffer_failed:
|
|||
return GST_FLOW_ERROR;
|
||||
}
|
||||
|
||||
static gint
|
||||
gst_opus_dec_get_frame_samples (GstOpusDec * dec)
|
||||
{
|
||||
gint frame_samples = 0;
|
||||
switch (dec->frame_size) {
|
||||
case 2:
|
||||
frame_samples = dec->sample_rate / 400;
|
||||
break;
|
||||
case 5:
|
||||
frame_samples = dec->sample_rate / 200;
|
||||
break;
|
||||
case 10:
|
||||
frame_samples = dec->sample_rate / 100;
|
||||
break;
|
||||
case 20:
|
||||
frame_samples = dec->sample_rate / 50;
|
||||
break;
|
||||
case 40:
|
||||
frame_samples = dec->sample_rate / 25;
|
||||
break;
|
||||
case 60:
|
||||
frame_samples = 3 * dec->sample_rate / 50;
|
||||
break;
|
||||
default:
|
||||
GST_WARNING_OBJECT (dec, "Unsupported frame size: %d", dec->frame_size);
|
||||
frame_samples = 0;
|
||||
break;
|
||||
}
|
||||
return frame_samples;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_opus_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
|
||||
{
|
||||
|
@ -337,25 +302,6 @@ gst_opus_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
|
|||
}
|
||||
}
|
||||
|
||||
if (!gst_structure_get_int (s, "frame-size", &dec->frame_size)) {
|
||||
GST_WARNING_OBJECT (dec, "Frame size not included in caps");
|
||||
}
|
||||
if (!gst_structure_get_int (s, "channels", &dec->n_channels)) {
|
||||
GST_WARNING_OBJECT (dec, "Number of channels not included in caps");
|
||||
}
|
||||
if (!gst_structure_get_int (s, "rate", &dec->sample_rate)) {
|
||||
GST_WARNING_OBJECT (dec, "Sample rate not included in caps");
|
||||
}
|
||||
|
||||
dec->frame_samples = gst_opus_dec_get_frame_samples (dec);
|
||||
dec->frame_duration = gst_util_uint64_scale_int (dec->frame_samples,
|
||||
GST_SECOND, dec->sample_rate);
|
||||
|
||||
GST_INFO_OBJECT (dec,
|
||||
"Got frame size %d, %d channels, %d Hz, giving %d samples per frame, frame duration %"
|
||||
GST_TIME_FORMAT, dec->frame_size, dec->n_channels, dec->sample_rate,
|
||||
dec->frame_samples, GST_TIME_ARGS (dec->frame_duration));
|
||||
|
||||
caps = gst_caps_new_simple ("audio/x-raw",
|
||||
"format", G_TYPE_STRING, "S16LE",
|
||||
"rate", G_TYPE_INT, dec->sample_rate,
|
||||
|
@ -387,6 +333,20 @@ memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
|
|||
return res;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_opus_dec_is_header (GstBuffer * buf, const char *magic, guint magic_size)
|
||||
{
|
||||
guint8 *data;
|
||||
gsize size;
|
||||
gboolean ret;
|
||||
|
||||
data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
|
||||
if (!data)
|
||||
return FALSE;
|
||||
ret = (size >= magic_size && !memcmp (magic, data, magic_size));
|
||||
gst_buffer_unmap (buf, data, size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static GstFlowReturn
|
||||
gst_opus_dec_handle_frame (GstAudioDecoder * adec, GstBuffer * buf)
|
||||
|
@ -424,16 +384,24 @@ gst_opus_dec_handle_frame (GstAudioDecoder * adec, GstBuffer * buf)
|
|||
* first two packets are the headers. */
|
||||
switch (dec->packetno) {
|
||||
case 0:
|
||||
GST_DEBUG_OBJECT (dec, "counted streamheader");
|
||||
res = GST_FLOW_OK;
|
||||
res = gst_opus_dec_parse_header (dec, buf);
|
||||
gst_audio_decoder_finish_frame (adec, NULL, 1);
|
||||
if (gst_opus_dec_is_header (buf, "OpusHead", 8)) {
|
||||
GST_DEBUG_OBJECT (dec, "found streamheader");
|
||||
res = gst_opus_dec_parse_header (dec, buf);
|
||||
gst_audio_decoder_finish_frame (adec, NULL, 1);
|
||||
} else {
|
||||
res = opus_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
|
||||
GST_BUFFER_DURATION (buf));
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
|
||||
res = GST_FLOW_OK;
|
||||
res = gst_opus_dec_parse_comments (dec, buf);
|
||||
gst_audio_decoder_finish_frame (adec, NULL, 1);
|
||||
if (gst_opus_dec_is_header (buf, "OpusTags", 8)) {
|
||||
GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
|
||||
res = gst_opus_dec_parse_comments (dec, buf);
|
||||
gst_audio_decoder_finish_frame (adec, NULL, 1);
|
||||
} else {
|
||||
res = opus_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
|
||||
GST_BUFFER_DURATION (buf));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
|
|
|
@ -45,11 +45,9 @@ struct _GstOpusDec {
|
|||
GstAudioDecoder element;
|
||||
|
||||
OpusDecoder *state;
|
||||
int frame_samples;
|
||||
|
||||
gint frame_size;
|
||||
GstClockTime frame_duration;
|
||||
guint64 packetno;
|
||||
GstClockTime next_ts;
|
||||
|
||||
GstBuffer *streamheader;
|
||||
GstBuffer *vorbiscomment;
|
||||
|
|
Loading…
Reference in a new issue