ext/wavpack/: Use a general wavpack debug category for common code.
[platform/upstream/gst-plugins-good.git] / ext / wavpack / gstwavpackcommon.c
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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, 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 GST_DEBUG_CATEGORY_EXTERN (wavpack_debug);
32 #define GST_CAT_DEFAULT wavpack_debug
33
34 gboolean
35 gst_wavpack_read_header (WavpackHeader * header, guint8 * buf)
36 {
37   g_memmove (header, buf, sizeof (WavpackHeader));
38
39 #ifndef WAVPACK_OLD_API
40   WavpackLittleEndianToNative (header, WavpackHeaderFormat);
41 #else
42   little_endian_to_native (header, WavpackHeaderFormat);
43 #endif
44
45   return (memcmp (header->ckID, "wvpk", 4) == 0);
46 }
47
48 /* inspired by the original one in wavpack */
49 gboolean
50 gst_wavpack_read_metadata (GstWavpackMetadata * wpmd, guint8 * header_data,
51     guint8 ** p_data)
52 {
53   WavpackHeader hdr;
54   guint8 *end;
55
56   gst_wavpack_read_header (&hdr, header_data);
57   end = header_data + hdr.ckSize + 8;
58
59   if (end - *p_data < 2)
60     return FALSE;
61
62   wpmd->id = GST_READ_UINT8 (*p_data);
63   wpmd->byte_length = 2 * (guint) GST_READ_UINT8 (*p_data + 1);
64
65   *p_data += 2;
66
67   if ((wpmd->id & ID_LARGE) == ID_LARGE) {
68     guint extra;
69
70     wpmd->id &= ~ID_LARGE;
71
72     if (end - *p_data < 2)
73       return FALSE;
74
75     extra = GST_READ_UINT16_LE (*p_data);
76     wpmd->byte_length += (extra << 9);
77     *p_data += 2;
78   }
79
80   if ((wpmd->id & ID_ODD_SIZE) == ID_ODD_SIZE) {
81     wpmd->id &= ~ID_ODD_SIZE;
82     --wpmd->byte_length;
83   }
84
85   if (wpmd->byte_length > 0) {
86     if (end - *p_data < wpmd->byte_length + (wpmd->byte_length & 1)) {
87       wpmd->data = NULL;
88       return FALSE;
89     }
90
91     wpmd->data = *p_data;
92     *p_data += wpmd->byte_length + (wpmd->byte_length & 1);
93   } else {
94     wpmd->data = NULL;
95   }
96
97   return TRUE;
98 }