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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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 gboolean
gst_opus_header_is_header(GstBuffer * buf,const char * magic,guint magic_size)30 gst_opus_header_is_header (GstBuffer * buf, const char *magic, guint magic_size)
31 {
32 return (gst_buffer_get_size (buf) >= magic_size
33 && !gst_buffer_memcmp (buf, 0, magic, magic_size));
34 }
35
36 gboolean
gst_opus_header_is_id_header(GstBuffer * buf)37 gst_opus_header_is_id_header (GstBuffer * buf)
38 {
39 gsize size = gst_buffer_get_size (buf);
40 guint8 *data = NULL;
41 guint8 version, channels, channel_mapping_family, n_streams, n_stereo_streams;
42 gboolean ret = FALSE;
43 GstMapInfo map;
44
45 if (size < 19)
46 goto beach;
47 if (!gst_opus_header_is_header (buf, "OpusHead", 8))
48 goto beach;
49
50 gst_buffer_map (buf, &map, GST_MAP_READ);
51 data = map.data;
52 size = map.size;
53
54 version = data[8];
55 if (version >= 0x0f) /* major version >=0 is what we grok */
56 goto beach;
57
58 channels = data[9];
59
60 if (channels == 0)
61 goto beach;
62
63 channel_mapping_family = data[18];
64
65 if (channel_mapping_family == 0) {
66 if (channels > 2)
67 goto beach;
68 } else {
69 channels = data[9];
70 if (size < 21 + channels)
71 goto beach;
72 n_streams = data[19];
73 n_stereo_streams = data[20];
74 if (n_streams == 0)
75 goto beach;
76 if (n_stereo_streams > n_streams)
77 goto beach;
78 if (n_streams + n_stereo_streams > 255)
79 goto beach;
80 }
81 ret = TRUE;
82
83 beach:
84 if (data)
85 gst_buffer_unmap (buf, &map);
86 return ret;
87 }
88
89 gboolean
gst_opus_header_is_comment_header(GstBuffer * buf)90 gst_opus_header_is_comment_header (GstBuffer * buf)
91 {
92 return gst_opus_header_is_header (buf, "OpusTags", 8);
93 }
94