summaryrefslogtreecommitdiff
path: root/apps/plugins/codecmpc.c
blob: 3fe35705eb6b7c4cbb7e3b41c8bc9c6abc6f33fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Thom Johansen
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"
#include "playback.h"
#include "lib/codeclib.h"
#include <codecs/libmusepack/musepack.h>

static struct plugin_api* rb;
mpc_decoder decoder;

/*
  Our implementations of the mpc_reader callback functions.
*/
mpc_int32_t
read_impl(void *data, void *ptr, mpc_int32_t size)
{
  struct codec_api* ci = (struct codec_api*)data;

  return((mpc_int32_t)(ci->read_filebuf(ptr,size)));
}

bool
seek_impl(void *data, mpc_int32_t offset)
{  
  struct codec_api* ci = (struct codec_api*)data;

  /* We don't support seeking yet. */
  (void)ci;
  (void)offset;
  return 0;
}

mpc_int32_t
tell_impl(void *data)
{
  struct codec_api* ci = (struct codec_api*)data;

  return ci->curpos;
}

mpc_int32_t
get_size_impl(void *data)
{
  struct codec_api* ci = (struct codec_api*)data;
  return ci->filesize;
}

bool
canseek_impl(void *data)
{
    (void)data;
    return false;
}

static int
shift_signed(MPC_SAMPLE_FORMAT val, int shift)
{
  if (shift > 0)
    val <<= shift;
  else if (shift < 0)
    val >>= -shift;
  return (int)val;
}

#define OUTPUT_BUFFER_SIZE 65536 /* Must be an integer multiple of 4. */

unsigned char OutputBuffer[OUTPUT_BUFFER_SIZE];
MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
unsigned char *OutputPtr=OutputBuffer;
const unsigned char *OutputBufferEnd=OutputBuffer+OUTPUT_BUFFER_SIZE;

#ifdef USE_IRAM
extern char iramcopy[];
extern char iramstart[];
extern char iramend[];
#endif

/* this is the plugin entry point */
enum plugin_status plugin_start(struct plugin_api* api, void* parm)
{
  struct codec_api* ci = (struct codec_api*)parm;
  unsigned short Sample;
  unsigned long samplesdone;
  unsigned long frequency;
  unsigned status = 1;
  unsigned int i;
  mpc_reader reader;

  /* Generic plugin inititialisation */

  TEST_PLUGIN_API(api);
  rb = api;

#ifndef SIMULATOR
  rb->memcpy(iramstart, iramcopy, iramend-iramstart);
#endif

  ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*2));
  ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*16));

  next_track:

  if (codec_init(api, ci)) {
    return PLUGIN_ERROR;
  }

  /* Create a decoder instance */

  reader.read = read_impl;
  reader.seek = seek_impl;
  reader.tell = tell_impl;
  reader.get_size = get_size_impl;
  reader.canseek = canseek_impl;
  reader.data = ci;

  /* read file's streaminfo data */
  mpc_streaminfo info;
  mpc_streaminfo_init(&info);
  if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
    return PLUGIN_ERROR;  
  }
  frequency=info.sample_freq;

  /* instantiate a decoder with our file reader */
  mpc_decoder_setup(&decoder, &reader);
  if (!mpc_decoder_initialize(&decoder, &info)) {
    return PLUGIN_ERROR;
  }

  /* Initialise the output buffer. */
  OutputPtr=OutputBuffer;
  OutputBufferEnd=OutputBuffer+OUTPUT_BUFFER_SIZE;

  /* This is the decoding loop. */
  samplesdone=0;
  while (status != 0) {
    if (ci->stop_codec || ci->reload_codec) {
      break;
    }

    status = mpc_decoder_decode(&decoder, sample_buffer, 0, 0);
    if (status == (unsigned)(-1)) {
      //decode error
      return PLUGIN_ERROR;
    }
    else                    //status>0
    {
    //      file_info.current_sample += status;
    //      file_info.frames_decoded++;
      /* Convert musepack's numbers to an array of 16-bit BE signed integers */
      for(i = 0; i < status*info.channels; i += info.channels)
      {
        /* Left channel */
        Sample=shift_signed(sample_buffer[i], 16 - MPC_FIXED_POINT_SCALE_SHIFT);
        *(OutputPtr++)=Sample>>8;
        *(OutputPtr++)=Sample&0xff;

        /* Right channel. If the decoded stream is monophonic then
         * the right output channel is the same as the left one.
         */
        if(info.channels==2) {
          Sample=shift_signed(sample_buffer[i + 1], 16 - MPC_FIXED_POINT_SCALE_SHIFT);
        }
        *(OutputPtr++)=Sample>>8;
        *(OutputPtr++)=Sample&0xff;

        samplesdone++;

        /* Flush the buffer if it is full. */
        if(OutputPtr==OutputBufferEnd)
        {
          rb->yield();
          while (!ci->audiobuffer_insert(OutputBuffer, OUTPUT_BUFFER_SIZE)) {
            rb->yield();
          }

          ci->set_elapsed(samplesdone/(frequency/1000));
          OutputPtr=OutputBuffer;
        }
      }
    }
  }

  /* Flush the remaining data in the output buffer */
  if (OutputPtr > OutputBuffer) {
    rb->yield();
    while (!ci->audiobuffer_insert(OutputBuffer, OutputPtr-OutputBuffer)) {
      rb->yield();
    }
  }

  if (ci->request_next_track())
    goto next_track;

  return PLUGIN_OK;
}