Merge branch 'maint'
[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 /*
46 40 9e 69 f8 4d 5b cf 11 a8 fd 00 80 5f 5c 44 2b
47 */
48 static int find_audio_stream_info(const char *buf, int len)
49 {
50 const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
51 const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
52
53 if (!p)
54 return -E_WMA_NO_GUID;
55 PARA_DEBUG_LOG("found audio stream guid@%0x\n", (int)(p - buf));
56 return p - buf + 16;
57 }
58
59 static int read_header_len(const char *buf, int len)
60 {
61 uint16_t header_len;
62
63 if (len < 18)
64 return 0;
65 header_len = read_u16(buf + 16) + 46;
66 PARA_DEBUG_LOG("header_len: %d\n", header_len);
67 return header_len;
68 }
69
70 /**
71 * Read an asf audio file header.
72 *
73 * \param buf The input buffer.
74 * \param loaded Number of bytes in \a buf.
75 * \param ahi Result pointer.
76 *
77 * \return Negative on errors, zero if more data is needed in order to read the
78 * full header, 1 on success.
79 */
80 int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
81 {
82 int ret;
83 const char *start;
84
85 ahi->header_len = read_header_len(buf, loaded);
86 if (ahi->header_len == 0) /* too short to read header len */
87 return 0;
88 if (ahi->header_len > loaded) /* too short to read header */
89 return 0;
90 ret = find_audio_stream_info(buf, ahi->header_len);
91 if (ret < 0)
92 return ret;
93 if (ret + 62 > loaded)
94 return 0;
95 ahi->audio_stream_info_start = ret;
96 start = buf + ahi->audio_stream_info_start;
97 ahi->channels = ((uint8_t *)start)[40];
98 ahi->sample_rate = read_u16(start + 42);
99 PARA_NOTICE_LOG("%d channels, sample rate: %d\n", ahi->channels,
100 ahi->sample_rate);
101
102 ahi->bit_rate = 8 * read_u16(start + 46);
103 PARA_INFO_LOG("bit rate: %d\n", ahi->bit_rate);
104
105 ahi->block_align = read_u16(start + 50);
106 PARA_INFO_LOG("block_align: %d\n", ahi->block_align);
107
108 ahi->flags1 = read_u32(start + 56);
109 ahi->flags2 = read_u16(start + 60);
110 PARA_INFO_LOG("read_asf_header: flags1: %d, flag2: %d\n",
111 ahi->flags1, ahi->flags2);
112 ahi->use_exp_vlc = ahi->flags2 & 0x0001;
113 ahi->use_bit_reservoir = ahi->flags2 & 0x0002;
114 ahi->use_variable_block_len = ahi->flags2 & 0x0004;
115 return 1;
116 }
117
118 static const uint8_t log2_tab[256] = {
119 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
120 4, 4, 4, 4, 4, 4, 4, 4,
121 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
122 5, 5, 5, 5, 5, 5, 5, 5,
123 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
124 6, 6, 6, 6, 6, 6, 6, 6,
125 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
126 6, 6, 6, 6, 6, 6, 6, 6,
127 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
128 7, 7, 7, 7, 7, 7, 7, 7,
129 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
130 7, 7, 7, 7, 7, 7, 7, 7,
131 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
132 7, 7, 7, 7, 7, 7, 7, 7,
133 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
134 7, 7, 7, 7, 7, 7, 7, 7
135 };
136
137 /**
138 * Compute the base-2 logarithm.
139 *
140 * \param v The value to compute the logarithm of.
141 *
142 * \return An integer approximation of log2(v).
143 */
144 __a_const int wma_log2(unsigned int v)
145 {
146 int n = 0;
147 if (v & 0xffff0000) {
148 v >>= 16;
149 n += 16;
150 }
151 if (v & 0xff00) {
152 v >>= 8;
153 n += 8;
154 }
155 n += log2_tab[v];
156
157 return n;
158 }