2011-11-20 09:52:46 +00:00
|
|
|
/* GStreamer Opus Encoder
|
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* Copyright (C) <2008> Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
|
|
|
* Copyright (C) <2011> Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-11-20 09:52:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
#include <gst/tag/tag.h>
|
|
|
|
#include <gst/base/gstbytewriter.h>
|
|
|
|
#include "gstopusheader.h"
|
|
|
|
|
|
|
|
static GstBuffer *
|
2011-12-08 18:45:27 +00:00
|
|
|
gst_opus_enc_create_id_buffer (gint nchannels, gint n_stereo_streams,
|
|
|
|
gint sample_rate, guint8 channel_mapping_family,
|
|
|
|
const guint8 * channel_mapping)
|
2011-11-20 09:52:46 +00:00
|
|
|
{
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstByteWriter bw;
|
2012-04-04 09:51:28 +00:00
|
|
|
gboolean hdl = TRUE;
|
2011-11-20 09:52:46 +00:00
|
|
|
|
2011-12-08 18:45:27 +00:00
|
|
|
g_return_val_if_fail (nchannels > 0 && nchannels < 256, NULL);
|
|
|
|
g_return_val_if_fail (n_stereo_streams >= 0, NULL);
|
|
|
|
g_return_val_if_fail (n_stereo_streams <= nchannels - n_stereo_streams, NULL);
|
|
|
|
|
2011-11-20 09:52:46 +00:00
|
|
|
gst_byte_writer_init (&bw);
|
|
|
|
|
|
|
|
/* See http://wiki.xiph.org/OggOpus */
|
2012-04-04 09:51:28 +00:00
|
|
|
hdl &= gst_byte_writer_put_data (&bw, (const guint8 *) "OpusHead", 8);
|
2012-05-24 20:58:44 +00:00
|
|
|
hdl &= gst_byte_writer_put_uint8 (&bw, 0x01); /* version number */
|
2012-04-04 09:51:28 +00:00
|
|
|
hdl &= gst_byte_writer_put_uint8 (&bw, nchannels);
|
|
|
|
hdl &= gst_byte_writer_put_uint16_le (&bw, 0); /* pre-skip */
|
|
|
|
hdl &= gst_byte_writer_put_uint32_le (&bw, sample_rate);
|
|
|
|
hdl &= gst_byte_writer_put_uint16_le (&bw, 0); /* output gain */
|
|
|
|
hdl &= gst_byte_writer_put_uint8 (&bw, channel_mapping_family);
|
2011-11-24 13:29:56 +00:00
|
|
|
if (channel_mapping_family > 0) {
|
2012-04-04 09:51:28 +00:00
|
|
|
hdl &= gst_byte_writer_put_uint8 (&bw, nchannels - n_stereo_streams);
|
|
|
|
hdl &= gst_byte_writer_put_uint8 (&bw, n_stereo_streams);
|
|
|
|
hdl &= gst_byte_writer_put_data (&bw, channel_mapping, nchannels);
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
2011-11-20 09:52:46 +00:00
|
|
|
|
2012-04-04 09:51:28 +00:00
|
|
|
if (!hdl)
|
|
|
|
GST_WARNING ("Error creating header");
|
|
|
|
|
2011-11-20 09:52:46 +00:00
|
|
|
buffer = gst_byte_writer_reset_and_get_buffer (&bw);
|
|
|
|
|
|
|
|
GST_BUFFER_OFFSET (buffer) = 0;
|
|
|
|
GST_BUFFER_OFFSET_END (buffer) = 0;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBuffer *
|
|
|
|
gst_opus_enc_create_metadata_buffer (const GstTagList * tags)
|
|
|
|
{
|
|
|
|
GstTagList *empty_tags = NULL;
|
|
|
|
GstBuffer *comments = NULL;
|
|
|
|
|
|
|
|
GST_DEBUG ("tags = %" GST_PTR_FORMAT, tags);
|
|
|
|
|
|
|
|
if (tags == NULL) {
|
|
|
|
/* FIXME: better fix chain of callers to not write metadata at all,
|
|
|
|
* if there is none */
|
2011-11-25 11:48:58 +00:00
|
|
|
empty_tags = gst_tag_list_new_empty ();
|
2011-11-20 09:52:46 +00:00
|
|
|
tags = empty_tags;
|
|
|
|
}
|
|
|
|
comments =
|
|
|
|
gst_tag_list_to_vorbiscomment_buffer (tags, (const guint8 *) "OpusTags",
|
|
|
|
8, "Encoded with GStreamer Opusenc");
|
|
|
|
|
|
|
|
GST_BUFFER_OFFSET (comments) = 0;
|
|
|
|
GST_BUFFER_OFFSET_END (comments) = 0;
|
|
|
|
|
|
|
|
if (empty_tags)
|
2012-08-04 15:31:30 +00:00
|
|
|
gst_tag_list_unref (empty_tags);
|
2011-11-20 09:52:46 +00:00
|
|
|
|
|
|
|
return comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (really really) FIXME: move into core (dixit tpm)
|
|
|
|
*/
|
2014-08-09 12:24:59 +00:00
|
|
|
/*
|
2011-11-20 09:52:46 +00:00
|
|
|
* _gst_caps_set_buffer_array:
|
2014-08-09 12:24:59 +00:00
|
|
|
* @caps: (transfer full): a #GstCaps
|
2011-11-20 09:52:46 +00:00
|
|
|
* @field: field in caps to set
|
|
|
|
* @buf: header buffers
|
|
|
|
*
|
|
|
|
* Adds given buffers to an array of buffers set as the given @field
|
|
|
|
* on the given @caps. List of buffer arguments must be NULL-terminated.
|
|
|
|
*
|
2014-08-09 12:24:59 +00:00
|
|
|
* Returns: (transfer full): input caps with a streamheader field added, or NULL
|
|
|
|
* if some error occurred
|
2011-11-20 09:52:46 +00:00
|
|
|
*/
|
|
|
|
static GstCaps *
|
|
|
|
_gst_caps_set_buffer_array (GstCaps * caps, const gchar * field,
|
|
|
|
GstBuffer * buf, ...)
|
|
|
|
{
|
|
|
|
GstStructure *structure = NULL;
|
|
|
|
va_list va;
|
|
|
|
GValue array = { 0 };
|
|
|
|
GValue value = { 0 };
|
|
|
|
|
|
|
|
g_return_val_if_fail (caps != NULL, NULL);
|
|
|
|
g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
|
|
|
|
g_return_val_if_fail (field != NULL, NULL);
|
|
|
|
|
|
|
|
caps = gst_caps_make_writable (caps);
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
|
|
g_value_init (&array, GST_TYPE_ARRAY);
|
|
|
|
|
|
|
|
va_start (va, buf);
|
|
|
|
/* put buffers in a fixed list */
|
|
|
|
while (buf) {
|
|
|
|
g_assert (gst_buffer_is_writable (buf));
|
|
|
|
|
|
|
|
/* mark buffer */
|
2012-03-07 16:14:29 +00:00
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
|
2011-11-20 09:52:46 +00:00
|
|
|
|
|
|
|
g_value_init (&value, GST_TYPE_BUFFER);
|
|
|
|
buf = gst_buffer_copy (buf);
|
2012-03-07 16:14:29 +00:00
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
|
2011-11-20 09:52:46 +00:00
|
|
|
gst_value_set_buffer (&value, buf);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
gst_value_array_append_value (&array, &value);
|
|
|
|
g_value_unset (&value);
|
|
|
|
|
|
|
|
buf = va_arg (va, GstBuffer *);
|
|
|
|
}
|
2014-04-09 10:02:00 +00:00
|
|
|
va_end (va);
|
2011-11-20 09:52:46 +00:00
|
|
|
|
|
|
|
gst_structure_set_value (structure, field, &array);
|
|
|
|
g_value_unset (&array);
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
void
|
|
|
|
gst_opus_header_create_caps_from_headers (GstCaps ** caps, GSList ** headers,
|
|
|
|
GstBuffer * buf1, GstBuffer * buf2)
|
|
|
|
{
|
2011-12-08 19:47:55 +00:00
|
|
|
int n_streams, family;
|
2011-12-09 17:25:41 +00:00
|
|
|
gboolean multistream;
|
2012-03-07 16:14:29 +00:00
|
|
|
GstMapInfo map;
|
2011-12-30 10:41:17 +00:00
|
|
|
guint8 *data;
|
2011-12-08 19:47:55 +00:00
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
g_return_if_fail (caps);
|
|
|
|
g_return_if_fail (headers && !*headers);
|
2011-12-30 10:41:17 +00:00
|
|
|
g_return_if_fail (gst_buffer_get_size (buf1) >= 19);
|
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_map (buf1, &map, GST_MAP_READ);
|
|
|
|
data = map.data;
|
2011-12-08 19:47:55 +00:00
|
|
|
|
|
|
|
/* work out the number of streams */
|
2011-12-30 10:41:17 +00:00
|
|
|
family = data[18];
|
2011-12-08 19:47:55 +00:00
|
|
|
if (family == 0) {
|
|
|
|
n_streams = 1;
|
|
|
|
} else {
|
|
|
|
/* only included in the header for family > 0 */
|
2012-03-07 16:14:29 +00:00
|
|
|
if (map.size >= 20)
|
2011-12-30 10:41:17 +00:00
|
|
|
n_streams = data[19];
|
|
|
|
else {
|
|
|
|
g_warning ("family > 0 but header buffer size < 20");
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_unmap (buf1, &map);
|
2011-12-30 10:41:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-12-08 19:47:55 +00:00
|
|
|
}
|
2011-11-24 13:29:56 +00:00
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_unmap (buf1, &map);
|
2011-11-24 13:29:56 +00:00
|
|
|
|
|
|
|
/* mark and put on caps */
|
2011-12-09 17:25:41 +00:00
|
|
|
multistream = n_streams > 1;
|
|
|
|
*caps = gst_caps_new_simple ("audio/x-opus",
|
|
|
|
"multistream", G_TYPE_BOOLEAN, multistream, NULL);
|
2011-11-24 13:29:56 +00:00
|
|
|
*caps = _gst_caps_set_buffer_array (*caps, "streamheader", buf1, buf2, NULL);
|
|
|
|
|
2014-07-10 14:52:46 +00:00
|
|
|
*headers = g_slist_prepend (*headers, gst_buffer_ref (buf2));
|
|
|
|
*headers = g_slist_prepend (*headers, gst_buffer_ref (buf1));
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
|
|
|
|
2011-11-20 09:52:46 +00:00
|
|
|
void
|
|
|
|
gst_opus_header_create_caps (GstCaps ** caps, GSList ** headers, gint nchannels,
|
2011-12-08 18:45:27 +00:00
|
|
|
gint n_stereo_streams, gint sample_rate, guint8 channel_mapping_family,
|
2011-11-24 13:29:56 +00:00
|
|
|
const guint8 * channel_mapping, const GstTagList * tags)
|
2011-11-20 09:52:46 +00:00
|
|
|
{
|
|
|
|
GstBuffer *buf1, *buf2;
|
|
|
|
|
|
|
|
g_return_if_fail (caps);
|
|
|
|
g_return_if_fail (headers && !*headers);
|
|
|
|
g_return_if_fail (nchannels > 0);
|
|
|
|
g_return_if_fail (sample_rate >= 0); /* 0 -> unset */
|
2011-11-24 13:29:56 +00:00
|
|
|
g_return_if_fail (channel_mapping_family == 0 || channel_mapping);
|
2011-11-20 09:52:46 +00:00
|
|
|
|
|
|
|
/* Opus streams in Ogg begin with two headers; the initial header (with
|
|
|
|
most of the codec setup parameters) which is mandated by the Ogg
|
|
|
|
bitstream spec. The second header holds any comment fields. */
|
|
|
|
|
|
|
|
/* create header buffers */
|
2011-11-24 13:29:56 +00:00
|
|
|
buf1 =
|
2011-12-08 18:45:27 +00:00
|
|
|
gst_opus_enc_create_id_buffer (nchannels, n_stereo_streams, sample_rate,
|
2011-11-24 13:29:56 +00:00
|
|
|
channel_mapping_family, channel_mapping);
|
2011-11-20 09:52:46 +00:00
|
|
|
buf2 = gst_opus_enc_create_metadata_buffer (tags);
|
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
gst_opus_header_create_caps_from_headers (caps, headers, buf1, buf2);
|
2014-07-10 14:52:46 +00:00
|
|
|
|
|
|
|
gst_buffer_unref (buf2);
|
|
|
|
gst_buffer_unref (buf1);
|
2011-11-20 09:52:46 +00:00
|
|
|
}
|
2011-11-21 17:48:54 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_opus_header_is_header (GstBuffer * buf, const char *magic, guint magic_size)
|
|
|
|
{
|
2011-11-25 11:48:58 +00:00
|
|
|
return (gst_buffer_get_size (buf) >= magic_size
|
|
|
|
&& !gst_buffer_memcmp (buf, 0, magic, magic_size));
|
2011-11-21 17:48:54 +00:00
|
|
|
}
|
2011-11-24 13:29:56 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_opus_header_is_id_header (GstBuffer * buf)
|
|
|
|
{
|
2011-11-25 11:48:58 +00:00
|
|
|
gsize size = gst_buffer_get_size (buf);
|
|
|
|
guint8 *data = NULL;
|
2012-05-24 21:12:56 +00:00
|
|
|
guint8 version, channels, channel_mapping_family, n_streams, n_stereo_streams;
|
2011-11-25 11:48:58 +00:00
|
|
|
gboolean ret = FALSE;
|
2012-03-07 16:14:29 +00:00
|
|
|
GstMapInfo map;
|
2011-11-24 13:29:56 +00:00
|
|
|
|
|
|
|
if (size < 19)
|
2011-11-25 11:48:58 +00:00
|
|
|
goto beach;
|
2011-11-24 13:29:56 +00:00
|
|
|
if (!gst_opus_header_is_header (buf, "OpusHead", 8))
|
2011-11-25 11:48:58 +00:00
|
|
|
goto beach;
|
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
data = map.data;
|
|
|
|
size = map.size;
|
2011-11-25 11:48:58 +00:00
|
|
|
|
2012-05-24 21:12:56 +00:00
|
|
|
version = data[8];
|
|
|
|
if (version >= 0x0f) /* major version >=0 is what we grok */
|
|
|
|
goto beach;
|
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
channels = data[9];
|
2011-11-25 11:48:58 +00:00
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
if (channels == 0)
|
2011-11-25 11:48:58 +00:00
|
|
|
goto beach;
|
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
channel_mapping_family = data[18];
|
2011-11-25 11:48:58 +00:00
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
if (channel_mapping_family == 0) {
|
|
|
|
if (channels > 2)
|
2011-11-25 11:48:58 +00:00
|
|
|
goto beach;
|
2011-11-24 13:29:56 +00:00
|
|
|
} else {
|
|
|
|
channels = data[9];
|
|
|
|
if (size < 21 + channels)
|
2011-11-25 11:48:58 +00:00
|
|
|
goto beach;
|
2011-11-24 13:29:56 +00:00
|
|
|
n_streams = data[19];
|
|
|
|
n_stereo_streams = data[20];
|
|
|
|
if (n_streams == 0)
|
2011-11-25 11:48:58 +00:00
|
|
|
goto beach;
|
2011-11-24 13:29:56 +00:00
|
|
|
if (n_stereo_streams > n_streams)
|
2011-11-25 11:48:58 +00:00
|
|
|
goto beach;
|
2011-11-24 13:29:56 +00:00
|
|
|
if (n_streams + n_stereo_streams > 255)
|
2011-11-25 11:48:58 +00:00
|
|
|
goto beach;
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
2011-11-25 11:48:58 +00:00
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
beach:
|
|
|
|
if (data)
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2011-11-25 11:48:58 +00:00
|
|
|
return ret;
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_opus_header_is_comment_header (GstBuffer * buf)
|
|
|
|
{
|
|
|
|
return gst_opus_header_is_header (buf, "OpusTags", 8);
|
2011-11-21 17:48:54 +00:00
|
|
|
}
|