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/mpc.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/mpc.c')
-rw-r--r-- | lib/rbcodec/codecs/mpc.c | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/mpc.c b/lib/rbcodec/codecs/mpc.c new file mode 100644 index 0000000000..b2628f988e --- /dev/null +++ b/lib/rbcodec/codecs/mpc.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 Thom Johansen | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "codeclib.h" | ||
23 | #include <codecs/libmusepack/mpcdec.h> | ||
24 | #include <codecs/libmusepack/internal.h> | ||
25 | |||
26 | CODEC_HEADER | ||
27 | |||
28 | static MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH] IBSS_ATTR; | ||
29 | |||
30 | /* Our implementations of the mpc_reader callback functions. */ | ||
31 | static mpc_int32_t read_impl(mpc_reader *reader, void *ptr, mpc_int32_t size) | ||
32 | { | ||
33 | (void)reader; | ||
34 | return ((mpc_int32_t)(ci->read_filebuf(ptr, size))); | ||
35 | } | ||
36 | |||
37 | static mpc_bool_t seek_impl(mpc_reader *reader, mpc_int32_t offset) | ||
38 | { | ||
39 | (void)reader; | ||
40 | return ci->seek_buffer(offset); | ||
41 | } | ||
42 | |||
43 | static mpc_int32_t tell_impl(mpc_reader *reader) | ||
44 | { | ||
45 | (void)reader; | ||
46 | return ci->curpos; | ||
47 | } | ||
48 | |||
49 | static mpc_int32_t get_size_impl(mpc_reader *reader) | ||
50 | { | ||
51 | (void)reader; | ||
52 | return ci->filesize; | ||
53 | } | ||
54 | |||
55 | /* this is the codec entry point */ | ||
56 | enum codec_status codec_main(enum codec_entry_call_reason reason) | ||
57 | { | ||
58 | if (reason == CODEC_LOAD) { | ||
59 | /* musepack's sample representation is 18.14 | ||
60 | * DSP_SET_SAMPLE_DEPTH = 14 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 29 */ | ||
61 | ci->configure(DSP_SET_SAMPLE_DEPTH, 29); | ||
62 | } | ||
63 | |||
64 | return CODEC_OK; | ||
65 | } | ||
66 | |||
67 | /* this is called for each file to process */ | ||
68 | enum codec_status codec_run(void) | ||
69 | { | ||
70 | mpc_int64_t samplesdone; | ||
71 | uint32_t frequency; /* 0.1 kHz accuracy */ | ||
72 | uint32_t elapsed_time; /* milliseconds */ | ||
73 | uint32_t byterate; /* bytes per second */ | ||
74 | mpc_status status; | ||
75 | mpc_reader reader; | ||
76 | mpc_streaminfo info; | ||
77 | mpc_frame_info frame; | ||
78 | mpc_demux *demux = NULL; | ||
79 | intptr_t param; | ||
80 | |||
81 | frame.buffer = sample_buffer; | ||
82 | |||
83 | /* Create a decoder instance */ | ||
84 | reader.read = read_impl; | ||
85 | reader.seek = seek_impl; | ||
86 | reader.tell = tell_impl; | ||
87 | reader.get_size = get_size_impl; | ||
88 | |||
89 | if (codec_init()) | ||
90 | return CODEC_ERROR; | ||
91 | |||
92 | /* Prep position */ | ||
93 | ci->seek_buffer(0); | ||
94 | |||
95 | /* Initialize demux/decoder. */ | ||
96 | demux = mpc_demux_init(&reader); | ||
97 | if (NULL == demux) | ||
98 | return CODEC_ERROR; | ||
99 | |||
100 | /* Read file's streaminfo data. */ | ||
101 | mpc_demux_get_info(demux, &info); | ||
102 | |||
103 | byterate = (mpc_uint32_t)(info.average_bitrate) / 8; | ||
104 | frequency = info.sample_freq / 100; /* 0.1 kHz accuracy */ | ||
105 | ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq); | ||
106 | |||
107 | /* Remark: rockbox offset is the file offset in bytes. So, estimate the | ||
108 | * sample seek position from the file offset, the sampling frequency and | ||
109 | * the bitrate. As the saved position is exactly calculated the reverse way | ||
110 | * there is no loss of information except rounding. */ | ||
111 | samplesdone = 100 * (((mpc_uint64_t)ci->id3->offset * frequency) / byterate); | ||
112 | |||
113 | /* Set up digital signal processing for correct number of channels */ | ||
114 | /* NOTE: current musepack format only allows for stereo files | ||
115 | but code is here to handle other configurations anyway */ | ||
116 | if (info.channels == 2) | ||
117 | ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED); | ||
118 | else if (info.channels == 1) | ||
119 | ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); | ||
120 | else | ||
121 | return CODEC_ERROR; | ||
122 | |||
123 | codec_set_replaygain(ci->id3); | ||
124 | |||
125 | /* Resume to saved sample offset. */ | ||
126 | elapsed_time = 0; | ||
127 | |||
128 | if (samplesdone > 0) | ||
129 | { | ||
130 | if (mpc_demux_seek_sample(demux, samplesdone) == MPC_STATUS_OK) | ||
131 | { | ||
132 | elapsed_time = (samplesdone*10)/frequency; | ||
133 | } | ||
134 | else | ||
135 | { | ||
136 | samplesdone = 0; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | ci->set_elapsed(elapsed_time); | ||
141 | |||
142 | /* This is the decoding loop. */ | ||
143 | do | ||
144 | { | ||
145 | enum codec_command_action action = ci->get_command(¶m); | ||
146 | |||
147 | if (action == CODEC_ACTION_HALT) | ||
148 | return CODEC_OK; | ||
149 | |||
150 | /* Complete seek handler. */ | ||
151 | if (action == CODEC_ACTION_SEEK_TIME) | ||
152 | { | ||
153 | mpc_int64_t new_offset = (param/10)*frequency; | ||
154 | if (mpc_demux_seek_sample(demux, new_offset) == MPC_STATUS_OK) | ||
155 | { | ||
156 | samplesdone = new_offset; | ||
157 | } | ||
158 | |||
159 | elapsed_time = (samplesdone*10)/frequency; | ||
160 | ci->set_elapsed(elapsed_time); | ||
161 | ci->seek_complete(); | ||
162 | } | ||
163 | |||
164 | /* Decode one frame. */ | ||
165 | status = mpc_demux_decode(demux, &frame); | ||
166 | ci->yield(); | ||
167 | if (frame.bits == -1) | ||
168 | { | ||
169 | /* Decoding error, exit decoding loop. */ | ||
170 | return (status == MPC_STATUS_OK) ? CODEC_OK : CODEC_ERROR; | ||
171 | } | ||
172 | else | ||
173 | { | ||
174 | /* Decoding passed, insert samples to PCM buffer. */ | ||
175 | ci->pcmbuf_insert(frame.buffer, | ||
176 | frame.buffer + MPC_FRAME_LENGTH, | ||
177 | frame.samples); | ||
178 | samplesdone += frame.samples; | ||
179 | elapsed_time = (samplesdone*10)/frequency; | ||
180 | ci->set_elapsed(elapsed_time); | ||
181 | /* Remark: rockbox offset is the file offset in bytes. So estimate | ||
182 | * this offset from the samples, sampling frequency and bitrate */ | ||
183 | ci->set_offset( (samplesdone * byterate)/(frequency*100) ); | ||
184 | } | ||
185 | } while (true); | ||
186 | } | ||