read_asf_header(): Make it take a const pointer.
[paraslash.git] / wma_common.c
1 /*
2 * Copyright (C) 2009 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 const char *search_pattern(const char *pattern, int pattern_len, const char *buf, int buf_size)
25 {
26 const char *p, *end = buf + buf_size;
27
28 for (p = buf; p + pattern_len < end; p++) {
29 if (memcmp(p, pattern, pattern_len))
30 continue;
31 PARA_DEBUG_LOG("found %d byte pattern@%d\n",
32 pattern_len, p - buf);
33 return p;
34 }
35 PARA_NOTICE_LOG("%d byte pattern not found\n", pattern_len);
36 return NULL;
37 }
38
39 /*
40 40 9e 69 f8 4d 5b cf 11 a8 fd 00 80 5f 5c 44 2b
41 */
42 static int find_audio_stream_info(const char *buf, int len)
43 {
44 const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
45 const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
46
47 if (!p)
48 return -E_WMA_NO_GUID;
49 PARA_DEBUG_LOG("found audio stream guid@%0zx\n", p - buf);
50 return p - buf + 16;
51 }
52
53 static int read_header_len(const char *buf, int len)
54 {
55 uint16_t header_len;
56
57 if (len < 18)
58 return 0;
59 header_len = read_u16(buf + 16) + 46;
60 PARA_DEBUG_LOG("header_len: %d\n", header_len);
61 return header_len;
62 }
63
64 /**
65 * Read an asf audio file header.
66 *
67 * \param buf The input buffer.
68 * \param loaded Number of bytes in \a buf.
69 * \param ahi Result pointer.
70 *
71 * \return Negative on errors, zero if more data is needed in order to read the
72 * full header, 1 on success.
73 */
74 int read_asf_header(const char *buf, int loaded, struct asf_header_info *ahi)
75 {
76 int ret;
77 const char *start;
78
79 ahi->header_len = read_header_len(buf, loaded);
80 if (ahi->header_len == 0) /* too short to read header len */
81 return 0;
82 if (ahi->header_len > loaded) /* too short to read header */
83 return 0;
84 ret = find_audio_stream_info(buf, ahi->header_len);
85 if (ret < 0)
86 return ret;
87 if (ret + 62 > loaded)
88 return 0;
89 ahi->audio_stream_info_start = ret;
90 start = buf + ahi->audio_stream_info_start;
91 ahi->channels = ((uint8_t *)start)[40];
92 ahi->sample_rate = read_u16(start + 42);
93 PARA_NOTICE_LOG("%d channels, sample rate: %d\n", ahi->channels,
94 ahi->sample_rate);
95
96 ahi->bit_rate = 8 * read_u16(start + 46);
97 PARA_INFO_LOG("bit rate: %d\n", ahi->bit_rate);
98
99 ahi->block_align = read_u16(start + 50);
100 PARA_INFO_LOG("block_align: %d\n", ahi->block_align);
101
102 ahi->flags1 = read_u32(start + 56);
103 ahi->flags2 = read_u16(start + 60);
104 PARA_INFO_LOG("read_asf_header: flags1: %d, flag2: %d\n",
105 ahi->flags1, ahi->flags2);
106 return 1;
107 }
108
109 const uint8_t log2_tab[256] = {
110 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
111 4, 4, 4, 4, 4, 4, 4, 4,
112 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
113 5, 5, 5, 5, 5, 5, 5, 5,
114 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
115 6, 6, 6, 6, 6, 6, 6, 6,
116 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
117 6, 6, 6, 6, 6, 6, 6, 6,
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,
120 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
121 7, 7, 7, 7, 7, 7, 7, 7,
122 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
123 7, 7, 7, 7, 7, 7, 7, 7,
124 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
125 7, 7, 7, 7, 7, 7, 7, 7
126 };
127
128 int wma_log2(unsigned int v)
129 {
130 int n = 0;
131 if (v & 0xffff0000) {
132 v >>= 16;
133 n += 16;
134 }
135 if (v & 0xff00) {
136 v >>= 8;
137 n += 8;
138 }
139 n += log2_tab[v];
140
141 return n;
142 }