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);
46 40 9e 69 f8 4d 5b cf 11 a8 fd 00 80 5f 5c 44 2b
48 static int find_audio_stream_info(const char *buf, int len)
50 const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
51 const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
54 return -E_WMA_NO_GUID;
55 PARA_DEBUG_LOG("found audio stream guid@%0x\n", (int)(p - buf));
59 static int read_header_len(const char *buf, int len)
65 header_len = read_u16(buf + 16) + 46;
66 PARA_DEBUG_LOG("header_len: %d\n", header_len);
71 * Read an asf audio file header.
73 * \param buf The input buffer.
74 * \param loaded Number of bytes in \a buf.
75 * \param ahi Result pointer.
77 * \return Negative on errors, zero if more data is needed in order to read the
78 * full header, 1 on success.
80 int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
85 ahi->header_len = read_header_len(buf, loaded);
86 if (ahi->header_len == 0) /* too short to read header len */
88 if (ahi->header_len > loaded) /* too short to read header */
90 ret = find_audio_stream_info(buf, ahi->header_len);
93 if (ret + 62 > loaded)
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,
102 ahi->bit_rate = 8 * read_u16(start + 46);
103 PARA_INFO_LOG("bit rate: %d\n", ahi->bit_rate);
105 ahi->block_align = read_u16(start + 50);
106 PARA_INFO_LOG("block_align: %d\n", ahi->block_align);
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;
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
138 * Compute the base-2 logarithm.
140 * \param v The value to compute the logarithm of.
142 * \return An integer approximation of log2(v).
144 __a_const int wma_log2(unsigned int v)
147 if (v & 0xffff0000) {