summaryrefslogtreecommitdiffstats
path: root/apps/codecs/alac.c
blob: 916dd06262fa22b319b02e468c7c5aaf20399f3d (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Dave Chapman
 *
 * 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 "codeclib.h"
#include "libm4a/m4a.h"
#include "libalac/decomp.h"

#ifndef SIMULATOR
extern char iramcopy[];
extern char iramstart[];
extern char iramend[];
#endif

#define destBufferSize (1024*16)

char inputBuffer[1024*32];    /* Input buffer */
int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE] IBSS_ATTR;

struct codec_api* rb;
struct codec_api* ci;

/* this is the codec entry point */
enum codec_status codec_start(struct codec_api* api)
{
  size_t n;
  demux_res_t demux_res;
  stream_t input_stream;
  uint32_t samplesdone;
  uint32_t elapsedtime;
  uint32_t sample_duration;
  uint32_t sample_byte_size;
  int samplesdecoded;
  unsigned int i;
  unsigned char* buffer;
  alac_file alac;

  /* Generic codec initialisation */
  TEST_CODEC_API(api);

  rb = api;
  ci = (struct codec_api*)api;

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

  ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*10));
  ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
  ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128));

  ci->configure(CODEC_DSP_ENABLE, (bool *)true);
  ci->configure(DSP_DITHER, (bool *)false);
  ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_NONINTERLEAVED);
  ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(28));
  
  next_track:

  if (codec_init(api)) {
    LOGF("ALAC: Error initialising codec\n");
    return CODEC_ERROR;
  }

  while (!rb->taginfo_ready)
    rb->yield();
  
  ci->configure(DSP_SET_FREQUENCY, (long *)(rb->id3->frequency));

  stream_create(&input_stream,ci);

  /* if qtmovie_read returns successfully, the stream is up to
   * the movie data, which can be used directly by the decoder */
  if (!qtmovie_read(&input_stream, &demux_res)) {
    LOGF("ALAC: Error initialising file\n");
    return CODEC_ERROR;
  }

  /* initialise the sound converter */
  create_alac(demux_res.sample_size, demux_res.num_channels,&alac);
  alac_set_info(&alac, demux_res.codecdata);

  i=0;
  samplesdone=0;
  /* The main decoding loop */
  while (i < demux_res.num_sample_byte_sizes) {
    rb->yield();
    if (ci->stop_codec || ci->reload_codec) {
      break;
    }

    /* Deal with any pending seek requests */
    if (ci->seek_time) {
      if (alac_seek(&demux_res,&input_stream,
                    (ci->seek_time/10) * (ci->id3->frequency/100),
                    &samplesdone, &i)) {
        elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
        ci->set_elapsed(elapsedtime);
      }
      ci->seek_time = 0;
    }

    /* Lookup the length (in samples and bytes) of block i */
    if (!get_sample_info(&demux_res, i, &sample_duration, 
                         &sample_byte_size)) {
      LOGF("ALAC: Error in get_sample_info\n");
      return CODEC_ERROR;
    }

    /* Request the required number of bytes from the input buffer */

    buffer=ci->request_buffer((long*)&n,sample_byte_size);
    if (n!=sample_byte_size) {
      /* The decode_frame function requires the whole frame, so if we
         can't get it contiguously from the buffer, then we need to
         copy it via a read - i.e. we are at the buffer wraparound
         point */

      /* Check we estimated the maximum buffer size correctly */
      if (sample_byte_size > sizeof(inputBuffer)) {
        LOGF("ALAC: Input buffer < %d bytes\n",sample_byte_size);
        return CODEC_ERROR;
      }

      n=ci->read_filebuf(inputBuffer,sample_byte_size);
      if (n!=sample_byte_size) {
        LOGF("ALAC: Error reading data\n");
        return CODEC_ERROR;
      }
      buffer=inputBuffer;
    }

    /* Decode one block - returned samples will be host-endian */
    rb->yield();
    samplesdecoded=alac_decode_frame(&alac, buffer, outputbuffer, rb->yield);

    /* Advance codec buffer - unless we did a read */
    if ((char*)buffer!=(char*)inputBuffer) {
      ci->advance_buffer(n);
    }

    /* Output the audio */
    rb->yield();
    while(!ci->pcmbuf_insert_split(outputbuffer[0],
                                   outputbuffer[1],
                                   samplesdecoded*sizeof(int32_t)))
      rb->yield();

    /* Update the elapsed-time indicator */
    samplesdone+=sample_duration;
    elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
    ci->set_elapsed(elapsedtime);

    /* Keep track of current position - for resuming */
    ci->set_offset(elapsedtime);

    i++;
  }

  LOGF("ALAC: Decoded %d samples\n",samplesdone);

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

  return CODEC_OK;
}