]> git.tuebingen.mpg.de Git - paraslash.git/blob - wma_common.c
9a7a22acf30bd310f2dd14f8494f93cff5adc070
[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 "mdct.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         if (pattern_len > buf_size)
29                 return NULL;
30         for (p = buf; p + pattern_len < end; p++) {
31                 if (memcmp(p, pattern, pattern_len))
32                         continue;
33                 PARA_DEBUG_LOG("found %d byte pattern@%d\n",
34                         pattern_len, p - buf);
35                 return p;
36         }
37         PARA_NOTICE_LOG("%d byte pattern not found\n", pattern_len);
38         return NULL;
39 }
40
41 /*
42    40 9e 69 f8 4d 5b cf 11  a8 fd 00 80 5f 5c 44 2b
43  */
44 static int find_audio_stream_info(const char *buf, int len)
45 {
46         const char pattern[] = {0x40, 0x9e, 0x69, 0xf8};
47         const char *p = search_pattern(pattern, sizeof(pattern), buf, len);
48
49         if (!p) {
50                 PARA_NOTICE_LOG("audio stream guid not found");
51                 return -1;
52         }
53         PARA_DEBUG_LOG("found audio stream guid@%0zx\n", p - buf);
54         return p - buf;
55 }
56
57 static int read_header_len(char *buf, int len)
58 {
59         uint16_t header_len;
60
61         if (len < 18)
62                 return -1;
63         header_len = read_u16(buf + 16) + 46;
64         if (header_len > len)
65                 return -2;
66         PARA_DEBUG_LOG("header_len: %d\n", header_len);
67         return header_len;
68 }
69
70 int read_asf_header(char *buf, int loaded, struct asf_header_info *ahi)
71 {
72         int ret;
73         char *start;
74
75         ret = read_header_len(buf, loaded);
76         if (ret < 0)
77                 return ret;
78         ahi->header_len = ret;
79
80         ret = find_audio_stream_info(buf, ahi->header_len);
81         if (ret < 0)
82                 return ret;
83         ahi->audio_stream_info_start = ret + 16;
84         start = buf + ahi->audio_stream_info_start;
85
86         ahi->channels = ((uint8_t *)start)[40];
87         ahi->sample_rate = read_u16(start + 42);
88         PARA_NOTICE_LOG("%d channels, sample rate: %d\n", ahi->channels,
89                 ahi->sample_rate);
90
91         ahi->bit_rate = 8 * read_u16(start + 46);
92         PARA_INFO_LOG("bit rate: %d\n", ahi->bit_rate);
93
94         ahi->block_align = read_u16(start + 50);
95         PARA_INFO_LOG("block_align: %d\n", ahi->block_align);
96
97         ahi->flags1 = read_u32(start + 56);
98         ahi->flags2 = read_u16(start + 60);
99         PARA_INFO_LOG("read_asf_header: flags1: %d, flag2: %d\n",
100                 ahi->flags1, ahi->flags2);
101         return 42;
102 }
103
104 const uint8_t log2_tab[256] = {
105         0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
106         4, 4, 4, 4, 4, 4, 4, 4,
107         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
108         5, 5, 5, 5, 5, 5, 5, 5,
109         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
110         6, 6, 6, 6, 6, 6, 6, 6,
111         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
112         6, 6, 6, 6, 6, 6, 6, 6,
113         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
114         7, 7, 7, 7, 7, 7, 7, 7,
115         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
116         7, 7, 7, 7, 7, 7, 7, 7,
117         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
118         7, 7, 7, 7, 7, 7, 7, 7,
119         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
120         7, 7, 7, 7, 7, 7, 7, 7
121 };
122
123 int wma_log2(unsigned int v)
124 {
125         int n = 0;
126         if (v & 0xffff0000) {
127                 v >>= 16;
128                 n += 16;
129         }
130         if (v & 0xff00) {
131                 v >>= 8;
132                 n += 8;
133         }
134         n += log2_tab[v];
135
136         return n;
137 }