diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/libwmapro/wma.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/libwmapro/wma.c')
-rw-r--r-- | lib/rbcodec/codecs/libwmapro/wma.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libwmapro/wma.c b/lib/rbcodec/codecs/libwmapro/wma.c new file mode 100644 index 0000000000..83cec89a4c --- /dev/null +++ b/lib/rbcodec/codecs/libwmapro/wma.c | |||
@@ -0,0 +1,164 @@ | |||
1 | /* | ||
2 | * WMA compatible codec | ||
3 | * Copyright (c) 2002-2007 The FFmpeg Project | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include "wma.h" | ||
23 | #include "codeclib.h" /* needed for av_log2() */ | ||
24 | |||
25 | /** | ||
26 | *@brief Get the samples per frame for this stream. | ||
27 | *@param sample_rate output sample_rate | ||
28 | *@param version wma version | ||
29 | *@param decode_flags codec compression features | ||
30 | *@return log2 of the number of output samples per frame | ||
31 | */ | ||
32 | int ff_wma_get_frame_len_bits(int sample_rate, int version, | ||
33 | unsigned int decode_flags) | ||
34 | { | ||
35 | |||
36 | int frame_len_bits; | ||
37 | |||
38 | if (sample_rate <= 16000) { | ||
39 | frame_len_bits = 9; | ||
40 | } else if (sample_rate <= 22050 || | ||
41 | (sample_rate <= 32000 && version == 1)) { | ||
42 | frame_len_bits = 10; | ||
43 | } else if (sample_rate <= 48000) { | ||
44 | frame_len_bits = 11; | ||
45 | } else if (sample_rate <= 96000) { | ||
46 | frame_len_bits = 12; | ||
47 | } else { | ||
48 | frame_len_bits = 13; | ||
49 | } | ||
50 | |||
51 | if (version == 3) { | ||
52 | int tmp = decode_flags & 0x6; | ||
53 | if (tmp == 0x2) { | ||
54 | ++frame_len_bits; | ||
55 | } else if (tmp == 0x4) { | ||
56 | --frame_len_bits; | ||
57 | } else if (tmp == 0x6) { | ||
58 | frame_len_bits -= 2; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | return frame_len_bits; | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Decode an uncompressed coefficient. | ||
67 | * @param s codec context | ||
68 | * @return the decoded coefficient | ||
69 | */ | ||
70 | unsigned int ff_wma_get_large_val(GetBitContext* gb) | ||
71 | { | ||
72 | /** consumes up to 34 bits */ | ||
73 | int n_bits = 8; | ||
74 | /** decode length */ | ||
75 | if (get_bits1(gb)) { | ||
76 | n_bits += 8; | ||
77 | if (get_bits1(gb)) { | ||
78 | n_bits += 8; | ||
79 | if (get_bits1(gb)) { | ||
80 | n_bits += 7; | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | return get_bits_long(gb, n_bits); | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * Decode run level compressed coefficients. | ||
89 | * @param avctx codec context | ||
90 | * @param gb bitstream reader context | ||
91 | * @param vlc vlc table for get_vlc2 | ||
92 | * @param level_table level codes | ||
93 | * @param run_table run codes | ||
94 | * @param version 0 for wma1,2 1 for wmapro | ||
95 | * @param ptr output buffer | ||
96 | * @param offset offset in the output buffer | ||
97 | * @param num_coefs number of input coefficents | ||
98 | * @param block_len input buffer length (2^n) | ||
99 | * @param frame_len_bits number of bits for escaped run codes | ||
100 | * @param coef_nb_bits number of bits for escaped level codes | ||
101 | * @return 0 on success, -1 otherwise | ||
102 | */ | ||
103 | #define av_log(...) | ||
104 | int ff_wma_run_level_decode(GetBitContext* gb, | ||
105 | VLC *vlc, | ||
106 | const int32_t *level_table, const uint16_t *run_table, | ||
107 | int version, int32_t *ptr, int offset, | ||
108 | int num_coefs, int block_len, int frame_len_bits, | ||
109 | int coef_nb_bits) | ||
110 | { | ||
111 | int32_t code, level, sign; | ||
112 | const unsigned int coef_mask = block_len - 1; | ||
113 | /* Rockbox: To be able to use rockbox' optimized mdct we need to pre-shift | ||
114 | * the values by >>(nbits-3). */ | ||
115 | const int nbits = av_log2(block_len)+1; | ||
116 | const int shift = WMAPRO_FRACT-(nbits-3); | ||
117 | for (; offset < num_coefs; offset++) { | ||
118 | code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX); | ||
119 | if (code > 1) { | ||
120 | /** normal code */ | ||
121 | offset += run_table[code]; | ||
122 | sign = !get_bits1(gb); | ||
123 | /* Rockbox: To be able to use rockbox' optimized mdct we need | ||
124 | * invert the sign. */ | ||
125 | ptr[offset & coef_mask] = sign ? level_table[code] : -level_table[code]; | ||
126 | ptr[offset & coef_mask] <<= shift; | ||
127 | } else if (code == 1) { | ||
128 | /** EOB */ | ||
129 | break; | ||
130 | } else { | ||
131 | /** escape */ | ||
132 | if (!version) { | ||
133 | level = get_bits(gb, coef_nb_bits); | ||
134 | /** NOTE: this is rather suboptimal. reading | ||
135 | block_len_bits would be better */ | ||
136 | offset += get_bits(gb, frame_len_bits); | ||
137 | } else { | ||
138 | level = ff_wma_get_large_val(gb); | ||
139 | /** escape decode */ | ||
140 | if (get_bits1(gb)) { | ||
141 | if (get_bits1(gb)) { | ||
142 | if (get_bits1(gb)) { | ||
143 | av_log(avctx,AV_LOG_ERROR, | ||
144 | "broken escape sequence\n"); | ||
145 | return -1; | ||
146 | } else | ||
147 | offset += get_bits(gb, frame_len_bits) + 4; | ||
148 | } else | ||
149 | offset += get_bits(gb, 2) + 1; | ||
150 | } | ||
151 | } | ||
152 | sign = !get_bits1(gb); | ||
153 | ptr[offset & coef_mask] = sign ? level : -level; | ||
154 | ptr[offset & coef_mask] <<= shift; | ||
155 | } | ||
156 | } | ||
157 | /** NOTE: EOB can be omitted */ | ||
158 | if (offset > num_coefs) { | ||
159 | av_log(avctx, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n"); | ||
160 | return -1; | ||
161 | } | ||
162 | |||
163 | return 0; | ||
164 | } | ||