2 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
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. */
10 #include <sys/types.h>
20 #include "portable_io.h"
25 * Find the first occurrence of the given pattern.
27 * \param pattern The pattern to search for.
28 * \param pattern_len The length of the pattern in bytes.
29 * \param buf The buffer to search for the pattern.
30 * \param buf_size The number of bytes in \a buf.
32 const char *search_pattern(const char *pattern, int pattern_len,
33 const char *buf, int buf_size)
35 const char *p, *end = buf + buf_size;
37 /* TODO: Use suffix arrays to speed up the search. */
38 for (p = buf; p + pattern_len < end; p++) {
39 if (memcmp(p, pattern, pattern_len))
41 PARA_DEBUG_LOG("found %d byte pattern@%d\n",
42 pattern_len, (int)(p - buf));
45 PARA_NOTICE_LOG("%d byte pattern not found\n", pattern_len);
50 40 9e 69 f8 4d 5b cf 11 a8 fd 00 80 5f 5c 44 2b
52 static int find_audio_stream_info(const char *buf, int len)
54 const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
55 const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
58 return -E_WMA_NO_GUID;
59 PARA_DEBUG_LOG("found audio stream guid@%0x\n", (int)(p - buf));
63 static int read_header_len(const char *buf, int len)
69 header_len = read_u16(buf + 16) + 46;
70 PARA_DEBUG_LOG("header_len: %d\n", header_len);
75 * Read an asf audio file header.
77 * \param buf The input buffer.
78 * \param loaded Number of bytes in \a buf.
79 * \param ahi Result pointer.
81 * \return Negative on errors, zero if more data is needed in order to read the
82 * full header, 1 on success.
84 int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
89 ahi->header_len = read_header_len(buf, loaded);
90 if (ahi->header_len == 0) /* too short to read header len */
92 if (ahi->header_len > loaded) /* too short to read header */
94 ret = find_audio_stream_info(buf, ahi->header_len);
97 if (ret + 62 > loaded)
99 ahi->audio_stream_info_start = ret;
100 start = buf + ahi->audio_stream_info_start;
101 ahi->channels = ((uint8_t *)start)[40];
102 ahi->sample_rate = read_u16(start + 42);
103 PARA_NOTICE_LOG("%d channels, sample rate: %d\n", ahi->channels,
106 ahi->bit_rate = 8 * read_u16(start + 46);
107 PARA_INFO_LOG("bit rate: %d\n", ahi->bit_rate);
109 ahi->block_align = read_u16(start + 50);
110 PARA_INFO_LOG("block_align: %d\n", ahi->block_align);
112 ahi->flags1 = read_u32(start + 56);
113 ahi->flags2 = read_u16(start + 60);
114 PARA_INFO_LOG("read_asf_header: flags1: %d, flag2: %d\n",
115 ahi->flags1, ahi->flags2);
119 const uint8_t log2_tab[256] = {
120 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
121 4, 4, 4, 4, 4, 4, 4, 4,
122 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
123 5, 5, 5, 5, 5, 5, 5, 5,
124 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
125 6, 6, 6, 6, 6, 6, 6, 6,
126 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
127 6, 6, 6, 6, 6, 6, 6, 6,
128 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
129 7, 7, 7, 7, 7, 7, 7, 7,
130 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
131 7, 7, 7, 7, 7, 7, 7, 7,
132 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
133 7, 7, 7, 7, 7, 7, 7, 7,
134 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
135 7, 7, 7, 7, 7, 7, 7, 7
139 * Compute the base-2 logarithm.
141 * \param v The value to compute the logarithm of.
143 * \return An integer approximation of log2(v).
145 int wma_log2(unsigned int v)
148 if (v & 0xffff0000) {