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"
24 const char *search_pattern(const char *pattern
, int pattern_len
, const char *buf
, int buf_size
)
26 const char *p
, *end
= buf
+ buf_size
;
28 for (p
= buf
; p
+ pattern_len
< end
; p
++) {
29 if (memcmp(p
, pattern
, pattern_len
))
31 PARA_DEBUG_LOG("found %d byte pattern@%d\n",
32 pattern_len
, p
- buf
);
35 PARA_NOTICE_LOG("%d byte pattern not found\n", pattern_len
);
40 40 9e 69 f8 4d 5b cf 11 a8 fd 00 80 5f 5c 44 2b
42 static int find_audio_stream_info(const char *buf
, int len
)
44 const char pattern
[] = {0x40, 0x9e, 0x69, 0xf8};
45 const char *p
= search_pattern(pattern
, sizeof(pattern
), buf
, len
);
48 return -E_WMA_NO_GUID
;
49 PARA_DEBUG_LOG("found audio stream guid@%0zx\n", p
- buf
);
53 static int read_header_len(char *buf
, int len
)
59 header_len
= read_u16(buf
+ 16) + 46;
60 PARA_DEBUG_LOG("header_len: %d\n", header_len
);
65 * \return Negative on errors, zero if more data is needed in order to read the
66 * full header. Positive header length on success.
68 int read_asf_header(char *buf
, int loaded
, struct asf_header_info
*ahi
)
73 ahi
->header_len
= read_header_len(buf
, loaded
);
74 if (ahi
->header_len
== 0) /* too short to read header len */
76 if (ahi
->header_len
> loaded
) /* too short to read header */
78 ret
= find_audio_stream_info(buf
, ahi
->header_len
);
81 /* FIXME: Check ranges */
82 ahi
->audio_stream_info_start
= ret
+ 16;
83 start
= buf
+ ahi
->audio_stream_info_start
;
85 ahi
->channels
= ((uint8_t *)start
)[40];
86 ahi
->sample_rate
= read_u16(start
+ 42);
87 PARA_NOTICE_LOG("%d channels, sample rate: %d\n", ahi
->channels
,
90 ahi
->bit_rate
= 8 * read_u16(start
+ 46);
91 PARA_INFO_LOG("bit rate: %d\n", ahi
->bit_rate
);
93 ahi
->block_align
= read_u16(start
+ 50);
94 PARA_INFO_LOG("block_align: %d\n", ahi
->block_align
);
96 ahi
->flags1
= read_u32(start
+ 56);
97 ahi
->flags2
= read_u16(start
+ 60);
98 PARA_INFO_LOG("read_asf_header: flags1: %d, flag2: %d\n",
99 ahi
->flags1
, ahi
->flags2
);
103 const uint8_t log2_tab
[256] = {
104 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
105 4, 4, 4, 4, 4, 4, 4, 4,
106 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
107 5, 5, 5, 5, 5, 5, 5, 5,
108 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
109 6, 6, 6, 6, 6, 6, 6, 6,
110 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
111 6, 6, 6, 6, 6, 6, 6, 6,
112 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
113 7, 7, 7, 7, 7, 7, 7, 7,
114 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
115 7, 7, 7, 7, 7, 7, 7, 7,
116 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
117 7, 7, 7, 7, 7, 7, 7, 7,
118 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
119 7, 7, 7, 7, 7, 7, 7, 7
122 int wma_log2(unsigned int v
)
125 if (v
& 0xffff0000) {