opus: Handle GstByteWriter return values
[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 n_stereo_streams,
31     gint sample_rate, guint8 channel_mapping_family,
32     const guint8 * channel_mapping)
33 {
34   GstBuffer *buffer;
35   GstByteWriter bw;
36   gboolean hdl = TRUE;
37
38   g_return_val_if_fail (nchannels > 0 && nchannels < 256, NULL);
39   g_return_val_if_fail (n_stereo_streams >= 0, NULL);
40   g_return_val_if_fail (n_stereo_streams <= nchannels - n_stereo_streams, NULL);
41
42   gst_byte_writer_init (&bw);
43
44   /* See http://wiki.xiph.org/OggOpus */
45   hdl &= gst_byte_writer_put_data (&bw, (const guint8 *) "OpusHead", 8);
46   hdl &= gst_byte_writer_put_uint8 (&bw, 0);    /* version number */
47   hdl &= gst_byte_writer_put_uint8 (&bw, nchannels);
48   hdl &= gst_byte_writer_put_uint16_le (&bw, 0);        /* pre-skip */
49   hdl &= gst_byte_writer_put_uint32_le (&bw, sample_rate);
50   hdl &= gst_byte_writer_put_uint16_le (&bw, 0);        /* output gain */
51   hdl &= gst_byte_writer_put_uint8 (&bw, channel_mapping_family);
52   if (channel_mapping_family > 0) {
53     hdl &= gst_byte_writer_put_uint8 (&bw, nchannels - n_stereo_streams);
54     hdl &= gst_byte_writer_put_uint8 (&bw, n_stereo_streams);
55     hdl &= gst_byte_writer_put_data (&bw, channel_mapping, nchannels);
56   }
57
58   if (!hdl)
59     GST_WARNING ("Error creating header");
60
61   buffer = gst_byte_writer_reset_and_get_buffer (&bw);
62
63   GST_BUFFER_OFFSET (buffer) = 0;
64   GST_BUFFER_OFFSET_END (buffer) = 0;
65
66   return buffer;
67 }
68
69 static GstBuffer *
70 gst_opus_enc_create_metadata_buffer (const GstTagList * tags)
71 {
72   GstTagList *empty_tags = NULL;
73   GstBuffer *comments = NULL;
74
75   GST_DEBUG ("tags = %" GST_PTR_FORMAT, tags);
76
77   if (tags == NULL) {
78     /* FIXME: better fix chain of callers to not write metadata at all,
79      * if there is none */
80     empty_tags = gst_tag_list_new_empty ();
81     tags = empty_tags;
82   }
83   comments =
84       gst_tag_list_to_vorbiscomment_buffer (tags, (const guint8 *) "OpusTags",
85       8, "Encoded with GStreamer Opusenc");
86
87   GST_BUFFER_OFFSET (comments) = 0;
88   GST_BUFFER_OFFSET_END (comments) = 0;
89
90   if (empty_tags)
91     gst_tag_list_free (empty_tags);
92
93   return comments;
94 }
95
96 /*
97  * (really really) FIXME: move into core (dixit tpm)
98  */
99 /**
100  * _gst_caps_set_buffer_array:
101  * @caps: a #GstCaps
102  * @field: field in caps to set
103  * @buf: header buffers
104  *
105  * Adds given buffers to an array of buffers set as the given @field
106  * on the given @caps.  List of buffer arguments must be NULL-terminated.
107  *
108  * Returns: input caps with a streamheader field added, or NULL if some error
109  */
110 static GstCaps *
111 _gst_caps_set_buffer_array (GstCaps * caps, const gchar * field,
112     GstBuffer * buf, ...)
113 {
114   GstStructure *structure = NULL;
115   va_list va;
116   GValue array = { 0 };
117   GValue value = { 0 };
118
119   g_return_val_if_fail (caps != NULL, NULL);
120   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
121   g_return_val_if_fail (field != NULL, NULL);
122
123   caps = gst_caps_make_writable (caps);
124   structure = gst_caps_get_structure (caps, 0);
125
126   g_value_init (&array, GST_TYPE_ARRAY);
127
128   va_start (va, buf);
129   /* put buffers in a fixed list */
130   while (buf) {
131     g_assert (gst_buffer_is_writable (buf));
132
133     /* mark buffer */
134     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
135
136     g_value_init (&value, GST_TYPE_BUFFER);
137     buf = gst_buffer_copy (buf);
138     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
139     gst_value_set_buffer (&value, buf);
140     gst_buffer_unref (buf);
141     gst_value_array_append_value (&array, &value);
142     g_value_unset (&value);
143
144     buf = va_arg (va, GstBuffer *);
145   }
146
147   gst_structure_set_value (structure, field, &array);
148   g_value_unset (&array);
149
150   return caps;
151 }
152
153 void
154 gst_opus_header_create_caps_from_headers (GstCaps ** caps, GSList ** headers,
155     GstBuffer * buf1, GstBuffer * buf2)
156 {
157   int n_streams, family;
158   gboolean multistream;
159   GstMapInfo map;
160   guint8 *data;
161
162   g_return_if_fail (caps);
163   g_return_if_fail (headers && !*headers);
164   g_return_if_fail (gst_buffer_get_size (buf1) >= 19);
165
166   gst_buffer_map (buf1, &map, GST_MAP_READ);
167   data = map.data;
168
169   /* work out the number of streams */
170   family = data[18];
171   if (family == 0) {
172     n_streams = 1;
173   } else {
174     /* only included in the header for family > 0 */
175     if (map.size >= 20)
176       n_streams = data[19];
177     else {
178       g_warning ("family > 0 but header buffer size < 20");
179       gst_buffer_unmap (buf1, &map);
180       return;
181     }
182   }
183
184   gst_buffer_unmap (buf1, &map);
185
186   /* mark and put on caps */
187   multistream = n_streams > 1;
188   *caps = gst_caps_new_simple ("audio/x-opus",
189       "multistream", G_TYPE_BOOLEAN, multistream, NULL);
190   *caps = _gst_caps_set_buffer_array (*caps, "streamheader", buf1, buf2, NULL);
191
192   *headers = g_slist_prepend (*headers, buf2);
193   *headers = g_slist_prepend (*headers, buf1);
194 }
195
196 void
197 gst_opus_header_create_caps (GstCaps ** caps, GSList ** headers, gint nchannels,
198     gint n_stereo_streams, gint sample_rate, guint8 channel_mapping_family,
199     const guint8 * channel_mapping, const GstTagList * tags)
200 {
201   GstBuffer *buf1, *buf2;
202
203   g_return_if_fail (caps);
204   g_return_if_fail (headers && !*headers);
205   g_return_if_fail (nchannels > 0);
206   g_return_if_fail (sample_rate >= 0);  /* 0 -> unset */
207   g_return_if_fail (channel_mapping_family == 0 || channel_mapping);
208
209   /* Opus streams in Ogg begin with two headers; the initial header (with
210      most of the codec setup parameters) which is mandated by the Ogg
211      bitstream spec.  The second header holds any comment fields. */
212
213   /* create header buffers */
214   buf1 =
215       gst_opus_enc_create_id_buffer (nchannels, n_stereo_streams, sample_rate,
216       channel_mapping_family, channel_mapping);
217   buf2 = gst_opus_enc_create_metadata_buffer (tags);
218
219   gst_opus_header_create_caps_from_headers (caps, headers, buf1, buf2);
220 }
221
222 gboolean
223 gst_opus_header_is_header (GstBuffer * buf, const char *magic, guint magic_size)
224 {
225   return (gst_buffer_get_size (buf) >= magic_size
226       && !gst_buffer_memcmp (buf, 0, magic, magic_size));
227 }
228
229 gboolean
230 gst_opus_header_is_id_header (GstBuffer * buf)
231 {
232   gsize size = gst_buffer_get_size (buf);
233   guint8 *data = NULL;
234   guint8 channels, channel_mapping_family, n_streams, n_stereo_streams;
235   gboolean ret = FALSE;
236   GstMapInfo map;
237
238   if (size < 19)
239     goto beach;
240   if (!gst_opus_header_is_header (buf, "OpusHead", 8))
241     goto beach;
242
243   gst_buffer_map (buf, &map, GST_MAP_READ);
244   data = map.data;
245   size = map.size;
246
247   channels = data[9];
248
249   if (channels == 0)
250     goto beach;
251
252   channel_mapping_family = data[18];
253
254   if (channel_mapping_family == 0) {
255     if (channels > 2)
256       goto beach;
257   } else {
258     channels = data[9];
259     if (size < 21 + channels)
260       goto beach;
261     n_streams = data[19];
262     n_stereo_streams = data[20];
263     if (n_streams == 0)
264       goto beach;
265     if (n_stereo_streams > n_streams)
266       goto beach;
267     if (n_streams + n_stereo_streams > 255)
268       goto beach;
269   }
270   ret = TRUE;
271
272 beach:
273   if (data)
274     gst_buffer_unmap (buf, &map);
275   return ret;
276 }
277
278 gboolean
279 gst_opus_header_is_comment_header (GstBuffer * buf)
280 {
281   return gst_opus_header_is_header (buf, "OpusTags", 8);
282 }