Remove *.cmd and command_util.bash.
[paraslash.git] / wma_common.c
1 /*
2  * Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file wma_common.c Functions used by both the WMA afh and decoder. */
8
9 #include <sys/types.h>
10
11 #include "para.h"
12 #include "error.h"
13 #include "afh.h"
14 #include "portable_io.h"
15 #include "imdct.h"
16 #include "wma.h"
17
18 /**
19  * Find the first occurrence of the given pattern.
20  *
21  * \param pattern The pattern to search for.
22  * \param pattern_len The length of the pattern in bytes.
23  * \param buf The buffer to search for the pattern.
24  * \param buf_size The number of bytes in \a buf.
25  *
26  * \return A pointer into \a buf or \p NULL if the pattern was not found.
27  */
28 const char *search_pattern(const char *pattern, int pattern_len,
29                 const char *buf, int buf_size)
30 {
31         const char *p, *end = buf + buf_size;
32
33         /* TODO: Use suffix arrays to speed up the search. */
34         for (p = buf; p + pattern_len < end; p++) {
35                 if (memcmp(p, pattern, pattern_len))
36                         continue;
37                 PARA_DEBUG_LOG("found %d byte pattern@%d\n",
38                         pattern_len, (int)(p - buf));
39                 return p;
40         }
41         PARA_NOTICE_LOG("%d byte pattern not found\n", pattern_len);
42         return NULL;
43 }
44
45 static int find_file_properties(const char *buf, int len)
46 {
47         const char pattern[] = {0xa1, 0xdc, 0xab, 0x8c};
48         const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
49
50         if (!p)
51                 return -E_WMA_NO_GUID;
52         PARA_DEBUG_LOG("found file property guid@%0x\n", (unsigned)(p - buf));
53         return p - buf + 16;
54 }
55
56 /*
57    40 9e 69 f8 4d 5b cf 11  a8 fd 00 80 5f 5c 44 2b
58  */
59 static int find_audio_stream_info(const char *buf, int len)
60 {
61         const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
62         const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
63
64         if (!p)
65                 return -E_WMA_NO_GUID;
66         PARA_DEBUG_LOG("found audio stream guid@%0x\n", (unsigned)(p - buf));
67         return p - buf + 16;
68 }
69
70 static int read_header_len(const char *buf, int len)
71 {
72         uint16_t header_len;
73
74         if (len < 18)
75                 return 0;
76         header_len = read_u16(buf + 16) + 46;
77         PARA_DEBUG_LOG("header_len: %d\n", header_len);
78         return header_len;
79 }
80
81 /**
82  * Read an asf audio file header.
83  *
84  * \param buf The input buffer.
85  * \param loaded Number of bytes in \a buf.
86  * \param ahi Result pointer.
87  *
88  * \return Negative on errors, zero if more data is needed in order to read the
89  * full header, 1 on success.
90  */
91 int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
92 {
93         int ret;
94         const char *start;
95
96         ahi->header_len = read_header_len(buf, loaded);
97         if (ahi->header_len == 0) /* too short to read header len */
98                 return 0;
99         if (ahi->header_len > loaded) /* too short to read header */
100                 return 0;
101         ret = find_audio_stream_info(buf, ahi->header_len);
102         if (ret < 0)
103                 return ret;
104         if (ret + 62 > loaded)
105                 return 0;
106         ahi->audio_stream_info_start = ret;
107         start = buf + ahi->audio_stream_info_start;
108         ahi->channels = ((uint8_t *)start)[40];
109         ahi->sample_rate = read_u16(start + 42);
110         PARA_NOTICE_LOG("%d channels, sample rate: %d\n", ahi->channels,
111                 ahi->sample_rate);
112
113         ahi->bit_rate = 8 * read_u16(start + 46);
114         PARA_INFO_LOG("bit rate: %u\n", ahi->bit_rate);
115
116         ahi->block_align = read_u16(start + 50);
117         PARA_INFO_LOG("block_align: %d\n", ahi->block_align);
118
119         ahi->flags1 = read_u32(start + 56);
120         ahi->flags2 = read_u16(start + 60);
121         PARA_INFO_LOG("read_asf_header: flags1: %u, flags2: %u\n",
122                 ahi->flags1, ahi->flags2);
123         ahi->use_exp_vlc = ahi->flags2 & 0x0001;
124         ahi->use_bit_reservoir = ahi->flags2 & 0x0002;
125         ahi->use_variable_block_len = ahi->flags2 & 0x0004;
126
127         ret = find_file_properties(buf, ahi->header_len);
128         if (ret < 0)
129                 return ret;
130         /* file property header is always 88 bytes (sans GUID) */
131         if (ret + 88 > loaded)
132                 return 0;
133         start = buf + ret;
134         ahi->packet_size = read_u32(start + 76); /* min packet size */
135         /* we only support fixed packet sizes */
136         if (ahi->packet_size != read_u32(start + 80)) /* min != max */
137                 return -E_BAD_ASF_FILE_PROPS;
138         if (ahi->packet_size <= ahi->block_align)
139                 return -E_BAD_ASF_FILE_PROPS;
140         PARA_INFO_LOG("packet size: %u\n", ahi->packet_size);
141         return 1;
142 }
143
144 static const uint8_t log2_tab[256] = {
145         0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
146         4, 4, 4, 4, 4, 4, 4, 4,
147         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
148         5, 5, 5, 5, 5, 5, 5, 5,
149         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
150         6, 6, 6, 6, 6, 6, 6, 6,
151         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
152         6, 6, 6, 6, 6, 6, 6, 6,
153         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
154         7, 7, 7, 7, 7, 7, 7, 7,
155         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
156         7, 7, 7, 7, 7, 7, 7, 7,
157         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
158         7, 7, 7, 7, 7, 7, 7, 7,
159         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
160         7, 7, 7, 7, 7, 7, 7, 7
161 };
162
163 /**
164  * Compute the base-2 logarithm.
165  *
166  * \param v The value to compute the logarithm of.
167  *
168  * \return An integer approximation of log2(v).
169  */
170 __a_const int wma_log2(unsigned int v)
171 {
172         int n = 0;
173         if (v & 0xffff0000) {
174                 v >>= 16;
175                 n += 16;
176         }
177         if (v & 0xff00) {
178                 v >>= 8;
179                 n += 8;
180         }
181         n += log2_tab[v];
182
183         return n;
184 }