summaryrefslogtreecommitdiffstats
path: root/apps/codecs/alac.c
blob: 428fc59836201009c7cc29a31357a7376e649107 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Dave Chapman
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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"

CODEC_HEADER

static int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE] IBSS_ATTR;

/* this is the codec entry point */
enum codec_status codec_main(void)
{
  size_t n;
  demux_res_t demux_res;
  stream_t input_stream;
  uint32_t samplesdone;
  uint32_t elapsedtime;
  int samplesdecoded;
  unsigned int i;
  unsigned char* buffer;
  alac_file alac;
  int retval;

  /* Generic codec initialisation */
  ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
  ci->configure(DSP_SET_SAMPLE_DEPTH, ALAC_OUTPUT_DEPTH-1);

  next_track:
    retval = CODEC_OK;

  /* Clean and initialize decoder structures */
   memset(&demux_res , 0, sizeof(demux_res));
  if (codec_init()) {
    LOGF("ALAC: Error initialising codec\n");
    retval = CODEC_ERROR;
    goto exit;
  }

  if (codec_wait_taginfo() != 0)
    goto done;
  
  ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
  codec_set_replaygain(ci->id3);

  stream_create(&input_stream,ci);

  /* Read from ci->id3->offset before calling qtmovie_read. */
  samplesdone = (uint32_t)(((uint64_t)(ci->id3->offset) * ci->id3->frequency) /  
                (ci->id3->bitrate*128));
  
  /* 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");
    retval = CODEC_ERROR;
    goto done;
  }

  /* initialise the sound converter */
  create_alac(demux_res.sound_sample_size, demux_res.num_channels,&alac);
  alac_set_info(&alac, demux_res.codecdata);
  
  /* Set i for first frame, seek to desired sample position for resuming. */
  i=0;
  if (samplesdone > 0) {
    if (alac_seek(&demux_res, &input_stream, samplesdone,
                  &samplesdone, (int*) &i)) {
        elapsedtime = (samplesdone * 10) / (ci->id3->frequency / 100);
        ci->set_elapsed(elapsedtime);
    } else {
        samplesdone = 0;
    }
  }

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

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

    /* Request the required number of bytes from the input buffer */
    buffer=ci->request_buffer(&n, demux_res.sample_byte_size[i]);
    if (n!=demux_res.sample_byte_size[i]) {
        retval = CODEC_ERROR;
        goto done;
    }

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

    /* Advance codec buffer by amount of consumed bytes */
    ci->advance_buffer(alac.bytes_consumed);

    /* Output the audio */
    ci->yield();
    ci->pcmbuf_insert(outputbuffer[0], outputbuffer[1], samplesdecoded);

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

    i++;
  }

done:
  LOGF("ALAC: Decoded %lu samples\n",(unsigned long)samplesdone);

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

exit:
  return retval;
}