• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer Wavpack plugin
2  * Copyright (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
3  * Copyright (c) 1998 - 2005 Conifer Software
4  * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
5  *
6  * gstwavpackcommon.c: common helper functions
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include "gstwavpackcommon.h"
29 #include <string.h>
30 
31 #include <gst/gst.h>
32 
33 GST_DEBUG_CATEGORY_EXTERN (wavpack_debug);
34 #define GST_CAT_DEFAULT wavpack_debug
35 
36 gboolean
gst_wavpack_read_header(WavpackHeader * header,guint8 * buf)37 gst_wavpack_read_header (WavpackHeader * header, guint8 * buf)
38 {
39   memmove (header, buf, sizeof (WavpackHeader));
40 
41   WavpackLittleEndianToNative (header, (char *) WavpackHeaderFormat);
42 
43   return (memcmp (header->ckID, "wvpk", 4) == 0);
44 }
45 
46 /* inspired by the original one in wavpack */
47 gboolean
gst_wavpack_read_metadata(GstWavpackMetadata * wpmd,guint8 * header_data,guint8 ** p_data)48 gst_wavpack_read_metadata (GstWavpackMetadata * wpmd, guint8 * header_data,
49     guint8 ** p_data)
50 {
51   WavpackHeader hdr;
52   guint8 *end;
53 
54   gst_wavpack_read_header (&hdr, header_data);
55   end = header_data + hdr.ckSize + 8;
56 
57   if (end - *p_data < 2)
58     return FALSE;
59 
60   wpmd->id = GST_READ_UINT8 (*p_data);
61   wpmd->byte_length = 2 * (guint) GST_READ_UINT8 (*p_data + 1);
62 
63   *p_data += 2;
64 
65   if ((wpmd->id & ID_LARGE) == ID_LARGE) {
66     guint extra;
67 
68     wpmd->id &= ~ID_LARGE;
69 
70     if (end - *p_data < 2)
71       return FALSE;
72 
73     extra = GST_READ_UINT16_LE (*p_data);
74     wpmd->byte_length += (extra << 9);
75     *p_data += 2;
76   }
77 
78   if ((wpmd->id & ID_ODD_SIZE) == ID_ODD_SIZE) {
79     wpmd->id &= ~ID_ODD_SIZE;
80     --wpmd->byte_length;
81   }
82 
83   if (wpmd->byte_length > 0) {
84     if (end - *p_data < wpmd->byte_length + (wpmd->byte_length & 1)) {
85       wpmd->data = NULL;
86       return FALSE;
87     }
88 
89     wpmd->data = *p_data;
90     *p_data += wpmd->byte_length + (wpmd->byte_length & 1);
91   } else {
92     wpmd->data = NULL;
93   }
94 
95   return TRUE;
96 }
97 
98 gint
gst_wavpack_get_default_channel_mask(gint nchannels)99 gst_wavpack_get_default_channel_mask (gint nchannels)
100 {
101   gint channel_mask = 0;
102 
103   /* Set the default channel mask for the given number of channels.
104    * It's the same as for WAVE_FORMAT_EXTENDED:
105    * http://www.microsoft.com/whdc/device/audio/multichaud.mspx
106    */
107   switch (nchannels) {
108     case 11:
109       channel_mask |= 0x00400;
110       channel_mask |= 0x00200;
111     case 9:
112       channel_mask |= 0x00100;
113     case 8:
114       channel_mask |= 0x00080;
115       channel_mask |= 0x00040;
116     case 6:
117       channel_mask |= 0x00020;
118       channel_mask |= 0x00010;
119     case 4:
120       channel_mask |= 0x00008;
121     case 3:
122       channel_mask |= 0x00004;
123     case 2:
124       channel_mask |= 0x00002;
125       channel_mask |= 0x00001;
126       break;
127     case 1:
128       /* For mono use front center */
129       channel_mask |= 0x00004;
130       break;
131   }
132 
133   return channel_mask;
134 }
135 
136 static const struct
137 {
138   const guint32 ms_mask;
139   const GstAudioChannelPosition gst_pos;
140 } layout_mapping[] = {
141   {
142   0x00001, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT}, {
143   0x00002, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
144   0x00004, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}, {
145   0x00008, GST_AUDIO_CHANNEL_POSITION_LFE1}, {
146   0x00010, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT}, {
147   0x00020, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
148   0x00040, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER}, {
149   0x00080, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
150   0x00100, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, {
151   0x00200, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT}, {
152   0x00400, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, {
153   0x00800, GST_AUDIO_CHANNEL_POSITION_TOP_CENTER}, {
154   0x01000, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT}, {
155   0x02000, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER}, {
156   0x04000, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT}, {
157   0x08000, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT}, {
158   0x10000, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER}, {
159   0x20000, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT}
160 };
161 
162 #define MAX_CHANNEL_POSITIONS G_N_ELEMENTS (layout_mapping)
163 
164 gboolean
gst_wavpack_get_channel_positions(gint num_channels,gint layout,GstAudioChannelPosition * pos)165 gst_wavpack_get_channel_positions (gint num_channels, gint layout,
166     GstAudioChannelPosition * pos)
167 {
168   gint i, p;
169 
170   if (num_channels == 1 && layout == 0x00004) {
171     pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
172     return TRUE;
173   }
174 
175   p = 0;
176   for (i = 0; i < MAX_CHANNEL_POSITIONS; ++i) {
177     if ((layout & layout_mapping[i].ms_mask) != 0) {
178       if (p >= num_channels) {
179         GST_WARNING ("More bits set in the channel layout map than there "
180             "are channels! Broken file");
181         return FALSE;
182       }
183       if (layout_mapping[i].gst_pos == GST_AUDIO_CHANNEL_POSITION_INVALID) {
184         GST_WARNING ("Unsupported channel position (mask 0x%08x) in channel "
185             "layout map - ignoring those channels", layout_mapping[i].ms_mask);
186         /* what to do? just ignore it and let downstream deal with a channel
187          * layout that has INVALID positions in it for now ... */
188       }
189       pos[p] = layout_mapping[i].gst_pos;
190       ++p;
191     }
192   }
193 
194   if (p != num_channels) {
195     GST_WARNING ("Only %d bits set in the channel layout map, but there are "
196         "supposed to be %d channels! Broken file", p, num_channels);
197     return FALSE;
198   }
199 
200   return TRUE;
201 }
202 
203 GstAudioChannelPosition *
gst_wavpack_get_default_channel_positions(gint nchannels)204 gst_wavpack_get_default_channel_positions (gint nchannels)
205 {
206   GstAudioChannelPosition *pos = g_new (GstAudioChannelPosition, nchannels);
207   gint i;
208 
209   if (nchannels == 1) {
210     pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
211     return pos;
212   }
213 
214   for (i = 0; i < nchannels; i++)
215     pos[i] = layout_mapping[i].gst_pos;
216 
217   return pos;
218 }
219 
220 gint
gst_wavpack_get_channel_mask_from_positions(GstAudioChannelPosition * pos,gint nchannels)221 gst_wavpack_get_channel_mask_from_positions (GstAudioChannelPosition * pos,
222     gint nchannels)
223 {
224   gint channel_mask = 0;
225   gint i, j;
226 
227   if (nchannels == 1 && pos[0] == GST_AUDIO_CHANNEL_POSITION_MONO) {
228     channel_mask = 0x00000004;
229     return channel_mask;
230   }
231 
232   /* FIXME: not exactly efficient but otherwise we need an inverse
233    * mapping table too */
234   for (i = 0; i < nchannels; i++) {
235     for (j = 0; j < MAX_CHANNEL_POSITIONS; j++) {
236       if (pos[i] == layout_mapping[j].gst_pos) {
237         channel_mask |= layout_mapping[j].ms_mask;
238         break;
239       }
240     }
241   }
242 
243   return channel_mask;
244 }
245 
246 gboolean
gst_wavpack_set_channel_mapping(GstAudioChannelPosition * pos,gint nchannels,gint8 * channel_mapping)247 gst_wavpack_set_channel_mapping (GstAudioChannelPosition * pos, gint nchannels,
248     gint8 * channel_mapping)
249 {
250   gint i, j;
251   gboolean ret = TRUE;
252 
253   for (i = 0; i < nchannels; i++) {
254     for (j = 0; j < MAX_CHANNEL_POSITIONS; j++) {
255       if (pos[i] == layout_mapping[j].gst_pos) {
256         channel_mapping[i] = j;
257         ret &= (i == j);
258         break;
259       }
260     }
261   }
262 
263   return !ret;
264 }
265