b101d0a6e88e921d814e630113d59a263a657fd1
[paraslash.git] / wma_common.c
1 /*
2 * Copyright (C) 2009-2013 Andre Noll <maan@systemlinux.org>
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 <inttypes.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <string.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "afh.h"
20 #include "portable_io.h"
21 #include "imdct.h"
22 #include "wma.h"
23
24 /**
25 * Find the first occurrence of the given pattern.
26 *
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.
31 *
32 * \return A pointer into \a buf or \p NULL if the pattern was not found.
33 */
34 const char *search_pattern(const char *pattern, int pattern_len,
35 const char *buf, int buf_size)
36 {
37 const char *p, *end = buf + buf_size;
38
39 /* TODO: Use suffix arrays to speed up the search. */
40 for (p = buf; p + pattern_len < end; p++) {
41 if (memcmp(p, pattern, pattern_len))
42 continue;
43 PARA_DEBUG_LOG("found %d byte pattern@%d\n",
44 pattern_len, (int)(p - buf));
45 return p;
46 }
47 PARA_NOTICE_LOG("%d byte pattern not found\n", pattern_len);
48 return NULL;
49 }
50
51 /*
52 40 9e 69 f8 4d 5b cf 11 a8 fd 00 80 5f 5c 44 2b
53 */
54 static int find_audio_stream_info(const char *buf, int len)
55 {
56 const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
57 const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
58
59 if (!p)
60 return -E_WMA_NO_GUID;
61 PARA_DEBUG_LOG("found audio stream guid@%0x\n", (int)(p - buf));
62 return p - buf + 16;
63 }
64
65 static int read_header_len(const char *buf, int len)
66 {
67 uint16_t header_len;
68
69 if (len < 18)
70 return 0;
71 header_len = read_u16(buf + 16) + 46;
72 PARA_DEBUG_LOG("header_len: %d\n", header_len);
73 return header_len;
74 }
75
76 /**
77 * Read an asf audio file header.
78 *
79 * \param buf The input buffer.
80 * \param loaded Number of bytes in \a buf.
81 * \param ahi Result pointer.
82 *
83 * \return Negative on errors, zero if more data is needed in order to read the
84 * full header, 1 on success.
85 */
86 int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
87 {
88 int ret;
89 const char *start;
90
91 ahi->header_len = read_header_len(buf, loaded);
92 if (ahi->header_len == 0) /* too short to read header len */
93 return 0;
94 if (ahi->header_len > loaded) /* too short to read header */
95 return 0;
96 ret = find_audio_stream_info(buf, ahi->header_len);
97 if (ret < 0)
98 return ret;
99 if (ret + 62 > loaded)
100 return 0;
101 ahi->audio_stream_info_start = ret;
102 start = buf + ahi->audio_stream_info_start;
103 ahi->channels = ((uint8_t *)start)[40];
104 ahi->sample_rate = read_u16(start + 42);
105 PARA_NOTICE_LOG("%d channels, sample rate: %d\n", ahi->channels,
106 ahi->sample_rate);
107
108 ahi->bit_rate = 8 * read_u16(start + 46);
109 PARA_INFO_LOG("bit rate: %d\n", ahi->bit_rate);
110
111 ahi->block_align = read_u16(start + 50);
112 PARA_INFO_LOG("block_align: %d\n", ahi->block_align);
113
114 ahi->flags1 = read_u32(start + 56);
115 ahi->flags2 = read_u16(start + 60);
116 PARA_INFO_LOG("read_asf_header: flags1: %d, flag2: %d\n",
117 ahi->flags1, ahi->flags2);
118 return 1;
119 }
120
121 static const uint8_t log2_tab[256] = {
122 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
123 4, 4, 4, 4, 4, 4, 4, 4,
124 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
125 5, 5, 5, 5, 5, 5, 5, 5,
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 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
129 6, 6, 6, 6, 6, 6, 6, 6,
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,
136 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
137 7, 7, 7, 7, 7, 7, 7, 7
138 };
139
140 /**
141 * Compute the base-2 logarithm.
142 *
143 * \param v The value to compute the logarithm of.
144 *
145 * \return An integer approximation of log2(v).
146 */
147 __a_const int wma_log2(unsigned int v)
148 {
149 int n = 0;
150 if (v & 0xffff0000) {
151 v >>= 16;
152 n += 16;
153 }
154 if (v & 0xff00) {
155 v >>= 8;
156 n += 8;
157 }
158 n += log2_tab[v];
159
160 return n;
161 }