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>
6 * gstwavpackcommon.c: common helper functions
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.
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.
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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
28 #include "gstwavpackcommon.h"
32 #include <gst/audio/multichannel.h>
34 GST_DEBUG_CATEGORY_EXTERN (wavpack_debug);
35 #define GST_CAT_DEFAULT wavpack_debug
38 gst_wavpack_read_header (WavpackHeader * header, guint8 * buf)
40 g_memmove (header, buf, sizeof (WavpackHeader));
42 #ifndef WAVPACK_OLD_API
43 WavpackLittleEndianToNative (header, WavpackHeaderFormat);
45 little_endian_to_native (header, WavpackHeaderFormat);
48 return (memcmp (header->ckID, "wvpk", 4) == 0);
51 /* inspired by the original one in wavpack */
53 gst_wavpack_read_metadata (GstWavpackMetadata * wpmd, guint8 * header_data,
59 gst_wavpack_read_header (&hdr, header_data);
60 end = header_data + hdr.ckSize + 8;
62 if (end - *p_data < 2)
65 wpmd->id = GST_READ_UINT8 (*p_data);
66 wpmd->byte_length = 2 * (guint) GST_READ_UINT8 (*p_data + 1);
70 if ((wpmd->id & ID_LARGE) == ID_LARGE) {
73 wpmd->id &= ~ID_LARGE;
75 if (end - *p_data < 2)
78 extra = GST_READ_UINT16_LE (*p_data);
79 wpmd->byte_length += (extra << 9);
83 if ((wpmd->id & ID_ODD_SIZE) == ID_ODD_SIZE) {
84 wpmd->id &= ~ID_ODD_SIZE;
88 if (wpmd->byte_length > 0) {
89 if (end - *p_data < wpmd->byte_length + (wpmd->byte_length & 1)) {
95 *p_data += wpmd->byte_length + (wpmd->byte_length & 1);
104 gst_wavpack_get_default_channel_mask (gint nchannels)
106 gint channel_mask = 0;
108 /* Set the default channel mask for the given number of channels.
109 * It's the same as for WAVE_FORMAT_EXTENDED:
110 * http://www.microsoft.com/whdc/device/audio/multichaud.mspx
114 channel_mask |= 0x00400;
115 channel_mask |= 0x00200;
117 channel_mask |= 0x00100;
119 channel_mask |= 0x00080;
120 channel_mask |= 0x00040;
122 channel_mask |= 0x00020;
123 channel_mask |= 0x00010;
125 channel_mask |= 0x00008;
127 channel_mask |= 0x00004;
129 channel_mask |= 0x00002;
130 channel_mask |= 0x00001;
133 /* For mono use front center */
134 channel_mask |= 0x00004;
143 const guint32 ms_mask;
144 const GstAudioChannelPosition gst_pos;
145 } layout_mapping[] = {
147 0x00001, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT}, {
148 0x00002, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
149 0x00004, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}, {
150 0x00008, GST_AUDIO_CHANNEL_POSITION_LFE}, {
151 0x00010, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT}, {
152 0x00020, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
153 0x00040, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER}, {
154 0x00080, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
155 0x00100, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, {
156 0x00200, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT}, {
157 0x00400, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, {
158 0x00800, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_CENTER */
160 0x01000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_FRONT_LEFT */
162 0x02000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_FRONT_CENTER */
164 0x04000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_FRONT_RIGHT */
166 0x08000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_BACK_LEFT */
168 0x10000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_BACK_CENTER */
170 0x20000, GST_AUDIO_CHANNEL_POSITION_INVALID} /* TOP_BACK_RIGHT */
173 #define MAX_CHANNEL_POSITIONS G_N_ELEMENTS (layout_mapping)
176 gst_wavpack_set_channel_layout (GstCaps * caps, gint layout)
178 GstAudioChannelPosition pos[MAX_CHANNEL_POSITIONS];
180 gint num_channels, i, p;
182 s = gst_caps_get_structure (caps, 0);
183 if (!gst_structure_get_int (s, "channels", &num_channels))
184 g_return_val_if_reached (FALSE);
186 if (num_channels == 1 && layout == 0x00004) {
187 pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO;
192 for (i = 0; i < MAX_CHANNEL_POSITIONS; ++i) {
193 if ((layout & layout_mapping[i].ms_mask) != 0) {
194 if (p >= num_channels) {
195 GST_WARNING ("More bits set in the channel layout map than there "
196 "are channels! Broken file");
199 if (layout_mapping[i].gst_pos == GST_AUDIO_CHANNEL_POSITION_INVALID) {
200 GST_WARNING ("Unsupported channel position (mask 0x%08x) in channel "
201 "layout map - ignoring those channels", layout_mapping[i].ms_mask);
202 /* what to do? just ignore it and let downstream deal with a channel
203 * layout that has INVALID positions in it for now ... */
205 pos[p] = layout_mapping[i].gst_pos;
210 if (p != num_channels) {
211 GST_WARNING ("Only %d bits set in the channel layout map, but there are "
212 "supposed to be %d channels! Broken file", p, num_channels);
216 gst_audio_set_channel_positions (s, pos);
220 GstAudioChannelPosition *
221 gst_wavpack_get_default_channel_positions (gint nchannels)
223 GstAudioChannelPosition *pos = g_new (GstAudioChannelPosition, nchannels);
226 if (nchannels == 1) {
227 pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
231 for (i = 0; i < nchannels; i++)
232 pos[i] = layout_mapping[i].gst_pos;
238 gst_wavpack_get_channel_mask_from_positions (GstAudioChannelPosition * pos,
241 gint channel_mask = 0;
244 if (nchannels == 1 && pos[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_MONO) {
245 channel_mask = 0x00000004;
249 /* FIXME: not exactly efficient but otherwise we need an inverse
250 * mapping table too */
251 for (i = 0; i < nchannels; i++) {
252 for (j = 0; j < MAX_CHANNEL_POSITIONS; j++) {
253 if (pos[i] == layout_mapping[j].gst_pos) {
254 channel_mask |= layout_mapping[j].ms_mask;
264 gst_wavpack_set_channel_mapping (GstAudioChannelPosition * pos, gint nchannels,
265 gint8 * channel_mapping)
270 for (i = 0; i < nchannels; i++) {
271 for (j = 0; j < MAX_CHANNEL_POSITIONS; j++) {
272 if (pos[i] == layout_mapping[j].gst_pos) {
273 channel_mapping[i] = j;