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