1 /* GStreamer Opus Encoder
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2008> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * Copyright (C) <2011> Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include <gst/tag/tag.h>
26 #include <gst/base/gstbytewriter.h>
27 #include "gstopusheader.h"
30 gst_opus_enc_create_id_buffer (gint nchannels, gint n_stereo_streams,
31 gint sample_rate, guint8 channel_mapping_family,
32 const guint8 * channel_mapping)
37 g_return_val_if_fail (nchannels > 0 && nchannels < 256, NULL);
38 g_return_val_if_fail (n_stereo_streams >= 0, NULL);
39 g_return_val_if_fail (n_stereo_streams <= nchannels - n_stereo_streams, NULL);
41 gst_byte_writer_init (&bw);
43 /* See http://wiki.xiph.org/OggOpus */
44 gst_byte_writer_put_data (&bw, (const guint8 *) "OpusHead", 8);
45 gst_byte_writer_put_uint8 (&bw, 0); /* version number */
46 gst_byte_writer_put_uint8 (&bw, nchannels);
47 gst_byte_writer_put_uint16_le (&bw, 0); /* pre-skip */
48 gst_byte_writer_put_uint32_le (&bw, sample_rate);
49 gst_byte_writer_put_uint16_le (&bw, 0); /* output gain */
50 gst_byte_writer_put_uint8 (&bw, channel_mapping_family);
51 if (channel_mapping_family > 0) {
52 gst_byte_writer_put_uint8 (&bw, nchannels - n_stereo_streams);
53 gst_byte_writer_put_uint8 (&bw, n_stereo_streams);
54 gst_byte_writer_put_data (&bw, channel_mapping, nchannels);
57 buffer = gst_byte_writer_reset_and_get_buffer (&bw);
59 GST_BUFFER_OFFSET (buffer) = 0;
60 GST_BUFFER_OFFSET_END (buffer) = 0;
66 gst_opus_enc_create_metadata_buffer (const GstTagList * tags)
68 GstTagList *empty_tags = NULL;
69 GstBuffer *comments = NULL;
71 GST_DEBUG ("tags = %" GST_PTR_FORMAT, tags);
74 /* FIXME: better fix chain of callers to not write metadata at all,
76 empty_tags = gst_tag_list_new_empty ();
80 gst_tag_list_to_vorbiscomment_buffer (tags, (const guint8 *) "OpusTags",
81 8, "Encoded with GStreamer Opusenc");
83 GST_BUFFER_OFFSET (comments) = 0;
84 GST_BUFFER_OFFSET_END (comments) = 0;
87 gst_tag_list_free (empty_tags);
93 * (really really) FIXME: move into core (dixit tpm)
96 * _gst_caps_set_buffer_array:
98 * @field: field in caps to set
99 * @buf: header buffers
101 * Adds given buffers to an array of buffers set as the given @field
102 * on the given @caps. List of buffer arguments must be NULL-terminated.
104 * Returns: input caps with a streamheader field added, or NULL if some error
107 _gst_caps_set_buffer_array (GstCaps * caps, const gchar * field,
108 GstBuffer * buf, ...)
110 GstStructure *structure = NULL;
112 GValue array = { 0 };
113 GValue value = { 0 };
115 g_return_val_if_fail (caps != NULL, NULL);
116 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
117 g_return_val_if_fail (field != NULL, NULL);
119 caps = gst_caps_make_writable (caps);
120 structure = gst_caps_get_structure (caps, 0);
122 g_value_init (&array, GST_TYPE_ARRAY);
125 /* put buffers in a fixed list */
127 g_assert (gst_buffer_is_writable (buf));
130 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
132 g_value_init (&value, GST_TYPE_BUFFER);
133 buf = gst_buffer_copy (buf);
134 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
135 gst_value_set_buffer (&value, buf);
136 gst_buffer_unref (buf);
137 gst_value_array_append_value (&array, &value);
138 g_value_unset (&value);
140 buf = va_arg (va, GstBuffer *);
143 gst_structure_set_value (structure, field, &array);
144 g_value_unset (&array);
150 gst_opus_header_create_caps_from_headers (GstCaps ** caps, GSList ** headers,
151 GstBuffer * buf1, GstBuffer * buf2)
153 int n_streams, family;
154 gboolean multistream;
158 g_return_if_fail (caps);
159 g_return_if_fail (headers && !*headers);
160 g_return_if_fail (gst_buffer_get_size (buf1) >= 19);
162 data = gst_buffer_map (buf1, &size, NULL, GST_MAP_READ);
164 /* work out the number of streams */
169 /* only included in the header for family > 0 */
171 n_streams = data[19];
173 g_warning ("family > 0 but header buffer size < 20");
174 gst_buffer_unmap (buf1, data, size);
179 gst_buffer_unmap (buf1, data, size);
181 /* mark and put on caps */
182 multistream = n_streams > 1;
183 *caps = gst_caps_new_simple ("audio/x-opus",
184 "multistream", G_TYPE_BOOLEAN, multistream, NULL);
185 *caps = _gst_caps_set_buffer_array (*caps, "streamheader", buf1, buf2, NULL);
187 *headers = g_slist_prepend (*headers, buf2);
188 *headers = g_slist_prepend (*headers, buf1);
192 gst_opus_header_create_caps (GstCaps ** caps, GSList ** headers, gint nchannels,
193 gint n_stereo_streams, gint sample_rate, guint8 channel_mapping_family,
194 const guint8 * channel_mapping, const GstTagList * tags)
196 GstBuffer *buf1, *buf2;
198 g_return_if_fail (caps);
199 g_return_if_fail (headers && !*headers);
200 g_return_if_fail (nchannels > 0);
201 g_return_if_fail (sample_rate >= 0); /* 0 -> unset */
202 g_return_if_fail (channel_mapping_family == 0 || channel_mapping);
204 /* Opus streams in Ogg begin with two headers; the initial header (with
205 most of the codec setup parameters) which is mandated by the Ogg
206 bitstream spec. The second header holds any comment fields. */
208 /* create header buffers */
210 gst_opus_enc_create_id_buffer (nchannels, n_stereo_streams, sample_rate,
211 channel_mapping_family, channel_mapping);
212 buf2 = gst_opus_enc_create_metadata_buffer (tags);
214 gst_opus_header_create_caps_from_headers (caps, headers, buf1, buf2);
218 gst_opus_header_is_header (GstBuffer * buf, const char *magic, guint magic_size)
220 return (gst_buffer_get_size (buf) >= magic_size
221 && !gst_buffer_memcmp (buf, 0, magic, magic_size));
225 gst_opus_header_is_id_header (GstBuffer * buf)
227 gsize size = gst_buffer_get_size (buf);
229 guint8 channels, channel_mapping_family, n_streams, n_stereo_streams;
230 gboolean ret = FALSE;
234 if (!gst_opus_header_is_header (buf, "OpusHead", 8))
237 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
244 channel_mapping_family = data[18];
246 if (channel_mapping_family == 0) {
251 if (size < 21 + channels)
253 n_streams = data[19];
254 n_stereo_streams = data[20];
257 if (n_stereo_streams > n_streams)
259 if (n_streams + n_stereo_streams > 255)
266 gst_buffer_unmap (buf, data, size);
271 gst_opus_header_is_comment_header (GstBuffer * buf)
273 return gst_opus_header_is_header (buf, "OpusTags", 8);