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 sample_rate,
31 guint8 channel_mapping_family, const guint8 * channel_mapping)
36 gst_byte_writer_init (&bw);
38 /* See http://wiki.xiph.org/OggOpus */
39 gst_byte_writer_put_data (&bw, (const guint8 *) "OpusHead", 8);
40 gst_byte_writer_put_uint8 (&bw, 0); /* version number */
41 gst_byte_writer_put_uint8 (&bw, nchannels);
42 gst_byte_writer_put_uint16_le (&bw, 0); /* pre-skip */
43 gst_byte_writer_put_uint32_le (&bw, sample_rate);
44 gst_byte_writer_put_uint16_le (&bw, 0); /* output gain */
45 gst_byte_writer_put_uint8 (&bw, channel_mapping_family);
46 if (channel_mapping_family > 0) {
47 gst_byte_writer_put_uint8 (&bw, nchannels);
48 gst_byte_writer_put_uint8 (&bw, 0);
49 gst_byte_writer_put_data (&bw, channel_mapping, nchannels);
52 buffer = gst_byte_writer_reset_and_get_buffer (&bw);
54 GST_BUFFER_OFFSET (buffer) = 0;
55 GST_BUFFER_OFFSET_END (buffer) = 0;
61 gst_opus_enc_create_metadata_buffer (const GstTagList * tags)
63 GstTagList *empty_tags = NULL;
64 GstBuffer *comments = NULL;
66 GST_DEBUG ("tags = %" GST_PTR_FORMAT, tags);
69 /* FIXME: better fix chain of callers to not write metadata at all,
71 empty_tags = gst_tag_list_new ();
75 gst_tag_list_to_vorbiscomment_buffer (tags, (const guint8 *) "OpusTags",
76 8, "Encoded with GStreamer Opusenc");
78 GST_BUFFER_OFFSET (comments) = 0;
79 GST_BUFFER_OFFSET_END (comments) = 0;
82 gst_tag_list_free (empty_tags);
88 * (really really) FIXME: move into core (dixit tpm)
91 * _gst_caps_set_buffer_array:
93 * @field: field in caps to set
94 * @buf: header buffers
96 * Adds given buffers to an array of buffers set as the given @field
97 * on the given @caps. List of buffer arguments must be NULL-terminated.
99 * Returns: input caps with a streamheader field added, or NULL if some error
102 _gst_caps_set_buffer_array (GstCaps * caps, const gchar * field,
103 GstBuffer * buf, ...)
105 GstStructure *structure = NULL;
107 GValue array = { 0 };
108 GValue value = { 0 };
110 g_return_val_if_fail (caps != NULL, NULL);
111 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
112 g_return_val_if_fail (field != NULL, NULL);
114 caps = gst_caps_make_writable (caps);
115 structure = gst_caps_get_structure (caps, 0);
117 g_value_init (&array, GST_TYPE_ARRAY);
120 /* put buffers in a fixed list */
122 g_assert (gst_buffer_is_writable (buf));
125 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
127 g_value_init (&value, GST_TYPE_BUFFER);
128 buf = gst_buffer_copy (buf);
129 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
130 gst_value_set_buffer (&value, buf);
131 gst_buffer_unref (buf);
132 gst_value_array_append_value (&array, &value);
133 g_value_unset (&value);
135 buf = va_arg (va, GstBuffer *);
138 gst_structure_set_value (structure, field, &array);
139 g_value_unset (&array);
145 gst_opus_header_create_caps_from_headers (GstCaps ** caps, GSList ** headers,
146 GstBuffer * buf1, GstBuffer * buf2)
148 g_return_if_fail (caps);
149 g_return_if_fail (headers && !*headers);
151 /* mark and put on caps */
152 *caps = gst_caps_from_string ("audio/x-opus");
153 *caps = _gst_caps_set_buffer_array (*caps, "streamheader", buf1, buf2, NULL);
155 *headers = g_slist_prepend (*headers, buf2);
156 *headers = g_slist_prepend (*headers, buf1);
160 gst_opus_header_create_caps (GstCaps ** caps, GSList ** headers, gint nchannels,
161 gint sample_rate, guint8 channel_mapping_family,
162 const guint8 * channel_mapping, const GstTagList * tags)
164 GstBuffer *buf1, *buf2;
166 g_return_if_fail (caps);
167 g_return_if_fail (headers && !*headers);
168 g_return_if_fail (nchannels > 0);
169 g_return_if_fail (sample_rate >= 0); /* 0 -> unset */
170 g_return_if_fail (channel_mapping_family == 0 || channel_mapping);
172 /* Opus streams in Ogg begin with two headers; the initial header (with
173 most of the codec setup parameters) which is mandated by the Ogg
174 bitstream spec. The second header holds any comment fields. */
176 /* create header buffers */
178 gst_opus_enc_create_id_buffer (nchannels, sample_rate,
179 channel_mapping_family, channel_mapping);
180 buf2 = gst_opus_enc_create_metadata_buffer (tags);
182 gst_opus_header_create_caps_from_headers (caps, headers, buf1, buf2);
186 gst_opus_header_is_header (GstBuffer * buf, const char *magic, guint magic_size)
188 return (GST_BUFFER_SIZE (buf) >= magic_size
189 && !memcmp (magic, GST_BUFFER_DATA (buf), magic_size));
193 gst_opus_header_is_id_header (GstBuffer * buf)
195 gsize size = GST_BUFFER_SIZE (buf);
196 const guint8 *data = GST_BUFFER_DATA (buf);
197 guint8 channels, channel_mapping_family, n_streams, n_stereo_streams;
201 if (!gst_opus_header_is_header (buf, "OpusHead", 8))
206 channel_mapping_family = data[18];
207 if (channel_mapping_family == 0) {
212 if (size < 21 + channels)
214 n_streams = data[19];
215 n_stereo_streams = data[20];
218 if (n_stereo_streams > n_streams)
220 if (n_streams + n_stereo_streams > 255)
227 gst_opus_header_is_comment_header (GstBuffer * buf)
229 return gst_opus_header_is_header (buf, "OpusTags", 8);