blob: bbe2d9943b963b928800b62e8150a452779202e4 [file] [log] [blame]
Dave Chapman2f2d7d42005-06-11 12:40:27 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Thom Johansen
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Dave Chapman2f2d7d42005-06-11 12:40:27 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
Thom Johansenc91e0bb2005-10-13 11:32:52 +000022#include "codeclib.h"
Andree Buschmannb3d95782010-03-07 19:34:44 +000023#include <codecs/libmusepack/mpcdec.h>
24#include <codecs/libmusepack/internal.h>
Dave Chapman2f2d7d42005-06-11 12:40:27 +000025
Jens Arnoldb8749fd2006-01-18 00:05:14 +000026CODEC_HEADER
27
Nils Wallménius6325ef92010-07-25 22:24:53 +000028static MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH] IBSS_ATTR;
Dave Chapman2f2d7d42005-06-11 12:40:27 +000029
Thom Johansen8b2531b2005-06-22 22:02:11 +000030/* Our implementations of the mpc_reader callback functions. */
Andree Buschmannb3d95782010-03-07 19:34:44 +000031static mpc_int32_t read_impl(mpc_reader *reader, void *ptr, mpc_int32_t size)
Dave Chapman2f2d7d42005-06-11 12:40:27 +000032{
Andree Buschmanna3541c02010-06-21 18:45:34 +000033 (void)reader;
Thom Johansen8b2531b2005-06-22 22:02:11 +000034 return ((mpc_int32_t)(ci->read_filebuf(ptr, size)));
Dave Chapman2f2d7d42005-06-11 12:40:27 +000035}
36
Andree Buschmannb3d95782010-03-07 19:34:44 +000037static mpc_bool_t seek_impl(mpc_reader *reader, mpc_int32_t offset)
Andree Buschmannfc1fab42010-06-23 06:25:18 +000038{
Andree Buschmanna3541c02010-06-21 18:45:34 +000039 (void)reader;
Thom Johansen8b2531b2005-06-22 22:02:11 +000040 return ci->seek_buffer(offset);
Dave Chapman2f2d7d42005-06-11 12:40:27 +000041}
42
Andree Buschmannb3d95782010-03-07 19:34:44 +000043static mpc_int32_t tell_impl(mpc_reader *reader)
Dave Chapman2f2d7d42005-06-11 12:40:27 +000044{
Andree Buschmanna3541c02010-06-21 18:45:34 +000045 (void)reader;
Thom Johansen8b2531b2005-06-22 22:02:11 +000046 return ci->curpos;
Dave Chapman2f2d7d42005-06-11 12:40:27 +000047}
48
Andree Buschmannb3d95782010-03-07 19:34:44 +000049static mpc_int32_t get_size_impl(mpc_reader *reader)
Dave Chapman2f2d7d42005-06-11 12:40:27 +000050{
Andree Buschmannb3d95782010-03-07 19:34:44 +000051 (void)reader;
Andree Buschmanna3541c02010-06-21 18:45:34 +000052 return ci->filesize;
Dave Chapman2f2d7d42005-06-11 12:40:27 +000053}
54
Michael Sevakisc537d592011-04-27 03:08:23 +000055/* this is the codec entry point */
56enum 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 */
68enum codec_status codec_run(void)
Dave Chapman2f2d7d42005-06-11 12:40:27 +000069{
Thom Johansen3d98ecf2005-10-10 19:56:40 +000070 mpc_int64_t samplesdone;
Andree Buschmann1f725eb2010-03-14 13:32:16 +000071 uint32_t frequency; /* 0.1 kHz accuracy */
72 uint32_t elapsed_time; /* milliseconds */
73 uint32_t byterate; /* bytes per second */
Andree Buschmannb3d95782010-03-07 19:34:44 +000074 mpc_status status;
Thom Johansen8b2531b2005-06-22 22:02:11 +000075 mpc_reader reader;
Thom Johansen3d98ecf2005-10-10 19:56:40 +000076 mpc_streaminfo info;
Andree Buschmannb3d95782010-03-07 19:34:44 +000077 mpc_frame_info frame;
78 mpc_demux *demux = NULL;
Michael Sevakisc537d592011-04-27 03:08:23 +000079 intptr_t param;
Thom Johansen3d98ecf2005-10-10 19:56:40 +000080
Andree Buschmannb3d95782010-03-07 19:34:44 +000081 frame.buffer = sample_buffer;
Michael Sevakisc537d592011-04-27 03:08:23 +000082
Thom Johansen8b2531b2005-06-22 22:02:11 +000083 /* Create a decoder instance */
Andree Buschmannb3d95782010-03-07 19:34:44 +000084 reader.read = read_impl;
85 reader.seek = seek_impl;
86 reader.tell = tell_impl;
Thom Johansen8b2531b2005-06-22 22:02:11 +000087 reader.get_size = get_size_impl;
Dave Chapman2f2d7d42005-06-11 12:40:27 +000088
Andree Buschmannb3d95782010-03-07 19:34:44 +000089 if (codec_init())
Michael Sevakisc537d592011-04-27 03:08:23 +000090 return CODEC_ERROR;
Thom Johansen3d98ecf2005-10-10 19:56:40 +000091
Michael Sevakisc537d592011-04-27 03:08:23 +000092 /* Prep position */
93 ci->seek_buffer(0);
Magnus Holmgren87842ca2008-02-24 19:12:15 +000094
Andree Buschmannfc1fab42010-06-23 06:25:18 +000095 /* Initialize demux/decoder. */
Andree Buschmannb3d95782010-03-07 19:34:44 +000096 demux = mpc_demux_init(&reader);
97 if (NULL == demux)
Michael Sevakisc537d592011-04-27 03:08:23 +000098 return CODEC_ERROR;
99
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000100 /* Read file's streaminfo data. */
Andree Buschmannb3d95782010-03-07 19:34:44 +0000101 mpc_demux_get_info(demux, &info);
102
Andree Buschmann1f725eb2010-03-14 13:32:16 +0000103 byterate = (mpc_uint32_t)(info.average_bitrate) / 8;
Andree Buschmannf4ac7572009-04-20 19:16:48 +0000104 frequency = info.sample_freq / 100; /* 0.1 kHz accuracy */
Michael Sevakis97f369a2007-02-10 16:34:16 +0000105 ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
Andree Buschmann1f725eb2010-03-14 13:32:16 +0000106
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);
Thom Johansen3d98ecf2005-10-10 19:56:40 +0000112
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000113 /* Set up digital signal processing for correct number of channels */
Thom Johansen3d98ecf2005-10-10 19:56:40 +0000114 /* NOTE: current musepack format only allows for stereo files
115 but code is here to handle other configurations anyway */
Andree Buschmannb3d95782010-03-07 19:34:44 +0000116 if (info.channels == 2)
Michael Sevakis97f369a2007-02-10 16:34:16 +0000117 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
Thom Johansen3d98ecf2005-10-10 19:56:40 +0000118 else if (info.channels == 1)
Michael Sevakis97f369a2007-02-10 16:34:16 +0000119 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
Michael Sevakisc537d592011-04-27 03:08:23 +0000120 else
121 return CODEC_ERROR;
Thom Johansen3d98ecf2005-10-10 19:56:40 +0000122
Thom Johansend5eefe82005-11-04 21:35:08 +0000123 codec_set_replaygain(ci->id3);
Andree Buschmannb3d95782010-03-07 19:34:44 +0000124
Adam Boota5d73092007-04-06 21:48:17 +0000125 /* Resume to saved sample offset. */
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000126 if (samplesdone > 0)
127 {
Andree Buschmannb3d95782010-03-07 19:34:44 +0000128 if (mpc_demux_seek_sample(demux, samplesdone) == MPC_STATUS_OK)
129 {
Andree Buschmannf4ac7572009-04-20 19:16:48 +0000130 elapsed_time = (samplesdone*10)/frequency;
131 ci->set_elapsed(elapsed_time);
Andree Buschmannb3d95782010-03-07 19:34:44 +0000132 }
133 else
134 {
Adam Boota5d73092007-04-06 21:48:17 +0000135 samplesdone = 0;
136 }
Adam Boota5d73092007-04-06 21:48:17 +0000137 }
Dave Chapman2f2d7d42005-06-11 12:40:27 +0000138
Thom Johansen8b2531b2005-06-22 22:02:11 +0000139 /* This is the decoding loop. */
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000140 do
141 {
Michael Sevakisc537d592011-04-27 03:08:23 +0000142 enum codec_command_action action = ci->get_command(&param);
143
144 if (action == CODEC_ACTION_HALT)
145 return CODEC_OK;
146
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000147 /* Complete seek handler. */
Michael Sevakisc537d592011-04-27 03:08:23 +0000148 if (action == CODEC_ACTION_SEEK_TIME)
Andree Buschmannb3d95782010-03-07 19:34:44 +0000149 {
Michael Sevakisc537d592011-04-27 03:08:23 +0000150 mpc_int64_t new_offset = (param/10)*frequency;
Andree Buschmannb3d95782010-03-07 19:34:44 +0000151 if (mpc_demux_seek_sample(demux, new_offset) == MPC_STATUS_OK)
152 {
Thom Johansen74101202005-11-06 19:12:27 +0000153 samplesdone = new_offset;
Thom Johansen3d98ecf2005-10-10 19:56:40 +0000154 }
Michael Sevakisc537d592011-04-27 03:08:23 +0000155
156 elapsed_time = (samplesdone*10)/frequency;
157 ci->set_elapsed(elapsed_time);
Thom Johansen3d98ecf2005-10-10 19:56:40 +0000158 ci->seek_complete();
159 }
Dave Chapman2f2d7d42005-06-11 12:40:27 +0000160
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000161 /* Decode one frame. */
Andree Buschmannb3d95782010-03-07 19:34:44 +0000162 status = mpc_demux_decode(demux, &frame);
Thom Johansen3d98ecf2005-10-10 19:56:40 +0000163 ci->yield();
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000164 if (frame.bits == -1)
Andree Buschmannb3d95782010-03-07 19:34:44 +0000165 {
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000166 /* Decoding error, exit decoding loop. */
Michael Sevakisc537d592011-04-27 03:08:23 +0000167 return (status == MPC_STATUS_OK) ? CODEC_OK : CODEC_ERROR;
Andree Buschmannb3d95782010-03-07 19:34:44 +0000168 }
169 else
170 {
Andree Buschmannfc1fab42010-06-23 06:25:18 +0000171 /* Decoding passed, insert samples to PCM buffer. */
Andree Buschmannb3d95782010-03-07 19:34:44 +0000172 ci->pcmbuf_insert(frame.buffer,
173 frame.buffer + MPC_FRAME_LENGTH,
174 frame.samples);
175 samplesdone += frame.samples;
Andree Buschmannf4ac7572009-04-20 19:16:48 +0000176 elapsed_time = (samplesdone*10)/frequency;
177 ci->set_elapsed(elapsed_time);
Andree Buschmann1f725eb2010-03-14 13:32:16 +0000178 /* Remark: rockbox offset is the file offset in bytes. So estimate
179 * this offset from the samples, sampling frequency and bitrate */
180 ci->set_offset( (samplesdone * byterate)/(frequency*100) );
Dave Chapman2f2d7d42005-06-11 12:40:27 +0000181 }
Andree Buschmannb3d95782010-03-07 19:34:44 +0000182 } while (true);
Dave Chapman2f2d7d42005-06-11 12:40:27 +0000183}