static GstFlowReturn gst_opus_dec_parse_header (GstOpusDec * dec, GstBuffer * buf) { const guint8 *data; GstAudioChannelPosition pos[64]; const GstAudioChannelPosition *posn = NULL; GstMapInfo map; if (!gst_opus_header_is_id_header (buf)) { GST_ERROR_OBJECT (dec, "Header is not an Opus ID header"); return GST_FLOW_ERROR; } gst_buffer_map (buf, &map, GST_MAP_READ); data = map.data; if (!(dec->n_channels == 0 || dec->n_channels == data[9])) { gst_buffer_unmap (buf, &map); GST_ERROR_OBJECT (dec, "Opus ID header has invalid channels"); return GST_FLOW_ERROR; } dec->n_channels = data[9]; dec->sample_rate = GST_READ_UINT32_LE (data + 12); dec->pre_skip = GST_READ_UINT16_LE (data + 10); dec->r128_gain = GST_READ_UINT16_LE (data + 16); dec->r128_gain_volume = gst_opus_dec_get_r128_volume (dec->r128_gain); GST_INFO_OBJECT (dec, "Found pre-skip of %u samples, R128 gain %d (volume %f)", dec->pre_skip, dec->r128_gain, dec->r128_gain_volume); dec->channel_mapping_family = data[18]; if (dec->channel_mapping_family == 0) { /* implicit mapping */ GST_INFO_OBJECT (dec, "Channel mapping family 0, implicit mapping"); dec->n_streams = dec->n_stereo_streams = 1; dec->channel_mapping[0] = 0; dec->channel_mapping[1] = 1; } else { dec->n_streams = data[19]; dec->n_stereo_streams = data[20]; memcpy (dec->channel_mapping, data + 21, dec->n_channels); if (dec->channel_mapping_family == 1) { GST_INFO_OBJECT (dec, "Channel mapping family 1, Vorbis mapping"); switch (dec->n_channels) { case 1: case 2: /* nothing */ break; case 3: case 4: case 5: case 6: case 7: case 8: posn = gst_opus_channel_positions[dec->n_channels - 1]; break; default:{ gint i; GST_ELEMENT_WARNING (GST_ELEMENT (dec), STREAM, DECODE, (NULL), ("Using NONE channel layout for more than 8 channels")); for (i = 0; i < dec->n_channels; i++) pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE; posn = pos; } } } else { GST_INFO_OBJECT (dec, "Channel mapping family %d", dec->channel_mapping_family); } } gst_opus_dec_negotiate (dec, posn); gst_buffer_unmap (buf, &map); return GST_FLOW_OK; }
static GstFlowReturn gst_opus_dec_parse_header (GstOpusDec * dec, GstBuffer * buf) { GstAudioChannelPosition pos[64]; const GstAudioChannelPosition *posn = NULL; if (!gst_opus_header_is_id_header (buf)) { GST_ELEMENT_ERROR (dec, STREAM, FORMAT, (NULL), ("Header is not an Opus ID header")); return GST_FLOW_ERROR; } if (!gst_codec_utils_opus_parse_header (buf, &dec->sample_rate, &dec->n_channels, &dec->channel_mapping_family, &dec->n_streams, &dec->n_stereo_streams, dec->channel_mapping, &dec->pre_skip, &dec->r128_gain)) { GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("Failed to parse Opus ID header")); return GST_FLOW_ERROR; } dec->r128_gain_volume = gst_opus_dec_get_r128_volume (dec->r128_gain); GST_INFO_OBJECT (dec, "Found pre-skip of %u samples, R128 gain %d (volume %f)", dec->pre_skip, dec->r128_gain, dec->r128_gain_volume); if (dec->channel_mapping_family == 1) { GST_INFO_OBJECT (dec, "Channel mapping family 1, Vorbis mapping"); switch (dec->n_channels) { case 1: case 2: /* nothing */ break; case 3: case 4: case 5: case 6: case 7: case 8: posn = gst_opus_channel_positions[dec->n_channels - 1]; break; default:{ gint i; GST_ELEMENT_WARNING (GST_ELEMENT (dec), STREAM, DECODE, (NULL), ("Using NONE channel layout for more than 8 channels")); for (i = 0; i < dec->n_channels; i++) pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE; posn = pos; } } } else { GST_INFO_OBJECT (dec, "Channel mapping family %d", dec->channel_mapping_family); } if (!gst_opus_dec_negotiate (dec, posn)) return GST_FLOW_NOT_NEGOTIATED; return GST_FLOW_OK; }
static GstFlowReturn gst_opus_parse_handle_frame (GstBaseParse * base, GstBaseParseFrame * frame, gint * skip) { GstOpusParse *parse; guint8 *data; gsize size; guint32 packet_size; int ret = FALSE; const unsigned char *frames[48]; unsigned char toc; short frame_sizes[48]; int payload_offset; int packet_offset = 0; gboolean is_header, is_idheader, is_commentheader; GstMapInfo map; parse = GST_OPUS_PARSE (base); *skip = -1; gst_buffer_map (frame->buffer, &map, GST_MAP_READ); data = map.data; size = map.size; GST_DEBUG_OBJECT (parse, "Checking for frame, %" G_GSIZE_FORMAT " bytes in buffer", size); /* check for headers */ is_idheader = gst_opus_header_is_id_header (frame->buffer); is_commentheader = gst_opus_header_is_comment_header (frame->buffer); is_header = is_idheader || is_commentheader; if (!is_header) { int nframes; /* Next, check if there's an Opus packet there */ nframes = opus_packet_parse (data, size, &toc, frames, frame_sizes, &payload_offset); if (nframes < 0) { /* Then, check for the test vector framing */ GST_DEBUG_OBJECT (parse, "No Opus packet found, trying test vector framing"); if (size < 4) { GST_DEBUG_OBJECT (parse, "Too small"); goto beach; } packet_size = GST_READ_UINT32_BE (data); GST_DEBUG_OBJECT (parse, "Packet size: %u bytes", packet_size); if (packet_size > MAX_PAYLOAD_BYTES) { GST_DEBUG_OBJECT (parse, "Too large"); goto beach; } if (packet_size > size - 4) { GST_DEBUG_OBJECT (parse, "Truncated"); goto beach; } nframes = opus_packet_parse (data + 8, packet_size, &toc, frames, frame_sizes, &payload_offset); if (nframes < 0) { GST_DEBUG_OBJECT (parse, "No test vector framing either"); goto beach; } packet_offset = 8; /* for ad hoc framing, heed the framing, so we eat any padding */ payload_offset = packet_size; } else { /* Add up all the frame sizes found */ int f; for (f = 0; f < nframes; ++f) payload_offset += frame_sizes[f]; } } if (is_header) { *skip = 0; } else { *skip = packet_offset; size = payload_offset; } GST_DEBUG_OBJECT (parse, "Got Opus packet at offset %d, %" G_GSIZE_FORMAT " bytes", *skip, size); ret = TRUE; beach: gst_buffer_unmap (frame->buffer, &map); /* convert old style result to new one */ if (!ret) { if (*skip < 0) *skip = 1; return GST_FLOW_OK; } /* always skip first if needed */ if (*skip > 0) return GST_FLOW_OK; /* normalize again */ if (*skip < 0) *skip = 0; /* not enough */ if (size > map.size) return GST_FLOW_OK; /* FIXME some day ... should not mess with buffer itself */ if (!parse->got_headers) { gst_buffer_replace (&frame->buffer, gst_buffer_copy_region (frame->buffer, GST_BUFFER_COPY_ALL, 0, size)); gst_buffer_unref (frame->buffer); } ret = gst_opus_parse_parse_frame (base, frame); if (ret == GST_BASE_PARSE_FLOW_DROPPED) { frame->flags |= GST_BASE_PARSE_FRAME_FLAG_DROP; ret = GST_FLOW_OK; } if (ret == GST_FLOW_OK) ret = gst_base_parse_finish_frame (base, frame, size); return ret; }
static GstFlowReturn gst_opus_parse_parse_frame (GstBaseParse * base, GstBaseParseFrame * frame) { guint64 duration; GstOpusParse *parse; gboolean is_idheader, is_commentheader; GstMapInfo map; GstAudioClippingMeta *cmeta = gst_buffer_get_audio_clipping_meta (frame->buffer); parse = GST_OPUS_PARSE (base); g_assert (!cmeta || cmeta->format == GST_FORMAT_DEFAULT); is_idheader = gst_opus_header_is_id_header (frame->buffer); is_commentheader = gst_opus_header_is_comment_header (frame->buffer); if (!parse->got_headers || !parse->header_sent) { GstCaps *caps; /* Opus streams can decode to 1 or 2 channels, so use the header value if we have one, or 2 otherwise */ if (is_idheader) { gst_buffer_replace (&parse->id_header, frame->buffer); GST_DEBUG_OBJECT (parse, "Found ID header, keeping"); return GST_BASE_PARSE_FLOW_DROPPED; } else if (is_commentheader) { gst_buffer_replace (&parse->comment_header, frame->buffer); GST_DEBUG_OBJECT (parse, "Found comment header, keeping"); return GST_BASE_PARSE_FLOW_DROPPED; } parse->got_headers = TRUE; if (cmeta && cmeta->start) { parse->pre_skip += cmeta->start; gst_buffer_map (frame->buffer, &map, GST_MAP_READ); duration = packet_duration_opus (map.data, map.size); gst_buffer_unmap (frame->buffer, &map); /* Queue frame for later once we know all initial padding */ if (duration == cmeta->start) { frame->flags |= GST_BASE_PARSE_FRAME_FLAG_QUEUE; } } if (!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_QUEUE)) { if (FALSE && parse->id_header && parse->comment_header) { guint16 pre_skip; gst_buffer_map (parse->id_header, &map, GST_MAP_READWRITE); pre_skip = GST_READ_UINT16_LE (map.data + 10); if (pre_skip != parse->pre_skip) { GST_DEBUG_OBJECT (parse, "Fixing up pre-skip %u -> %" G_GUINT64_FORMAT, pre_skip, parse->pre_skip); GST_WRITE_UINT16_LE (map.data + 10, parse->pre_skip); } gst_buffer_unmap (parse->id_header, &map); caps = gst_codec_utils_opus_create_caps_from_header (parse->id_header, parse->comment_header); } else { GstCaps *sink_caps; guint32 sample_rate = 48000; guint8 n_channels, n_streams, n_stereo_streams, channel_mapping_family; guint8 channel_mapping[256]; GstBuffer *id_header; sink_caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (parse)); if (!sink_caps || !gst_codec_utils_opus_parse_caps (sink_caps, &sample_rate, &n_channels, &channel_mapping_family, &n_streams, &n_stereo_streams, channel_mapping)) { GST_INFO_OBJECT (parse, "No headers and no caps, blindly setting up canonical stereo"); n_channels = 2; n_streams = 1; n_stereo_streams = 1; channel_mapping_family = 0; channel_mapping[0] = 0; channel_mapping[1] = 1; } if (sink_caps) gst_caps_unref (sink_caps); id_header = gst_codec_utils_opus_create_header (sample_rate, n_channels, channel_mapping_family, n_streams, n_stereo_streams, channel_mapping, parse->pre_skip, 0); caps = gst_codec_utils_opus_create_caps_from_header (id_header, NULL); gst_buffer_unref (id_header); } gst_buffer_replace (&parse->id_header, NULL); gst_buffer_replace (&parse->comment_header, NULL); gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps); gst_caps_unref (caps); parse->header_sent = TRUE; } } GST_BUFFER_TIMESTAMP (frame->buffer) = parse->next_ts; gst_buffer_map (frame->buffer, &map, GST_MAP_READ); duration = packet_duration_opus (map.data, map.size); gst_buffer_unmap (frame->buffer, &map); parse->next_ts += duration; GST_BUFFER_DURATION (frame->buffer) = duration; GST_BUFFER_OFFSET_END (frame->buffer) = gst_util_uint64_scale (parse->next_ts, 48000, GST_SECOND); GST_BUFFER_OFFSET (frame->buffer) = parse->next_ts; return GST_FLOW_OK; }
static GstFlowReturn gst_opus_dec_parse_header (GstOpusDec * dec, GstBuffer * buf) { const guint8 *data = GST_BUFFER_DATA (buf); GstCaps *caps; const GstAudioChannelPosition *pos = NULL; if (!gst_opus_header_is_id_header (buf)) { GST_ERROR_OBJECT (dec, "Header is not an Opus ID header"); return GST_FLOW_ERROR; } if (!(dec->n_channels == 0 || dec->n_channels == data[9])) { GST_ERROR_OBJECT (dec, "Opus ID header has invalid channels"); return GST_FLOW_ERROR; } dec->n_channels = data[9]; dec->pre_skip = GST_READ_UINT16_LE (data + 10); dec->r128_gain = GST_READ_UINT16_LE (data + 16); dec->r128_gain_volume = gst_opus_dec_get_r128_volume (dec->r128_gain); GST_INFO_OBJECT (dec, "Found pre-skip of %u samples, R128 gain %d (volume %f)", dec->pre_skip, dec->r128_gain, dec->r128_gain_volume); dec->channel_mapping_family = data[18]; if (dec->channel_mapping_family == 0) { /* implicit mapping */ GST_INFO_OBJECT (dec, "Channel mapping family 0, implicit mapping"); dec->n_streams = dec->n_stereo_streams = 1; dec->channel_mapping[0] = 0; dec->channel_mapping[1] = 1; } else { dec->n_streams = data[19]; dec->n_stereo_streams = data[20]; memcpy (dec->channel_mapping, data + 21, dec->n_channels); if (dec->channel_mapping_family == 1) { GST_INFO_OBJECT (dec, "Channel mapping family 1, Vorbis mapping"); switch (dec->n_channels) { case 1: case 2: /* nothing */ break; case 3: case 4: case 5: case 6: case 7: case 8: pos = gst_opus_channel_positions[dec->n_channels - 1]; break; default:{ gint i; GstAudioChannelPosition *posn = g_new (GstAudioChannelPosition, dec->n_channels); GST_ELEMENT_WARNING (GST_ELEMENT (dec), STREAM, DECODE, (NULL), ("Using NONE channel layout for more than 8 channels")); for (i = 0; i < dec->n_channels; i++) posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE; pos = posn; } } } else { GST_INFO_OBJECT (dec, "Channel mapping family %d", dec->channel_mapping_family); } } caps = gst_opus_dec_negotiate (dec); if (pos) { GST_DEBUG_OBJECT (dec, "Setting channel positions on caps"); gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos); } if (dec->n_channels > 8) { g_free ((GstAudioChannelPosition *) pos); } GST_INFO_OBJECT (dec, "Setting src caps to %" GST_PTR_FORMAT, caps); gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps); gst_caps_unref (caps); return GST_FLOW_OK; }