2 * Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file wma_common.c Functions used by both the WMA afh and decoder. */
14 #include "portable_io.h"
19 * Find the first occurrence of the given pattern.
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.
26 * \return A pointer into \a buf or \p NULL if the pattern was not found.
28 const char *search_pattern(const char *pattern, int pattern_len,
29 const char *buf, int buf_size)
31 const char *p, *end = buf + buf_size;
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))
37 PARA_DEBUG_LOG("found %d byte pattern@%d\n",
38 pattern_len, (int)(p - buf));
41 PARA_NOTICE_LOG("%d byte pattern not found\n", pattern_len);
45 static int find_file_properties(const char *buf, int len)
47 const char pattern[] = {0xa1, 0xdc, 0xab, 0x8c};
48 const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
51 return -E_WMA_NO_GUID;
52 PARA_DEBUG_LOG("found file property guid@%0x\n", (unsigned)(p - buf));
57 40 9e 69 f8 4d 5b cf 11 a8 fd 00 80 5f 5c 44 2b
59 static int find_audio_stream_info(const char *buf, int len)
61 const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
62 const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
65 return -E_WMA_NO_GUID;
66 PARA_DEBUG_LOG("found audio stream guid@%0x\n", (unsigned)(p - buf));
70 static int read_header_len(const char *buf, int len)
76 header_len = read_u16(buf + 16) + 46;
77 PARA_DEBUG_LOG("header_len: %d\n", header_len);
82 * Read an asf audio file header.
84 * \param buf The input buffer.
85 * \param loaded Number of bytes in \a buf.
86 * \param ahi Result pointer.
88 * \return Negative on errors, zero if more data is needed in order to read the
89 * full header, 1 on success.
91 int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
96 ahi->header_len = read_header_len(buf, loaded);
97 if (ahi->header_len == 0) /* too short to read header len */
99 if (ahi->header_len > loaded) /* too short to read header */
101 ret = find_audio_stream_info(buf, ahi->header_len);
104 if (ret + 62 > loaded)
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,
113 ahi->bit_rate = 8 * read_u16(start + 46);
114 PARA_INFO_LOG("bit rate: %u\n", ahi->bit_rate);
116 ahi->block_align = read_u16(start + 50);
117 PARA_INFO_LOG("block_align: %d\n", ahi->block_align);
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;
127 ret = find_file_properties(buf, ahi->header_len);
130 /* file property header is always 88 bytes (sans GUID) */
131 if (ret + 88 > loaded)
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);
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
164 * Compute the base-2 logarithm.
166 * \param v The value to compute the logarithm of.
168 * \return An integer approximation of log2(v).
170 __a_const int wma_log2(unsigned int v)
173 if (v & 0xffff0000) {
181 return n + log2_tab[v];