opusenc: only use mono streams for > 2 channels
[platform/upstream/gstreamer.git] / ext / opus / gstopusheader.c
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <gst/tag/tag.h>
26 #include <gst/base/gstbytewriter.h>
27 #include "gstopusheader.h"
28
29 static GstBuffer *
30 gst_opus_enc_create_id_buffer (gint nchannels, gint sample_rate,
31     guint8 channel_mapping_family, const guint8 * channel_mapping)
32 {
33   GstBuffer *buffer;
34   GstByteWriter bw;
35
36   gst_byte_writer_init (&bw);
37
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);
50   }
51
52   buffer = gst_byte_writer_reset_and_get_buffer (&bw);
53
54   GST_BUFFER_OFFSET (buffer) = 0;
55   GST_BUFFER_OFFSET_END (buffer) = 0;
56
57   return buffer;
58 }
59
60 static GstBuffer *
61 gst_opus_enc_create_metadata_buffer (const GstTagList * tags)
62 {
63   GstTagList *empty_tags = NULL;
64   GstBuffer *comments = NULL;
65
66   GST_DEBUG ("tags = %" GST_PTR_FORMAT, tags);
67
68   if (tags == NULL) {
69     /* FIXME: better fix chain of callers to not write metadata at all,
70      * if there is none */
71     empty_tags = gst_tag_list_new ();
72     tags = empty_tags;
73   }
74   comments =
75       gst_tag_list_to_vorbiscomment_buffer (tags, (const guint8 *) "OpusTags",
76       8, "Encoded with GStreamer Opusenc");
77
78   GST_BUFFER_OFFSET (comments) = 0;
79   GST_BUFFER_OFFSET_END (comments) = 0;
80
81   if (empty_tags)
82     gst_tag_list_free (empty_tags);
83
84   return comments;
85 }
86
87 /*
88  * (really really) FIXME: move into core (dixit tpm)
89  */
90 /**
91  * _gst_caps_set_buffer_array:
92  * @caps: a #GstCaps
93  * @field: field in caps to set
94  * @buf: header buffers
95  *
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.
98  *
99  * Returns: input caps with a streamheader field added, or NULL if some error
100  */
101 static GstCaps *
102 _gst_caps_set_buffer_array (GstCaps * caps, const gchar * field,
103     GstBuffer * buf, ...)
104 {
105   GstStructure *structure = NULL;
106   va_list va;
107   GValue array = { 0 };
108   GValue value = { 0 };
109
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);
113
114   caps = gst_caps_make_writable (caps);
115   structure = gst_caps_get_structure (caps, 0);
116
117   g_value_init (&array, GST_TYPE_ARRAY);
118
119   va_start (va, buf);
120   /* put buffers in a fixed list */
121   while (buf) {
122     g_assert (gst_buffer_is_writable (buf));
123
124     /* mark buffer */
125     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
126
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);
134
135     buf = va_arg (va, GstBuffer *);
136   }
137
138   gst_structure_set_value (structure, field, &array);
139   g_value_unset (&array);
140
141   return caps;
142 }
143
144 void
145 gst_opus_header_create_caps_from_headers (GstCaps ** caps, GSList ** headers,
146     GstBuffer * buf1, GstBuffer * buf2)
147 {
148   g_return_if_fail (caps);
149   g_return_if_fail (headers && !*headers);
150
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);
154
155   *headers = g_slist_prepend (*headers, buf2);
156   *headers = g_slist_prepend (*headers, buf1);
157 }
158
159 void
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)
163 {
164   GstBuffer *buf1, *buf2;
165
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);
171
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. */
175
176   /* create header buffers */
177   buf1 =
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);
181
182   gst_opus_header_create_caps_from_headers (caps, headers, buf1, buf2);
183 }
184
185 gboolean
186 gst_opus_header_is_header (GstBuffer * buf, const char *magic, guint magic_size)
187 {
188   return (GST_BUFFER_SIZE (buf) >= magic_size
189       && !memcmp (magic, GST_BUFFER_DATA (buf), magic_size));
190 }
191
192 gboolean
193 gst_opus_header_is_id_header (GstBuffer * buf)
194 {
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;
198
199   if (size < 19)
200     return FALSE;
201   if (!gst_opus_header_is_header (buf, "OpusHead", 8))
202     return FALSE;
203   channels = data[9];
204   if (channels == 0)
205     return FALSE;
206   channel_mapping_family = data[18];
207   if (channel_mapping_family == 0) {
208     if (channels > 2)
209       return FALSE;
210   } else {
211     channels = data[9];
212     if (size < 21 + channels)
213       return FALSE;
214     n_streams = data[19];
215     n_stereo_streams = data[20];
216     if (n_streams == 0)
217       return FALSE;
218     if (n_stereo_streams > n_streams)
219       return FALSE;
220     if (n_streams + n_stereo_streams > 255)
221       return FALSE;
222   }
223   return TRUE;
224 }
225
226 gboolean
227 gst_opus_header_is_comment_header (GstBuffer * buf)
228 {
229   return gst_opus_header_is_header (buf, "OpusTags", 8);
230 }